diff --git a/src/config.rs b/src/config.rs index 40c50b8..93adfa5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -26,6 +26,15 @@ impl core::fmt::Display for Error{ impl std::error::Error for Error {} +/// The outer value must be a Table so we can get the sub-table from it. +fn get_table<'outer>(outer: &'outer Table, table_name: String) -> Result<&'outer Table> { + Ok(outer + .get(&table_name) + .ok_or(Error::NoSuchTable)? + .as_table() + .ok_or(Error::BadFormat)?) +} + /// The config properties are individual strings. This gets the named property, /// or an error explaining why it couldn't be fetched. fn get_property<'outer>(outer: &'outer Table, property: String) -> Result<&'outer String> { @@ -39,6 +48,8 @@ fn get_property<'outer>(outer: &'outer Table, property: String) -> Result<&'oute #[cfg(test)] mod tests { + use toml::map::Map; + use super::*; #[test] @@ -65,4 +76,17 @@ mod tests { assert_eq!(res, expected); Ok(()) } + + #[test] + fn read_table() -> Result<()> { + let fx_input_str = "[tab]\nwith_a_garbage = \"value\""; + let fx_value = fx_input_str.parse::()?; + let fx_value = fx_value.as_table().ok_or(Error::BadFormat)?; + let mut expected: Map = Map::new(); + expected.insert(String::from("with_a_garbage"), Value::String(String::from("value"))); + + let res = get_table(&fx_value, String::from("tab"))?; + assert_eq!(res, &expected); + Ok(()) + } }