Struct Config

Source
#[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
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§default_unit: TimeUnit

The 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: Multiplier

The 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: bool

Disable 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: bool

Disable 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: bool

Disable 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: bool

Make 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: bool

If 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: bool

Allow 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: Delimiter

This delimiter may occur within a duration

The occurrences of this delimiter depend on other configuration settings:

  • If allow_sign_delimiter is set, this delimiter may separate the sign from the number
  • If allow_time_unit_delimiter is set, this delimiter may separate the number from the time unit
  • If allow_ago is set, this delimiter has to separate the time unit from the ago keyword

If parsing with NumbersLike numerals, then this delimiter has to separate the numeral from the time unit.

§outer_delimiter: Delimiter

This delimiter may occur to separate multiple durations and Config::conjunctions

This delimiter is used only if allow_multiple is set.

§allow_ago: bool

Allow 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: bool

Allow 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: bool

Allow 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>

Source

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);
Source

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);

Trait Implementations§

Source§

impl<'a> Clone for Config<'a>

Source§

fn clone(&self) -> Config<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Config<'a>

Source§

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

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

impl<'a> Default for Config<'a>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'a> PartialEq for Config<'a>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> Eq for Config<'a>

Source§

impl<'a> StructuralPartialEq for Config<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Config<'a>

§

impl<'a> RefUnwindSafe for Config<'a>

§

impl<'a> Send for Config<'a>

§

impl<'a> Sync for Config<'a>

§

impl<'a> Unpin for Config<'a>

§

impl<'a> UnwindSafe for Config<'a>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.