#[non_exhaustive]pub struct Config<'a> {Show 14 fields
pub default_unit: TimeUnit,
pub default_multiplier: Multiplier,
pub disable_exponent: bool,
pub disable_fraction: bool,
pub disable_infinity: bool,
pub number_is_optional: bool,
pub allow_multiple: bool,
pub conjunctions: Option<&'a [&'a str]>,
pub allow_negative: bool,
pub inner_delimiter: Delimiter,
pub outer_delimiter: Delimiter,
pub allow_ago: bool,
pub allow_sign_delimiter: bool,
pub allow_time_unit_delimiter: bool,
}Expand description
The structure containing all options for the crate::parse::Parser
This struct is highly likely to change often, so it is not possible to create the Config with
Config { ... } outside of this crate. Instead, use Config::new, Config::default or the
ConfigBuilder to create a new Config.
§Examples
Here’s a little bit more involved example to see the effects of the configuration in action. To
keep the example small, crate::time::TimeUnitsLike is implemented in such a way that no time
units are allowed in the input string. The final Config uses TimeUnit::MilliSecond as
default time unit instead of TimeUnit::Second and allows negative durations.
use fundu_core::config::{Config, ConfigBuilder};
use fundu_core::parse::Parser;
use fundu_core::time::{Duration, Multiplier, TimeUnit, TimeUnitsLike};
struct TimeUnits {}
impl TimeUnitsLike for TimeUnits {
fn is_empty(&self) -> bool {
true
}
fn get(&self, string: &str) -> Option<(TimeUnit, Multiplier)> {
None
}
}
const TIME_UNITS: TimeUnits = TimeUnits {};
const CONFIG: Config = ConfigBuilder::new()
.default_unit(TimeUnit::MilliSecond)
.allow_negative()
.build();
const PARSER: Parser = Parser::with_config(CONFIG);
assert_eq!(
PARSER.parse("1000", &TIME_UNITS, None, None),
Ok(Duration::positive(1, 0))
);
assert_eq!(
PARSER.parse("-1", &TIME_UNITS, None, None),
Ok(Duration::negative(0, 1_000_000))
);Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.default_unit: TimeUnitThe TimeUnit the parser applies if no time unit was given (Default: TimeUnit::Second)
A configuration with TimeUnit::MilliSecond would parse a string without time units like
"1000" to a crate::time::Duration with Duration::positive(1, 0) which is worth 1
second.
default_multiplier: MultiplierThe default Multiplier used internally to make custom time units possible
This field is only used internally and it is not recommended to change this setting!
disable_exponent: boolDisable parsing an exponent (Default: false)
The exponent in the input string may start with an e or E followed by an optional sign
character and a mandatory number like in "1e+20", "2E-3" … Occurrences of an exponent
in strings like "1e20","10E2", "3.4e-10" lead to a crate::error::ParseError.
disable_fraction: boolDisable parsing a fraction (Default: false)
The fraction in the input string starts with a point character . followed by an optional
number like in "1.234" but also "1." as long as there is a number present before the
point. Occurrences of a fraction like in "1.234" when a fraction is not allowed by this
setting lead to a crate::error::ParseError.
disable_infinity: boolDisable parsing infinity (Default: false)
An infinity in the input string is either "inf" or "infinity" case insensitive
optionally preceded by a sign character. Infinity values evaluate to the maximum possible
duration or if negative to the maximum negative value of the duration.
number_is_optional: boolMake a number in the input string optional (Default: false)
Usually, a time unit needs a number like in "1second". With this setting set to true a
time unit can occur without number like "second" and a number with value 1 is assumed.
allow_multiple: boolIf true, this setting allows multiple durations in the input (Default: false)
Multiple durations may be delimited by the Config::outer_delimiter. A subsequent
duration is also recognized if it starts with a digit or a sign character.
conjunctions: Option<&'a [&'a str]>If parsing multiple durations, allow conjunctions in addition to the Delimiter
(Default: None)
Conjunctions are words like "and", "or" but also single characters like "," or “;”.
So, a string like "3seconds and 1second" would parse to a Duration::positive(4, 0) and
"1second, 2seconds" would parse to a Duration::positive(3, 0). Unlike a Delimiter,
conjunctions can occur only once between two durations.
allow_negative: boolAllow parsing negative durations (Default: false)
Negative durations usually start with a - sign like in -1second which would evaluate to
a Duration::negative(1, 0). But it can also be indicated by the ago keyword if the
allow_ago settings is set to true.
inner_delimiter: DelimiterThis delimiter may occur within a duration
The occurrences of this delimiter depend on other configuration settings:
- If
allow_sign_delimiteris set, this delimiter may separate the sign from the number - If
allow_time_unit_delimiteris set, this delimiter may separate the number from the time unit - If
allow_agois set, this delimiter has to separate the time unit from theagokeyword
If parsing with NumbersLike numerals, then this delimiter has to separate the numeral
from the time unit.
outer_delimiter: DelimiterThis delimiter may occur to separate multiple durations and Config::conjunctions
This delimiter is used only if allow_multiple is set.
allow_ago: boolAllow the ago keyword to indicate a negative duration (Default: false)
The ago keyword must be delimited by the Config::inner_delimiter from the time unit.
IMPORTANT: If allow_ago is set to true it’s also necessary to set allow_negative to
true explicitly.
The ago keyword can only occur in conjunction with a time unit like in "1second ago"
which would result in a Duration::negative(1, 0). "1 ago" and "1ago" would result in
a crate::error::ParseError.
allow_sign_delimiter: boolAllow the Config::inner_delimiter between the sign and a number, time keyword …
(Default: false)
For example, setting the inner_delimiter to |byte| matches!(byte, b' ' | b'\n') would
parse strings like "+1ms", "- 1ms", "+ yesterday", "+\n4e2000years" …
allow_time_unit_delimiter: boolAllow a Delimiter between the number and time unit (Default: false)
This setting does not enforce the Config::inner_delimiter, so time units directly
following the number are still parsed without error. A delimiter may occur multiple times.
For example, setting this option with an inner_delimiter of |byte| matches!(byte, b' ' | b'\n') would parse strings like "1ms", "1 ms", "3.2 minutes", "4e2000 \n years"
…
Implementations§
Source§impl<'a> Config<'a>
impl<'a> Config<'a>
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Create a new default configuration
Please see the documentation of the fields of this struct for more information and their
default values.
§Examples
use fundu_core::config::Config;
use fundu_core::time::{Multiplier, TimeUnit};
const DEFAULT_CONFIG: Config = Config::new();
assert_eq!(DEFAULT_CONFIG.default_unit, TimeUnit::Second);
assert_eq!(DEFAULT_CONFIG.allow_time_unit_delimiter, false);
assert_eq!(DEFAULT_CONFIG.default_multiplier, Multiplier(1, 0));
assert_eq!(DEFAULT_CONFIG.disable_exponent, false);
assert_eq!(DEFAULT_CONFIG.disable_fraction, false);
assert_eq!(DEFAULT_CONFIG.number_is_optional, false);
assert_eq!(DEFAULT_CONFIG.disable_infinity, false);
assert_eq!(DEFAULT_CONFIG.allow_multiple, false);
assert_eq!(DEFAULT_CONFIG.conjunctions, None);
assert_eq!(DEFAULT_CONFIG.allow_negative, false);
assert_eq!(DEFAULT_CONFIG.allow_ago, false);Sourcepub const fn builder() -> ConfigBuilder<'a>
pub const fn builder() -> ConfigBuilder<'a>
Convenience method to use the ConfigBuilder to build this Config
§Examples
use fundu_core::config::{Config, ConfigBuilder};
use fundu_core::time::TimeUnit;
let config = Config::builder()
.disable_infinity()
.allow_negative()
.build();
assert_eq!(config.disable_infinity, true);
assert_eq!(config.allow_negative, true);