Skip to content

Change parse_duration onto parsing using humantime #38

@ikripaka

Description

@ikripaka

Describe the feature

The idea of this issue is to replace manual parsing with humantime, so it accepts any possible duration notation.

pub fn parse_duration(s: &str) -> Result<i64, Error> {
    let s = s.trim();
    if s.is_empty() {
        return Err(Error::Config("Empty duration".to_string()));
    }

    let (num_str, unit) = s.split_at(s.len() - 1);
    let num: i64 = num_str
        .parse()
        .map_err(|_| Error::Config(format!("Invalid duration number: {num_str}")))?;

    let multiplier = match unit {
        "s" => 1,
        "m" => 60,
        "h" => 3600,
        "d" => 86_400,
        "w" => 604_800,
        _ => return Err(Error::Config(format!("Invalid duration unit: {unit}. Use s/m/h/d/w"))),
    };

    Ok(num * multiplier)
}

Link to the code.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions