pub struct Parser<'a> {
pub config: Config<'a>,
}
Expand description
The core duration parser to parse strings into a crate::time::Duration
To be able to use the Parser::parse
method an implementation of the
crate::time::TimeUnitsLike
trait is needed for the time units (even if there are no time
units) and optionally for time keywords (like yesterday
and tomorrow
etc.). Optionally, an
implementation of the NumbersLike
trait can be provided, too. The custom
and standard
features have such implementations and their parsers are more convenient to use than using this
parser directly. However, for example, the custom
feature’s fundu::CustomDurationParser
cannot be fully built in const
context and is a slightly slower than this parser. So, using
this parser is more involved but if maximum performance and building a parser in const
context
is wanted then this parser is the better choice.
§Examples
use fundu_core::error::ParseError;
use fundu_core::parse::Parser;
use fundu_core::time::TimeUnit::*;
use fundu_core::time::{Duration, Multiplier, TimeUnit, TimeUnitsLike};
struct TimeUnits {}
impl TimeUnitsLike for TimeUnits {
#[inline]
fn is_empty(&self) -> bool {
false
}
#[inline]
fn get(&self, identifier: &str) -> Option<(TimeUnit, Multiplier)> {
match identifier {
"s" | "sec" | "secs" => Some((Second, Multiplier(1, 0))),
"m" | "min" | "mins" => Some((Minute, Multiplier(1, 0))),
_ => None,
}
}
}
let parser = Parser::new();
let time_units = TimeUnits {};
assert_eq!(
parser.parse("1.0s", &time_units, None, None),
Ok(Duration::positive(1, 0))
);
assert_eq!(
parser.parse("1min", &time_units, None, None),
Ok(Duration::positive(60, 0))
);
assert_eq!(
parser.parse("1ms", &time_units, None, None),
Err(ParseError::TimeUnit(
1,
"Invalid time unit: 'ms'".to_string()
))
);
Fields§
§config: Config<'a>
The crate::config::Config
of this Parser
For convenience, there are also the const Parser::new
and const Parser::with_config
methods to create a new Parser
.
Implementations§
Source§impl<'a> Parser<'a>
impl<'a> Parser<'a>
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Convenience method to create a new parser with the default crate::config::Config
Sourcepub const fn with_config(config: Config<'a>) -> Self
pub const fn with_config(config: Config<'a>) -> Self
Convenience method to create a new parser with the the given crate::config::Config
pub fn parse_multiple( &self, source: &str, time_units: &dyn TimeUnitsLike, keywords: Option<&dyn TimeUnitsLike>, numerals: Option<&dyn NumbersLike>, ) -> Result<Duration, ParseError>
pub fn parse_single( &self, source: &str, time_units: &dyn TimeUnitsLike, keywords: Option<&dyn TimeUnitsLike>, numerals: Option<&dyn NumbersLike>, ) -> Result<Duration, ParseError>
Sourcepub fn parse(
&self,
source: &str,
time_units: &dyn TimeUnitsLike,
keywords: Option<&dyn TimeUnitsLike>,
numerals: Option<&dyn NumbersLike>,
) -> Result<Duration, ParseError>
pub fn parse( &self, source: &str, time_units: &dyn TimeUnitsLike, keywords: Option<&dyn TimeUnitsLike>, numerals: Option<&dyn NumbersLike>, ) -> Result<Duration, ParseError>
Parse the source
string into a saturating crate::time::Duration
This method needs a struct implementing the crate::time::TimeUnitsLike
for time units
and optionally for time keywords (like yesterday
, tomorrow
). But also NumbersLike
implementations for words like one
, next
, last
are supported. The standard
and
custom
features of fundu
offer such implementations and are more convenient to use than
using this method directly. They both provide facades and an own parser which calls this
method.
§Errors
Returns a crate::error::ParseError
if the given source
string is invalid
§Examples
An example with a quick and dirty implementation of crate::time::TimeUnitsLike
use fundu_core::error::ParseError;
use fundu_core::parse::Parser;
use fundu_core::time::TimeUnit::*;
use fundu_core::time::{Duration, Multiplier, TimeUnit, TimeUnitsLike};
struct TimeUnits {}
impl TimeUnitsLike for TimeUnits {
#[inline]
fn is_empty(&self) -> bool {
false
}
#[inline]
fn get(&self, identifier: &str) -> Option<(TimeUnit, Multiplier)> {
match identifier {
"s" | "sec" | "secs" => Some((Second, Multiplier(1, 0))),
"m" | "min" | "mins" => Some((Minute, Multiplier(1, 0))),
_ => None,
}
}
}
let parser = Parser::new();
let time_units = TimeUnits {};
assert_eq!(
parser.parse("1.0s", &time_units, None, None),
Ok(Duration::positive(1, 0))
);
assert_eq!(
parser.parse("1min", &time_units, None, None),
Ok(Duration::positive(60, 0))
);
assert_eq!(
parser.parse("1ms", &time_units, None, None),
Err(ParseError::TimeUnit(
1,
"Invalid time unit: 'ms'".to_string()
))
);