Skip to content

Commit

Permalink
feat: validating config
Browse files Browse the repository at this point in the history
  • Loading branch information
luisnquin committed Jan 28, 2024
1 parent a9d9c1b commit c306092
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use log::{error, info, warn};
use serde::Deserialize;
use std::{env, fs, path::Path};
use std::{env, fs, path::Path, process};

#[derive(Debug, Clone, Deserialize)]
pub struct Bound {
Expand Down Expand Up @@ -94,6 +94,18 @@ impl Config {
}
}

pub fn validate(&self) {
if self.reminder.threshold <= self.warn.threshold {
error!("reminder threshold must be higher than warn threshold");
process::exit(1);
}

if self.warn.threshold <= self.threat.threshold {
error!("warn threshold must be higher than threat threshold");
process::exit(1);
}
}

fn merge(mut self, other: Config) -> Config {
let warn_if_not_zero = |threshold: u8, label: &str| {
if threshold != 0 {
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ fn main() {

let config = Config::parse_or_default(cp);
debug!("{:#?}", config);
config.validate();

// Calculates the notification level based on the provided battery capacity.
let get_notification_level = |capacity: u8| -> BatteryNotificationLevel {
Expand Down

0 comments on commit c306092

Please sign in to comment.