From b26a594cc868ff1f2d5659361429e05f94476722 Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Thu, 17 Jul 2025 15:06:01 -0500 Subject: [PATCH] Implement the load_from_file function The implementation is dead simple, and pretty dumb. I'm not going to figure out all the different IO errors I might see. Instead, the function will report that it couldn't read the file and call it good. --- src/config.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/config.rs b/src/config.rs index 08a184f..1f611aa 100644 --- a/src/config.rs +++ b/src/config.rs @@ -8,6 +8,7 @@ pub enum Error { BadFormat, NoSuchProperty, NoSuchTable, + CouldntReadFile, TomlWrap(toml::de::Error), } @@ -72,7 +73,14 @@ struct WholeFile { } fn load_from_file(path: &str) -> Result { - todo!(); + let res = std::fs::read_to_string(path); + match res { + Ok(s) => read_conf_str(s.as_str()), + Err(e) => { + eprintln!("->> file io err: {:?}", e); + Err(Error::CouldntReadFile) + } + } } fn read_conf_str(text: &str) -> Result { @@ -260,7 +268,7 @@ mod tests { fn load_from_file_missing() -> Result<()> { let res = load_from_file("test_data/doesnt_exist.toml"); let err = res.unwrap_err(); - assert_eq!(err, todo!("Need a new config::Error variant to indicate a missing config file")); + assert_eq!(err, Error::CouldntReadFile); Ok(()) }