mirror of
https://github.com/pcvolkmer/checkbar.git
synced 2025-04-19 11:06:50 +00:00
Implement Default for Config
This commit is contained in:
parent
01afd6eba6
commit
513d327879
@ -6,9 +6,12 @@ use std::{env, fs};
|
||||
use serde::de::{Error, Visitor};
|
||||
use serde::{Deserialize, Deserializer};
|
||||
|
||||
#[derive(Default, Deserialize)]
|
||||
#[derive(Deserialize)]
|
||||
pub struct Config {
|
||||
#[serde(default, deserialize_with = "deserialize_duration")]
|
||||
#[serde(
|
||||
default = "default_duration",
|
||||
deserialize_with = "deserialize_duration"
|
||||
)]
|
||||
pub interval: Option<Duration>,
|
||||
#[serde(default)]
|
||||
pub colors: ColorConfig,
|
||||
@ -39,6 +42,16 @@ impl Config {
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
interval: Some(Duration::from_secs(60)),
|
||||
colors: ColorConfig::default(),
|
||||
checks: vec![],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct ColorConfig {
|
||||
pub up: String,
|
||||
@ -71,6 +84,10 @@ pub enum CheckType {
|
||||
Tcp,
|
||||
}
|
||||
|
||||
fn default_duration() -> Option<Duration> {
|
||||
Some(Duration::from_secs(60))
|
||||
}
|
||||
|
||||
fn deserialize_duration<'de, D>(d: D) -> Result<Option<Duration>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
@ -144,7 +161,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_should_parse_config_without_interval() {
|
||||
fn test_should_parse_config_and_use_default_interval() {
|
||||
let config: Config = toml::from_str(
|
||||
r#"
|
||||
[[checks]]
|
||||
@ -154,7 +171,7 @@ mod tests {
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(config.interval, None);
|
||||
assert_eq!(config.interval, Some(Duration::from_secs(60)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -195,7 +212,10 @@ mod tests {
|
||||
#[test]
|
||||
fn test_should_return_default_if_no_config_file() {
|
||||
let config = Config::read_file("./tests/no_testconfig.toml");
|
||||
assert_eq!(config.interval, None);
|
||||
assert_eq!(config.interval, Some(Duration::from_secs(60)));
|
||||
assert_eq!(config.colors.up, "#00FF00".to_string());
|
||||
assert_eq!(config.colors.warn, "#FFFF00".to_string());
|
||||
assert_eq!(config.colors.down, "#FF0000".to_string());
|
||||
assert_eq!(config.checks.len(), 0);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user