use rudof_config::TomlConfig;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(default)]
pub struct LoggingConfig {
#[serde(rename = "level")]
pub(crate) level: String,
}
impl LoggingConfig {
pub fn new() -> Self {
Self {
level: Self::default_level(),
}
}
pub fn with_level(mut self, level: Option<String>) -> Self {
self.level = level.unwrap_or_default();
self
}
}
impl LoggingConfig {
pub fn level(&self) -> Option<&str> {
if self.level.is_empty() { None } else { Some(&self.level) }
}
}
#[allow(dead_code)]
#[rustfmt::skip]
impl LoggingConfig {
#[inline] fn default_level() -> String { String::new() }
}
impl Default for LoggingConfig {
fn default() -> Self {
Self::new()
}
}
impl TomlConfig for LoggingConfig {}
#[cfg(test)]
mod tests {
use super::LoggingConfig;
use rudof_config::TomlConfig;
#[test]
fn defaults() {
let c = LoggingConfig::default();
assert_eq!(c.level(), None);
}
#[test]
fn partial_toml_fills_remaining_defaults() {
let c = LoggingConfig::from_toml_str(r#"level = "debug""#).unwrap();
assert_eq!(c.level(), Some("debug"));
}
#[test]
fn toml_round_trip() {
let c = LoggingConfig::default().with_level(Some("trace".to_string()));
let s = c.to_toml_string().unwrap();
let d = LoggingConfig::from_toml_str(&s).unwrap();
assert_eq!(c, d);
}
#[test]
fn empty_level_is_always_present_in_the_toml_tree() {
let s = LoggingConfig::default().to_toml_string().unwrap();
assert!(s.contains("level"));
}
}