pub struct DurationParserBuilder<'a> { /* private fields */ }
Available on crate feature standard only.
Expand description

An ergonomic builder for a DurationParser.

The DurationParserBuilder is more ergonomic in some use cases than using DurationParser directly, especially when using the DurationParser for parsing multiple inputs.

Examples

use std::time::Duration;

use fundu::TimeUnit::*;
use fundu::{DurationParser, DurationParserBuilder};

let parser = DurationParserBuilder::new()
    .all_time_units()
    .default_unit(MicroSecond)
    .allow_delimiter(|byte| byte == b' ')
    .build();

assert_eq!(parser.parse("1   ns").unwrap(), Duration::new(0, 1));
assert_eq!(parser.parse("1").unwrap(), Duration::new(0, 1_000));

// instead of

let mut parser = DurationParser::with_all_time_units();
parser
    .default_unit(MicroSecond)
    .allow_delimiter(Some(|byte| byte == b' '));

assert_eq!(parser.parse("1    ns").unwrap(), Duration::new(0, 1));
assert_eq!(parser.parse("1").unwrap(), Duration::new(0, 1_000));

Implementations§

source§

impl<'a> DurationParserBuilder<'a>

source

pub const fn new() -> Self

Construct a new reusable DurationParserBuilder.

This method is the same like invoking DurationParserBuilder::default. Per default there are no time units configured in the builder. Use one of

to add time units.

Examples
use fundu::{DurationParser, DurationParserBuilder};

assert_eq!(
    DurationParserBuilder::new().build(),
    DurationParser::without_time_units()
);
source

pub fn default_time_units(&mut self) -> &mut Self

Configure DurationParserBuilder to build the DurationParser with default time units.

Setting the time units with this method overwrites any previously made choices with

The default time units with their identifiers are:

TimeUnitdefault id
Nanosecondns
MicrosecondMs
Millisecondms
Seconds
Minutem
Hourh
Dayd
Weekw
Examples
use fundu::DurationParserBuilder;
use fundu::TimeUnit::*;

assert_eq!(
    DurationParserBuilder::new()
        .default_time_units()
        .build()
        .get_current_time_units(),
    vec![
        NanoSecond,
        MicroSecond,
        MilliSecond,
        Second,
        Minute,
        Hour,
        Day,
        Week
    ]
);
source

pub fn all_time_units(&mut self) -> &mut Self

Configure DurationParserBuilder to build the DurationParser with all time units.

Setting the time units with this method overwrites any previously made choices with

The time units with their identifiers are:

TimeUnitdefault id
Nanosecondns
MicrosecondMs
Millisecondms
Seconds
Minutem
Hourh
Dayd
Weekw
MonthM
Yeary
Examples
use fundu::DurationParserBuilder;
use fundu::TimeUnit::*;

assert_eq!(
    DurationParserBuilder::new()
        .all_time_units()
        .build()
        .get_current_time_units(),
    vec![
        NanoSecond,
        MicroSecond,
        MilliSecond,
        Second,
        Minute,
        Hour,
        Day,
        Week,
        Month,
        Year
    ]
);
source

pub fn custom_time_units(&mut self, time_units: &'a [TimeUnit]) -> &mut Self

Configure the DurationParserBuilder to build the DurationParser with a custom set of time units.

Setting the time units with this method overwrites any previously made choices with

Examples
use fundu::DurationParserBuilder;
use fundu::TimeUnit::*;

assert_eq!(
    DurationParserBuilder::new()
        .custom_time_units(&[NanoSecond, Second, Year])
        .build()
        .get_current_time_units(),
    vec![NanoSecond, Second, Year]
);
source

pub fn default_unit(&mut self, unit: TimeUnit) -> &mut Self

Set the default time unit to something different than TimeUnit::Second

See also DurationParser::default_unit

Examples
use std::time::Duration;

use fundu::DurationParserBuilder;
use fundu::TimeUnit::*;

assert_eq!(
    DurationParserBuilder::new()
        .all_time_units()
        .default_unit(NanoSecond)
        .build()
        .parse("42")
        .unwrap(),
    Duration::new(0, 42)
);
source

pub fn allow_delimiter(&mut self, delimiter: Delimiter) -> &mut Self

Allow one or more delimiters between the number and the TimeUnit.

See also DurationParser::allow_delimiter.

Examples
use std::time::Duration;

use fundu::DurationParserBuilder;

let parser = DurationParserBuilder::new()
    .default_time_units()
    .allow_delimiter(|byte| byte.is_ascii_whitespace())
    .build();

assert_eq!(parser.parse("123 \t\n\x0C\rns"), Ok(Duration::new(0, 123)));
assert_eq!(parser.parse("123\n"), Ok(Duration::new(123, 0)));
source

pub fn disable_exponent(&mut self) -> &mut Self

Disable parsing an exponent.

See also DurationParser::disable_exponent.

Examples
use fundu::{DurationParserBuilder, ParseError};

assert_eq!(
    DurationParserBuilder::new()
        .default_time_units()
        .disable_exponent()
        .build()
        .parse("123e+1"),
    Err(ParseError::Syntax(3, "No exponent allowed".to_string()))
);
source

pub fn disable_fraction(&mut self) -> &mut Self

Disable parsing a fraction in the source string.

See also DurationParser::disable_fraction.

Examples
use std::time::Duration;

use fundu::{DurationParserBuilder, ParseError};

let parser = DurationParserBuilder::new()
    .default_time_units()
    .disable_fraction()
    .build();

assert_eq!(
    parser.parse("123.456"),
    Err(ParseError::Syntax(3, "No fraction allowed".to_string()))
);

assert_eq!(parser.parse("123e-2"), Ok(Duration::new(1, 230_000_000)));
assert_eq!(parser.parse("123ns"), Ok(Duration::new(0, 123)));
source

pub fn disable_infinity(&mut self) -> &mut Self

Disable parsing infinity values

See also DurationParser::disable_infinity.

Examples
use fundu::{DurationParserBuilder, ParseError};

let parser = DurationParserBuilder::new().disable_infinity().build();

assert_eq!(
    parser.parse("inf"),
    Err(ParseError::Syntax(0, format!("Invalid input: 'inf'")))
);
assert_eq!(
    parser.parse("infinity"),
    Err(ParseError::Syntax(0, format!("Invalid input: 'infinity'")))
);
assert_eq!(
    parser.parse("+inf"),
    Err(ParseError::Syntax(1, format!("Invalid input: 'inf'")))
);
source

pub fn number_is_optional(&mut self) -> &mut Self

This setting makes a number in the source string optional.

See also DurationParser::number_is_optional.

Examples
use std::time::Duration;

use fundu::DurationParserBuilder;

let parser = DurationParserBuilder::new()
    .default_time_units()
    .number_is_optional()
    .build();

for input in &["ns", "e-9", "e-3Ms"] {
    assert_eq!(parser.parse(input), Ok(Duration::new(0, 1)));
}
source

pub fn parse_multiple(&mut self, delimiter: Delimiter) -> &mut Self

Parse possibly multiple durations and sum them up.

See also DurationParser::parse_multiple.

Examples
use std::time::Duration;

use fundu::DurationParserBuilder;

let parser = DurationParserBuilder::new()
    .default_time_units()
    .parse_multiple(|byte| matches!(byte, b' ' | b'\t'))
    .build();

assert_eq!(parser.parse("1.5h 2e+2ns"), Ok(Duration::new(5400, 200)));
assert_eq!(parser.parse("55s500ms"), Ok(Duration::new(55, 500_000_000)));
assert_eq!(parser.parse("1\t1"), Ok(Duration::new(2, 0)));
assert_eq!(parser.parse("1.   .1"), Ok(Duration::new(1, 100_000_000)));
assert_eq!(parser.parse("2h"), Ok(Duration::new(2 * 60 * 60, 0)));
assert_eq!(
    parser.parse("300ms20s 5d"),
    Ok(Duration::new(5 * 60 * 60 * 24 + 20, 300_000_000))
);
source

pub fn build(&mut self) -> DurationParser

Finally, build the DurationParser from this builder.

Examples
use std::time::Duration;

use fundu::DurationParserBuilder;

let parser = DurationParserBuilder::new().default_time_units().build();
for input in &["1m", "60s"] {
    assert_eq!(parser.parse(input).unwrap(), Duration::new(60, 0))
}

Trait Implementations§

source§

impl<'a> Debug for DurationParserBuilder<'a>

source§

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

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

impl<'a> Default for DurationParserBuilder<'a>

source§

fn default() -> Self

Construct a new DurationParserBuilder without any time units.

source§

impl<'a> PartialEq<DurationParserBuilder<'a>> for DurationParserBuilder<'a>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> Eq for DurationParserBuilder<'a>

source§

impl<'a> StructuralEq for DurationParserBuilder<'a>

source§

impl<'a> StructuralPartialEq for DurationParserBuilder<'a>

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.