use super::{ConfigLoader, ConfigMatch};
use anyhow::{Context, Result};
pub(crate) const LYCHEE_CONFIG_FILE: &str = "lychee.toml";
pub(crate) struct LycheeTomlLoader;
impl ConfigLoader for LycheeTomlLoader {
fn filename(&self) -> &str {
LYCHEE_CONFIG_FILE
}
fn load(&self, contents: &str) -> Result<ConfigMatch> {
let config =
toml::from_str(contents).with_context(|| "Failed to parse configuration file")?;
Ok(ConfigMatch::Found(Box::new(config)))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_load_config() {
let toml = r#"
exclude = ["foo"]
"#;
let result = LycheeTomlLoader.load(toml).unwrap();
match result {
ConfigMatch::Found(config) => assert_eq!(config.exclude, vec!["foo".to_string()]),
ConfigMatch::NotFound => panic!("Expected config to be found"),
}
}
#[test]
fn test_load_invalid_config() {
let toml = r#"
exclude = "foo" # should be an array
"#;
assert!(LycheeTomlLoader.load(toml).is_err());
}
}