Struct TimeSpanParser

Source
pub struct TimeSpanParser<'a> { /* private fields */ }
Expand description

The main systemd time span parser

Note this parser can be created as const at compile time.

§Examples

use fundu::Duration;
use fundu_systemd::{TimeSpanParser, SYSTEMD_MAX_MICRO_DURATION};

const PARSER: TimeSpanParser = TimeSpanParser::new();

let parser = &PARSER;
assert_eq!(parser.parse("2h"), Ok(Duration::positive(2 * 60 * 60, 0)));
assert_eq!(
    parser.parse("2hours"),
    Ok(Duration::positive(2 * 60 * 60, 0))
);
assert_eq!(parser.parse("second"), Ok(Duration::positive(1, 0)));
assert_eq!(
    parser.parse("48hr"),
    Ok(Duration::positive(48 * 60 * 60, 0))
);
assert_eq!(
    parser.parse("12.3 seconds"),
    Ok(Duration::positive(12, 300_000_000))
);
assert_eq!(
    parser.parse("1y 12month"),
    Ok(Duration::positive(63_115_200, 0))
);
assert_eq!(
    parser.parse("999us +1d"),
    Ok(Duration::positive(86_400, 999_000))
);
assert_eq!(
    parser.parse("55s500ms"),
    Ok(Duration::positive(55, 500_000_000))
);
assert_eq!(
    parser.parse("300ms20s 5day"),
    Ok(Duration::positive(20 + 5 * 60 * 60 * 24, 300_000_000))
);
assert_eq!(
    parser.parse("123456789"),
    Ok(Duration::positive(123_456_789, 0))
);
assert_eq!(parser.parse("infinity"), Ok(SYSTEMD_MAX_MICRO_DURATION));

It’s possible to change the default unit to something different than Second either during the initialization with TimeSpanParser::with_default_unit or at runtime with TimeSpanParser::set_default_unit

use fundu::{Duration, TimeUnit};
use fundu_systemd::TimeSpanParser;

let parser = TimeSpanParser::with_default_unit(TimeUnit::MicroSecond);
assert_eq!(parser.parse("100"), Ok(Duration::positive(0, 100_000)));

let mut parser = TimeSpanParser::new();
parser.set_default_unit(TimeUnit::MicroSecond);

assert_eq!(parser.parse("100"), Ok(Duration::positive(0, 100_000)));

Implementations§

Source§

impl<'a> TimeSpanParser<'a>

Source

pub const fn new() -> Self

Create a new TimeSpanParser with TimeUnit::Second as default unit

§Examples
use fundu::Duration;
use fundu_systemd::TimeSpanParser;

let parser = TimeSpanParser::new();
assert_eq!(parser.parse("2h"), Ok(Duration::positive(2 * 60 * 60, 0)));
assert_eq!(parser.parse("123"), Ok(Duration::positive(123, 0)));
assert_eq!(
    parser.parse("3us +10sec"),
    Ok(Duration::positive(10, 3_000))
);
Source

pub const fn with_default_unit(time_unit: TimeUnit) -> Self

Create a new TimeSpanParser with the specified TimeUnit as default

§Examples
use fundu::{Duration, TimeUnit};
use fundu_systemd::TimeSpanParser;

let parser = TimeSpanParser::with_default_unit(TimeUnit::MicroSecond);
assert_eq!(parser.parse("123"), Ok(Duration::positive(0, 123_000)));
assert_eq!(
    parser.parse("3us +10sec"),
    Ok(Duration::positive(10, 3_000))
);
Source

pub fn parse(&self, source: &str) -> Result<Duration, ParseError>

Parse the source string into a Duration

This method does not include the time units for nano seconds unlike the TimeSpanParser::parse_nanos method. The parser saturates at the maximum Duration of u64::MAX micro seconds. If you need a different maximum use the TimeSpanParser::parse_with_max method.

§Errors

Returns a ParseError if an error during the parsing process occurred

§Examples
use fundu::Duration;
use fundu_systemd::{TimeSpanParser, SYSTEMD_MAX_MICRO_DURATION};

let parser = TimeSpanParser::new();
assert_eq!(
    parser.parse("2hours"),
    Ok(Duration::positive(2 * 60 * 60, 0))
);
assert_eq!(
    parser.parse("12.3 seconds"),
    Ok(Duration::positive(12, 300_000_000))
);
assert_eq!(
    parser.parse("100000000000000000000000000000years"),
    Ok(SYSTEMD_MAX_MICRO_DURATION)
);
assert_eq!(
    parser.parse("1y 12month"),
    Ok(Duration::positive(63_115_200, 0))
);
assert_eq!(
    parser.parse("123456789"),
    Ok(Duration::positive(123_456_789, 0))
);
assert_eq!(parser.parse("infinity"), Ok(SYSTEMD_MAX_MICRO_DURATION));
Source

pub fn parse_with_max( &self, source: &str, max: Duration, ) -> Result<Duration, ParseError>

Parse the source string into a Duration saturating at the given max Duration

This method does not include the time units for nano seconds unlike the TimeSpanParser::parse_nanos_with_max method

§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;
use fundu_systemd::TimeSpanParser;

let parser = TimeSpanParser::new();
assert_eq!(
    parser.parse_with_max("100000000000000000000000000000years", Duration::MAX),
    Ok(Duration::MAX)
);
assert_eq!(
    parser.parse_with_max("123 sec", Duration::positive(1, 0)),
    Ok(Duration::positive(1, 0))
);
assert_eq!(
    parser.parse_with_max("infinity", Duration::positive(i64::MAX as u64, 123)),
    Ok(Duration::positive(i64::MAX as u64, 123))
);
Source

pub fn parse_nanos(&self, source: &str) -> Result<Duration, ParseError>

Parse the source string into a Duration

This method does include the time units for nano seconds unlike the TimeSpanParser::parse method. The parser saturates at the maximum Duration of u64::MAX nano seconds. If you need a different maximum use the TimeSpanParser::parse_nanos_with_max method.

§Errors

Returns a ParseError if an error during the parsing process occurred

§Examples
use fundu::Duration;
use fundu_systemd::{TimeSpanParser, SYSTEMD_MAX_NANOS_DURATION};

let parser = TimeSpanParser::new();
assert_eq!(
    parser.parse_nanos("2hours"),
    Ok(Duration::positive(2 * 60 * 60, 0))
);
assert_eq!(
    parser.parse_nanos("12.3 seconds"),
    Ok(Duration::positive(12, 300_000_000))
);
assert_eq!(
    parser.parse_nanos("100000000000000000000000000000years"),
    Ok(SYSTEMD_MAX_NANOS_DURATION)
);
assert_eq!(
    parser.parse_nanos("1y 12month"),
    Ok(Duration::positive(63_115_200, 0))
);
assert_eq!(
    parser.parse_nanos("123456789"),
    Ok(Duration::positive(123_456_789, 0))
);
assert_eq!(
    parser.parse_nanos("infinity"),
    Ok(SYSTEMD_MAX_NANOS_DURATION)
);
Source

pub fn parse_nanos_with_max( &self, source: &str, max: Duration, ) -> Result<Duration, ParseError>

Parse the source string into a Duration saturating at the given max Duration

This method does include the time units for nano seconds unlike the TimeSpanParser::parse_with_max method

§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;
use fundu_systemd::TimeSpanParser;

let parser = TimeSpanParser::new();
assert_eq!(
    parser.parse_nanos_with_max("100000000000000000000000000000years", Duration::MAX),
    Ok(Duration::MAX)
);
assert_eq!(
    parser.parse_nanos_with_max("1234567890 nsec", Duration::positive(1, 0)),
    Ok(Duration::positive(1, 0))
);
assert_eq!(
    parser.parse_nanos_with_max("infinity", Duration::positive(i64::MAX as u64, 123)),
    Ok(Duration::positive(i64::MAX as u64, 123))
);
Source

pub fn set_default_unit(&mut self, time_unit: TimeUnit)

Set the default TimeUnit during runtime

The default unit is applied to numbers without time units

§Examples
use fundu::{Duration, TimeUnit};
use fundu_systemd::TimeSpanParser;

let mut parser = TimeSpanParser::with_default_unit(TimeUnit::MicroSecond);
assert_eq!(parser.parse("100"), Ok(Duration::positive(0, 100_000)));

parser.set_default_unit(TimeUnit::Second);
assert_eq!(parser.parse("100"), Ok(Duration::positive(100, 0)));

let mut parser = TimeSpanParser::new();
assert_eq!(parser.parse("123456"), Ok(Duration::positive(123456, 0)));

parser.set_default_unit(TimeUnit::MicroSecond);
assert_eq!(
    parser.parse("123456"),
    Ok(Duration::positive(0, 123_456_000))
);

Trait Implementations§

Source§

impl<'a> Debug for TimeSpanParser<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Default for TimeSpanParser<'a>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'a> PartialEq for TimeSpanParser<'a>

Source§

fn eq(&self, other: &TimeSpanParser<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> Eq for TimeSpanParser<'a>

Source§

impl<'a> StructuralPartialEq for TimeSpanParser<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for TimeSpanParser<'a>

§

impl<'a> RefUnwindSafe for TimeSpanParser<'a>

§

impl<'a> Send for TimeSpanParser<'a>

§

impl<'a> Sync for TimeSpanParser<'a>

§

impl<'a> Unpin for TimeSpanParser<'a>

§

impl<'a> UnwindSafe for TimeSpanParser<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.