Skip to content

Commit

Permalink
Merge pull request #146 from sachaos/limit-minimum-interval
Browse files Browse the repository at this point in the history
Minimum interval validation
  • Loading branch information
sachaos authored Aug 29, 2024
2 parents 9a52e5a + 7c13156 commit ad07647
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::PathBuf;

use chrono::{format::Parsed, Duration};
use clap::Parser;
use color_eyre::eyre::Result;
use color_eyre::eyre::{bail, eyre, Result};
use serde_with::serde_as;

use crate::utils::version;
Expand All @@ -20,9 +20,9 @@ pub struct Cli {
#[arg(
short = 'n',
long = "interval",
value_parser = parse_duration_from_str,
value_parser = validate_duration,
default_value = "2s",
help = "Seconds to wait between updates",
help = "Seconds to wait between updates (>= 100ms)",
)]
pub interval: Duration,

Expand Down Expand Up @@ -123,6 +123,15 @@ pub struct Cli {
pub load: Option<PathBuf>,
}

fn validate_duration(s: &str) -> Result<Duration> {
let d = parse_duration_from_str(s)?;
if d < Duration::milliseconds(100) {
bail!("The short interval is not allowed (less than 100ms)");
}

Ok(d)
}

fn parse_duration_from_str(s: &str) -> Result<Duration> {
match humantime::parse_duration(s) {
Ok(d) => Ok(Duration::from_std(d)?),
Expand Down

0 comments on commit ad07647

Please sign in to comment.