tools::file_read_firstline - improve error message

This commit is contained in:
Dietmar Maurer 2019-02-16 09:36:29 +01:00
parent 17ed456c2e
commit 6235a41862
1 changed files with 9 additions and 7 deletions

View File

@ -85,21 +85,23 @@ pub fn map_struct_mut<T>(buffer: &mut [u8]) -> Result<&mut T, Error> {
Ok(unsafe { &mut * (buffer.as_ptr() as *mut T) }) Ok(unsafe { &mut * (buffer.as_ptr() as *mut T) })
} }
pub fn file_read_firstline<P: AsRef<Path>>(path: P) -> Result<String, std::io::Error> { pub fn file_read_firstline<P: AsRef<Path>>(path: P) -> Result<String, Error> {
let path = path.as_ref(); let path = path.as_ref();
let file = std::fs::File::open(path)?; try_block!({
let file = std::fs::File::open(path)?;
use std::io::{BufRead, BufReader}; use std::io::{BufRead, BufReader};
let mut reader = BufReader::new(file); let mut reader = BufReader::new(file);
let mut line = String::new(); let mut line = String::new();
let _ = reader.read_line(&mut line)?; let _ = reader.read_line(&mut line)?;
Ok(line) Ok(line)
}).map_err(|err: Error| format_err!("unable to read {:?} - {}", path, err))
} }
pub fn file_get_contents<P: AsRef<Path>>(path: P) -> Result<Vec<u8>, std::io::Error> { pub fn file_get_contents<P: AsRef<Path>>(path: P) -> Result<Vec<u8>, std::io::Error> {