Add property-get utility function
This function will look for a given property in a given table. It gives back either the property, or an explanation of why it could not be retrieved.
This commit is contained in:
@@ -10,6 +10,7 @@ itertools = "0.10.0"
|
|||||||
reqwest = { version = "0.11.13", features = ["json", "stream", "multipart"] }
|
reqwest = { version = "0.11.13", features = ["json", "stream", "multipart"] }
|
||||||
serde = { version = "1.0.152", features = ["derive"] }
|
serde = { version = "1.0.152", features = ["derive"] }
|
||||||
tokio = { version = "1.24.2", features = ["macros", "rt-multi-thread"] }
|
tokio = { version = "1.24.2", features = ["macros", "rt-multi-thread"] }
|
||||||
|
toml = "0.5"
|
||||||
|
|
||||||
# Packages available in Debian (Sid)
|
# Packages available in Debian (Sid)
|
||||||
# clap = "4.5.23"
|
# clap = "4.5.23"
|
||||||
|
|||||||
@@ -1,8 +1,21 @@
|
|||||||
|
use toml::{Value, value::Table};
|
||||||
|
|
||||||
pub type Result<T> = core::result::Result<T, Error>;
|
pub type Result<T> = core::result::Result<T, Error>;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {}
|
#[cfg_attr(test, derive(PartialEq))]
|
||||||
|
pub enum Error {
|
||||||
|
BadFormat,
|
||||||
|
NoSuchProperty,
|
||||||
|
NoSuchTable,
|
||||||
|
TomlWrap(toml::de::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<toml::de::Error> for Error {
|
||||||
|
fn from(value: toml::de::Error) -> Self {
|
||||||
|
Error::TomlWrap(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl core::fmt::Display for Error{
|
impl core::fmt::Display for Error{
|
||||||
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
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 {}
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
}
|
|
||||||
|
#[test]
|
||||||
|
fn read_single_prop() -> Result<()> {
|
||||||
|
let fx_input_str = "owner = \"dingus\"";
|
||||||
|
let fx_value = fx_input_str.parse::<Value>()?;
|
||||||
|
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::<Value>()?;
|
||||||
|
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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user