Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimum interval validation #146

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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