1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
use serde::Deserialize;
use std::fmt;
use std::fs;
/// Reads from a specified filepath, parses as toml and returns as the
/// desired deserialized type.
///
/// Example:
/// ```
/// use serde::Deserialize;
/// use lib_humus::read_toml_from_file;
///
/// #[derive(Deserialize)]
/// struct Config {
/// name: String,
/// }
///
/// fn main() {
/// let cfg: Config = read_toml_from_file("config.toml").unwrap();
/// println!("Hello {}!", cfg.name);
/// }
/// ```
///
pub fn read_toml_from_file<T>(path: &String) -> Result<T,TomlError>
where T: for<'de> Deserialize<'de> {
let text = match fs::read_to_string(path) {
Ok(t) => t,
Err(e) => {
return Err(TomlError::FileError{
path: path.clone(),
io_error: e
});
}
};
match toml::from_str(&text) {
Ok(t) => Ok(t),
Err(e) => {
return Err(TomlError::ParseError{
path: path.clone(),
toml_error: e
});
}
}
}
/// Returned when [read_toml_from_file()] fails.
///
/// [read_toml_from_file]: ./fn.read_toml_from_file.html
pub enum TomlError {
/// There was an error reading the file from the filesystem.
FileError{
/// The path the file is supposed to be at
path: String,
/// What went wrong
io_error: std::io::Error
},
/// There was an error parsing the file content as toml.
ParseError{
/// The path the file was read from
path: String,
/// What went wrong
toml_error: toml::de::Error
},
}
impl fmt::Display for TomlError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::FileError{path, io_error} =>
write!(f,"Error while reading file '{path}': {io_error}"),
Self::ParseError{path, toml_error} =>
write!(f,"Unable to parse file as toml '{path}':\n{toml_error}"),
}
}
}