Skip to content

Latest commit

 

History

History
62 lines (51 loc) · 2.08 KB

README.md

File metadata and controls

62 lines (51 loc) · 2.08 KB

Parse Duration with Fallbacks

Very thin wrapper over the humantime library that provides convenience methods to parse time either as a humantime compatible string or as a number, falling back to a default time unit. Comes in handy for passing a value_parser function when using Clap derive macros.

Supports the following convenience wrappers:

  • parse_duration_fallback_us: falls back to Microseconds
  • parse_duration_fallback_ns: falls back to Nanoseconds
  • parse_duration_fallback_ms: falls back to Milliseconds
  • parse_duration_fallback_sec: falls back to Seconds
  • parse_duration_fallback_min: falls back to Minutes
  • parse_duration_fallback_hour: falls back to Hours
  • parse_duration_fallback_day: falls back to Days

Examples

  • using a duration string:
    let duration_string = "42h";
    match parse_duration_fallback_sec(duration_string) {
        Ok(duration) => {
            println!("{:?}", duration);
        }
        Err(e) => {
            println!("{:?}", e)
        }
    }
  • using a number:
    let seconds = "1_000_000";
    match parse_duration_fallback_sec(seconds) {
        Ok(duration) => {
            println!("{:?}", duration);
        }
        Err(e) => {
            println!("{:?}", e)
        }
    }
  • in a Clap macro:
    #[clap(long = "duration-argument", default_value = "100ms", value_parser = parse_duration_fallback_ms)]
    duration_argument: Duration
    and you can now use ./cli --duration-argument 10ms or ./cli --duration-argument 10

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.