diff --git a/Cargo.toml b/Cargo.toml index 4fd569c..5a0ea7c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ itertools = "0.10.0" reqwest = { version = "0.11.13", features = ["json", "stream", "multipart"] } serde = { version = "1.0.152", features = ["derive"] } tokio = { version = "1.24.2", features = ["macros", "rt-multi-thread"] } +toml = "0.5" # Packages available in Debian (Sid) # clap = "4.5.23" diff --git a/src/config.rs b/src/config.rs index 5126e85..40c50b8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,8 +1,21 @@ +use toml::{Value, value::Table}; pub type Result = core::result::Result; #[derive(Debug)] -pub enum Error {} +#[cfg_attr(test, derive(PartialEq))] +pub enum Error { + BadFormat, + NoSuchProperty, + NoSuchTable, + TomlWrap(toml::de::Error), +} + +impl From for Error { + fn from(value: toml::de::Error) -> Self { + Error::TomlWrap(value) + } +} impl core::fmt::Display for Error{ fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -13,7 +26,43 @@ impl core::fmt::Display for Error{ impl std::error::Error for Error {} +/// 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> { + let maybe_prop = outer.get(&property).ok_or(Error::NoSuchProperty)?; + if let Value::String(text) = maybe_prop { + Ok(text) + } else { + Err(Error::BadFormat) + } +} + #[cfg(test)] mod tests { - use super::*; -} \ No newline at end of file + use super::*; + + #[test] + fn read_single_prop() -> Result<()> { + let fx_input_str = "owner = \"dingus\""; + let fx_value = fx_input_str.parse::()?; + let fx_value = fx_value.as_table().ok_or(Error::NoSuchTable)?; + let expected = "dingus"; + + let res = get_property(&fx_value, String::from("owner"))?; + assert_eq!(res, expected); + Ok(()) + } + + // The property is given the value of empty-string `""` + #[test] + fn read_single_prop_empty_quotes() -> Result<()> { + let fx_input_str = "owner = \"\""; + let fx_value = fx_input_str.parse::()?; + let fx_value = fx_value.as_table().ok_or(Error::NoSuchTable)?; + let expected = ""; + + let res = get_property(&fx_value, String::from("owner"))?; + assert_eq!(res, expected); + Ok(()) + } +}