mirror of
https://github.com/pcvolkmer/checkbar.git
synced 2025-04-19 19:16: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::de::{Error, Visitor};
|
||||||
use serde::{Deserialize, Deserializer};
|
use serde::{Deserialize, Deserializer};
|
||||||
|
|
||||||
#[derive(Default, Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
#[serde(default, deserialize_with = "deserialize_duration")]
|
#[serde(
|
||||||
|
default = "default_duration",
|
||||||
|
deserialize_with = "deserialize_duration"
|
||||||
|
)]
|
||||||
pub interval: Option<Duration>,
|
pub interval: Option<Duration>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub colors: ColorConfig,
|
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)]
|
#[derive(Deserialize)]
|
||||||
pub struct ColorConfig {
|
pub struct ColorConfig {
|
||||||
pub up: String,
|
pub up: String,
|
||||||
@ -71,6 +84,10 @@ pub enum CheckType {
|
|||||||
Tcp,
|
Tcp,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn default_duration() -> Option<Duration> {
|
||||||
|
Some(Duration::from_secs(60))
|
||||||
|
}
|
||||||
|
|
||||||
fn deserialize_duration<'de, D>(d: D) -> Result<Option<Duration>, D::Error>
|
fn deserialize_duration<'de, D>(d: D) -> Result<Option<Duration>, D::Error>
|
||||||
where
|
where
|
||||||
D: Deserializer<'de>,
|
D: Deserializer<'de>,
|
||||||
@ -144,7 +161,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_should_parse_config_without_interval() {
|
fn test_should_parse_config_and_use_default_interval() {
|
||||||
let config: Config = toml::from_str(
|
let config: Config = toml::from_str(
|
||||||
r#"
|
r#"
|
||||||
[[checks]]
|
[[checks]]
|
||||||
@ -154,7 +171,7 @@ mod tests {
|
|||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(config.interval, None);
|
assert_eq!(config.interval, Some(Duration::from_secs(60)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -195,7 +212,10 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_should_return_default_if_no_config_file() {
|
fn test_should_return_default_if_no_config_file() {
|
||||||
let config = Config::read_file("./tests/no_testconfig.toml");
|
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);
|
assert_eq!(config.checks.len(), 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user