Skip to content

Thin wrapper over humantime library providing convenience methods to parse durations with fallback units. Useful in Clap derive macros.

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

omarjatoi/parse-duration-with-fallbacks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Thin wrapper over humantime library providing convenience methods to parse durations with fallback units. Useful in Clap derive macros.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages