mirror of
https://github.com/pcvolkmer/checkbar.git
synced 2025-07-02 14:22:54 +00:00
Implement simple parser for update interval duration
This commit is contained in:
@ -1,9 +1,11 @@
|
||||
use regex::Regex;
|
||||
use serde::Deserialize;
|
||||
use std::time::Duration;
|
||||
use std::{env, fs};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Config {
|
||||
pub interval: Option<u64>,
|
||||
pub interval: Option<String>,
|
||||
pub colors: Option<ColorConfig>,
|
||||
pub checks: Vec<CheckConfig>,
|
||||
}
|
||||
@ -57,3 +59,79 @@ pub fn get_config() -> Config {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_duration(value: Option<String>) -> Duration {
|
||||
let mut result = 0;
|
||||
let value = match &value {
|
||||
Some(value) => {
|
||||
let result = value;
|
||||
result.as_str()
|
||||
}
|
||||
_ => return Duration::from_secs(60),
|
||||
};
|
||||
if let Ok(re) = Regex::new(r"^((?P<hours>\d+)h)?((?P<minutes>\d+)m)?((?P<seconds>\d+)s?)?$") {
|
||||
if re.is_match(value) {
|
||||
let parts = re.captures_iter(value).nth(0).unwrap();
|
||||
if let Some(hours) = parts.name("hours") {
|
||||
result += match u64::from_str_radix(hours.as_str(), 10) {
|
||||
Ok(value) => value * 60 * 60,
|
||||
_ => 0,
|
||||
};
|
||||
}
|
||||
if let Some(minutes) = parts.name("minutes") {
|
||||
result += match u64::from_str_radix(minutes.as_str(), 10) {
|
||||
Ok(value) => value * 60,
|
||||
_ => 0,
|
||||
};
|
||||
}
|
||||
if let Some(seconds) = parts.name("seconds") {
|
||||
result += match u64::from_str_radix(seconds.as_str(), 10) {
|
||||
Ok(value) => value,
|
||||
_ => 0,
|
||||
};
|
||||
}
|
||||
} else {
|
||||
return Duration::from_secs(60);
|
||||
}
|
||||
}
|
||||
Duration::from_secs(result)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::config::parse_duration;
|
||||
use std::time::Duration;
|
||||
|
||||
#[test]
|
||||
fn test_should_parse_durations() {
|
||||
assert_eq!(
|
||||
parse_duration(Some("1m30s".to_string())),
|
||||
Duration::from_secs(90)
|
||||
);
|
||||
assert_eq!(
|
||||
parse_duration(Some("2m".to_string())),
|
||||
Duration::from_secs(120)
|
||||
);
|
||||
assert_eq!(
|
||||
parse_duration(Some("1h1m1s".to_string())),
|
||||
Duration::from_secs(3661)
|
||||
);
|
||||
assert_eq!(
|
||||
parse_duration(Some("90".to_string())),
|
||||
Duration::from_secs(90)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_should_return_default_for_unparseable_durations() {
|
||||
assert_eq!(parse_duration(None), Duration::from_secs(60));
|
||||
assert_eq!(
|
||||
parse_duration(Some("invalid".to_string())),
|
||||
Duration::from_secs(60)
|
||||
);
|
||||
assert_eq!(
|
||||
parse_duration(Some("1x30m10q".to_string())),
|
||||
Duration::from_secs(60)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -2,14 +2,13 @@ mod checker;
|
||||
mod config;
|
||||
|
||||
use std::process;
|
||||
use std::time::Duration;
|
||||
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
use tokio::task;
|
||||
|
||||
use crate::checker::{ActuatorChecker, CheckResult, HttpChecker, TcpChecker};
|
||||
use crate::config::{get_config, CheckConfig, CheckType};
|
||||
use crate::config::{get_config, parse_duration, CheckConfig, CheckType};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct ClickEvent {
|
||||
@ -102,7 +101,7 @@ async fn main() {
|
||||
loop {
|
||||
let config = get_config();
|
||||
print_states(&config.checks).await;
|
||||
std::thread::sleep(Duration::from_secs(config.interval.unwrap_or(60)));
|
||||
std::thread::sleep(parse_duration(config.interval));
|
||||
}
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user