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

Like crate::DurationParserBuilder for crate::DurationParser, this is a builder for a CustomDurationParser.

Examples

use std::time::Duration;

use fundu::TimeUnit::*;
use fundu::{CustomDurationParser, CustomDurationParserBuilder};

let parser = CustomDurationParserBuilder::new()
    .time_units(&[(NanoSecond, &["ns"])])
    .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 = CustomDurationParser::with_time_units(&[(NanoSecond, &["ns"])]);
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> CustomDurationParserBuilder<'a>

source

pub const fn new() -> Self

Construct a new CustomDurationParserBuilder.

Per default there are no time units configured in the builder. Use CustomDurationParserBuilder::time_units to add time units.

Unlike its counterpart crate::DurationParserBuilder, this builder is not reusable and CustomDurationParserBuilder::build consumes this builder. This is due to the more complicated structure of custom time units and to keep the building process as performant as possible.

Examples
use fundu::{CustomDurationParser, CustomDurationParserBuilder};

assert_eq!(
    CustomDurationParserBuilder::new().build(),
    CustomDurationParser::new()
);
source

pub const fn time_units(self, time_units: &'a [Identifiers<'a>]) -> Self

Let’s the CustomDurationParserBuilder build the CustomDurationParser with a set of time units.

Examples
use fundu::TimeUnit::*;
use fundu::{CustomDurationParserBuilder, Multiplier};

let parser = CustomDurationParserBuilder::new()
    .time_units(&[
        (NanoSecond, &["ns"]),
        (Second, &["s", "sec", "secs"]),
        (Year, &["year"]),
    ])
    .build();

assert_eq!(
    parser.get_time_unit_by_id("ns"),
    Some((NanoSecond, Multiplier(1, 0)))
);
assert_eq!(
    parser.get_time_unit_by_id("s"),
    Some((Second, Multiplier(1, 0)))
);
assert_eq!(
    parser.get_time_unit_by_id("year"),
    Some((Year, Multiplier(1, 0)))
);
source

pub fn custom_time_unit(self, time_unit: CustomTimeUnit<'a>) -> Self

Add a custom time unit to the current set of TimeUnits.

See also CustomDurationParser.

Examples
use std::time::Duration;

use fundu::TimeUnit::*;
use fundu::{CustomDurationParserBuilder, CustomTimeUnit, Multiplier};

let parser = CustomDurationParserBuilder::new()
    .custom_time_unit(CustomTimeUnit::new(
        Week,
        &["fortnight", "fortnights"],
        Some(Multiplier(2, 0)),
    ))
    .build();
assert_eq!(
    parser.parse("1fortnight").unwrap(),
    Duration::new(2 * 7 * 24 * 60 * 60, 0),
);
source

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

Add multiple CustomTimeUnits at once.

See also CustomDurationParser::custom_time_units

Example
use std::time::Duration;

use fundu::TimeUnit::*;
use fundu::{CustomDurationParserBuilder, CustomTimeUnit, Multiplier};

const CUSTOM_TIME_UNITS: [CustomTimeUnit; 2] = [
    CustomTimeUnit::new(Week, &["fortnight", "fortnights"], Some(Multiplier(2, 0))),
    CustomTimeUnit::new(Second, &["shake", "shakes"], Some(Multiplier(1, -8))),
];

let parser = CustomDurationParserBuilder::new()
    .custom_time_units(&CUSTOM_TIME_UNITS)
    .build();

assert_eq!(
    parser.parse("1fortnight").unwrap(),
    Duration::new(2 * 7 * 24 * 60 * 60, 0),
);
source

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

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

See also crate::DurationParser::default_unit

Examples
use std::time::Duration;

use fundu::CustomDurationParserBuilder;
use fundu::TimeUnit::*;

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

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

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

See also crate::DurationParser::allow_delimiter.

Examples
use std::time::Duration;

use fundu::CustomDurationParserBuilder;
use fundu::TimeUnit::*;

let parser = CustomDurationParserBuilder::new()
    .time_units(&[(NanoSecond, &["ns"])])
    .allow_delimiter(|byte| byte == b' ')
    .build();

assert_eq!(parser.parse("123 ns"), Ok(Duration::new(0, 123)));
assert_eq!(parser.parse("123 "), Ok(Duration::new(123, 0)));
source

pub const fn disable_exponent(self) -> Self

Disable parsing an exponent.

See also crate::DurationParser::disable_exponent.

Examples
use fundu::{CustomDurationParserBuilder, ParseError};

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

pub const fn disable_fraction(self) -> Self

Disable parsing a fraction in the source string.

See also crate::DurationParser::disable_fraction.

Examples
use std::time::Duration;

use fundu::TimeUnit::*;
use fundu::{CustomDurationParserBuilder, ParseError};

let parser = CustomDurationParserBuilder::new()
    .time_units(&[(NanoSecond, &["ns"])])
    .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 const fn disable_infinity(self) -> Self

Disable parsing infinity values

See also crate::DurationParser::disable_infinity

Examples
use fundu::{CustomDurationParserBuilder, ParseError};

let parser = CustomDurationParserBuilder::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 const fn number_is_optional(self) -> Self

This setting makes a number in the source string optional.

See also crate::DurationParser::number_is_optional.

Examples
use std::time::Duration;

use fundu::{CustomDurationParserBuilder, DEFAULT_TIME_UNITS};

let parser = CustomDurationParserBuilder::new()
    .time_units(&DEFAULT_TIME_UNITS)
    .number_is_optional()
    .build();

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

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

Parse possibly multiple durations and sum them up.

See also crate::DurationParser::parse_multiple.

Examples
use std::time::Duration;

use fundu::{CustomDurationParserBuilder, DEFAULT_TIME_UNITS};

let parser = CustomDurationParserBuilder::new()
    .time_units(&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(self) -> CustomDurationParser<'a>

Finally, build the CustomDurationParser from this builder.

Note this method is meant as a one-off builder method and can therefore only be used once on each CustomDurationParserBuilder. However, the parser built with this method can be used multiple times.

Examples
use std::time::Duration;

use fundu::CustomDurationParserBuilder;
use fundu::TimeUnit::*;

let parser = CustomDurationParserBuilder::new()
    .time_units(&[(Minute, &["min"]), (Hour, &["h", "hr"])])
    .allow_delimiter(|byte| matches!(byte, b'\t' | b'\n' | b'\r' | b' '))
    .build();

for input in &["60 min", "1h", "1\t\n hr"] {
    assert_eq!(parser.parse(input).unwrap(), Duration::new(60 * 60, 0));
}

Trait Implementations§

source§

impl<'a> Debug for CustomDurationParserBuilder<'a>

source§

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

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

impl<'a> Default for CustomDurationParserBuilder<'a>

source§

fn default() -> Self

Construct a new CustomDurationParserBuilder without any time units.

source§

impl<'a> PartialEq<CustomDurationParserBuilder<'a>> for CustomDurationParserBuilder<'a>

source§

fn eq(&self, other: &CustomDurationParserBuilder<'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 CustomDurationParserBuilder<'a>

source§

impl<'a> StructuralEq for CustomDurationParserBuilder<'a>

source§

impl<'a> StructuralPartialEq for CustomDurationParserBuilder<'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.