Change api::files::read_single to return error enum instead of mutually exclusive option
This commit is contained in:
parent
35775afd84
commit
c51cbc9aab
3 changed files with 14 additions and 6 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -1037,7 +1037,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "usdpl-back"
|
name = "usdpl-back"
|
||||||
version = "0.7.2"
|
version = "0.8.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-recursion",
|
"async-recursion",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "usdpl-back"
|
name = "usdpl-back"
|
||||||
version = "0.7.2"
|
version = "0.8.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "GPL-3.0-only"
|
license = "GPL-3.0-only"
|
||||||
repository = "https://github.com/NGnius/usdpl-rs"
|
repository = "https://github.com/NGnius/usdpl-rs"
|
||||||
|
|
|
@ -12,11 +12,19 @@ pub fn write_single<P: AsRef<Path>, D: Display>(path: P, display: D) -> Result<(
|
||||||
write!(file, "{}", display)
|
write!(file, "{}", display)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// read_single error
|
||||||
|
pub enum ReadError<E> {
|
||||||
|
/// IO Error
|
||||||
|
Io(io::Error),
|
||||||
|
/// String parsing error
|
||||||
|
Parse(E),
|
||||||
|
}
|
||||||
|
|
||||||
/// Read something from a file.
|
/// Read something from a file.
|
||||||
/// Useful for kernel configuration files.
|
/// Useful for kernel configuration files.
|
||||||
pub fn read_single<P: AsRef<Path>, D: FromStr<Err=E>, E>(path: P) -> Result<D, (Option<io::Error>, Option<E>)> {
|
pub fn read_single<P: AsRef<Path>, D: FromStr<Err=E>, E>(path: P) -> Result<D, ReadError<E>> {
|
||||||
let mut file = File::open(path).map_err(|e| (Some(e), None))?;
|
let mut file = File::open(path).map_err(ReadError::Io)?;
|
||||||
let mut string = String::new();
|
let mut string = String::new();
|
||||||
file.read_to_string(&mut string).map_err(|e| (Some(e), None))?;
|
file.read_to_string(&mut string).map_err(ReadError::Io)?;
|
||||||
string.trim().parse().map_err(|e| (None, Some(e)))
|
string.trim().parse().map_err(ReadError::Parse)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue