Struct fundu::DurationParserBuilder
source · pub struct DurationParserBuilder<'a> { /* private fields */ }
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>
impl<'a> DurationParserBuilder<'a>
sourcepub const fn new() -> Self
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
DurationParserBuilder::default_time_units
DurationParserBuilder::all_time_units
DurationParserBuilder::custom_time_units
to add time units.
Examples
use fundu::{DurationParser, DurationParserBuilder};
assert_eq!(
DurationParserBuilder::new().build(),
DurationParser::without_time_units()
);
sourcepub fn default_time_units(&mut self) -> &mut Self
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:
TimeUnit | default id |
---|---|
Nanosecond | ns |
Microsecond | Ms |
Millisecond | ms |
Second | s |
Minute | m |
Hour | h |
Day | d |
Week | w |
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
]
);
sourcepub fn all_time_units(&mut self) -> &mut Self
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:
TimeUnit | default id |
---|---|
Nanosecond | ns |
Microsecond | Ms |
Millisecond | ms |
Second | s |
Minute | m |
Hour | h |
Day | d |
Week | w |
Month | M |
Year | y |
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
]
);
sourcepub fn custom_time_units(&mut self, time_units: &'a [TimeUnit]) -> &mut Self
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]
);
sourcepub fn default_unit(&mut self, unit: TimeUnit) -> &mut Self
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)
);
sourcepub fn allow_delimiter(&mut self, delimiter: Delimiter) -> &mut Self
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)));
sourcepub fn disable_exponent(&mut self) -> &mut Self
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()))
);
sourcepub fn disable_fraction(&mut self) -> &mut Self
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)));
sourcepub fn disable_infinity(&mut self) -> &mut Self
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'")))
);
sourcepub fn number_is_optional(&mut self) -> &mut Self
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)));
}
sourcepub fn parse_multiple(&mut self, delimiter: Delimiter) -> &mut Self
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))
);
sourcepub fn build(&mut self) -> DurationParser
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>
impl<'a> Debug for DurationParserBuilder<'a>
source§impl<'a> Default for DurationParserBuilder<'a>
impl<'a> Default for DurationParserBuilder<'a>
source§fn default() -> Self
fn default() -> Self
Construct a new DurationParserBuilder
without any time units.
source§impl<'a> PartialEq<DurationParserBuilder<'a>> for DurationParserBuilder<'a>
impl<'a> PartialEq<DurationParserBuilder<'a>> for DurationParserBuilder<'a>
source§fn eq(&self, other: &DurationParserBuilder<'a>) -> bool
fn eq(&self, other: &DurationParserBuilder<'a>) -> bool
self
and other
values to be equal, and is used
by ==
.