Function parse_nanos

Source
pub fn parse_nanos(
    source: &str,
    default_unit: Option<TimeUnit>,
    max: Option<Duration>,
) -> Result<Duration, ParseError>
Expand description

Parse the source string into a Duration with nano second time units

This method does include the time units for nano seconds unlike the parse method. The parser saturates at the maximum Duration of u64::MAX nano seconds if not specified otherwise. Optionally, it’s possible to specify a different default time unit than TimeUnit::Second

§Panics

This method panics if max is a a negative Duration.

§Errors

Returns a ParseError if an error during the parsing process occurred

§Examples

use fundu::{Duration, TimeUnit};
use fundu_systemd::{parse_nanos, SYSTEMD_MAX_NANOS_DURATION};

assert_eq!(
    parse_nanos("2nsec", None, None),
    Ok(Duration::positive(0, 2))
);
assert_eq!(
    parse_nanos("1y 12month", None, None),
    Ok(Duration::positive(63_115_200, 0))
);
assert_eq!(
    parse_nanos("12.3", Some(TimeUnit::MilliSecond), None),
    Ok(Duration::positive(0, 12_300_000))
);
assert_eq!(
    parse_nanos("100000000000000000000000000000years", None, None),
    Ok(SYSTEMD_MAX_NANOS_DURATION)
);
assert_eq!(
    parse_nanos(
        "100000000000000000000000000000years",
        None,
        Some(Duration::MAX)
    ),
    Ok(Duration::MAX)
);
assert_eq!(
    parse_nanos("infinity", None, None),
    Ok(SYSTEMD_MAX_NANOS_DURATION)
);