Change api::files::read_single to return error enum instead of mutually exclusive option

This commit is contained in:
NGnius (Graham) 2022-11-27 22:11:41 -05:00
parent 35775afd84
commit c51cbc9aab
3 changed files with 14 additions and 6 deletions

2
Cargo.lock generated
View file

@ -1037,7 +1037,7 @@ dependencies = [
[[package]]
name = "usdpl-back"
version = "0.7.2"
version = "0.8.0"
dependencies = [
"async-recursion",
"async-trait",

View file

@ -1,6 +1,6 @@
[package]
name = "usdpl-back"
version = "0.7.2"
version = "0.8.0"
edition = "2021"
license = "GPL-3.0-only"
repository = "https://github.com/NGnius/usdpl-rs"

View file

@ -12,11 +12,19 @@ pub fn write_single<P: AsRef<Path>, D: Display>(path: P, display: D) -> Result<(
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.
/// 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>)> {
let mut file = File::open(path).map_err(|e| (Some(e), None))?;
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(ReadError::Io)?;
let mut string = String::new();
file.read_to_string(&mut string).map_err(|e| (Some(e), None))?;
string.trim().parse().map_err(|e| (None, Some(e)))
file.read_to_string(&mut string).map_err(ReadError::Io)?;
string.trim().parse().map_err(ReadError::Parse)
}