Struct ixdtf::parsers::IsoDurationParser

source ·
pub struct IsoDurationParser<'a> { /* private fields */ }
Expand description

A parser for ISO8601 Duration strings.

Enabled with the duration Cargo feature.

§Example

use ixdtf::parsers::{IsoDurationParser, records::{Sign, DurationParseRecord, TimeDurationRecord}};

let duration_str = "P1Y2M1DT2H10M30S";

let result = IsoDurationParser::from_str(duration_str).parse().unwrap();

let date_duration = result.date.unwrap();

let (hours, minutes, seconds, fraction) = match result.time {
    // Hours variant is defined as { hours: u32, fraction: u64 }
    Some(TimeDurationRecord::Hours{ hours, fraction }) => (hours, 0, 0, fraction),
    // Minutes variant is defined as { hours: u32, minutes: u32, fraction: u64 }
    Some(TimeDurationRecord::Minutes{ hours, minutes, fraction }) => (hours, minutes, 0, fraction),
    // Seconds variant is defined as { hours: u32, minutes: u32, seconds: u32, fraction: u32 }
    Some(TimeDurationRecord::Seconds{ hours, minutes, seconds, fraction }) => (hours, minutes, seconds, fraction as u64),
    None => (0,0,0,0),
};

assert_eq!(result.sign, Sign::Positive);
assert_eq!(date_duration.years, 1);
assert_eq!(date_duration.months, 2);
assert_eq!(date_duration.weeks, 0);
assert_eq!(date_duration.days, 1);//
assert_eq!(hours, 2);
assert_eq!(minutes, 10);
assert_eq!(seconds, 30);
assert_eq!(fraction, 0);

Implementations§

source§

impl<'a> IsoDurationParser<'a>

source

pub fn from_utf8(source: &'a [u8]) -> Self

Creates a new IsoDurationParser from a slice of utf-8 bytes.

source

pub fn from_str(source: &'a str) -> Self

Creates a new IsoDurationParser from a source &str.

source

pub fn parse(&mut self) -> ParserResult<DurationParseRecord>

Parse the contents of this IsoDurationParser into a DurationParseRecord.

§Examples
§Parsing a date duration
let date_duration = "P1Y2M3W1D";

let result = IsoDurationParser::from_str(date_duration).parse().unwrap();

let date_duration = result.date.unwrap();

assert!(result.time.is_none());
assert_eq!(date_duration.years, 1);
assert_eq!(date_duration.months, 2);
assert_eq!(date_duration.weeks, 3);
assert_eq!(date_duration.days, 1);
§Parsing a time duration
let time_duration = "PT2H10M30S";

let result = IsoDurationParser::from_str(time_duration).parse().unwrap();

let (hours, minutes, seconds, fraction) = match result.time {
    // Hours variant is defined as { hours: u32, fraction: u64 }
    Some(TimeDurationRecord::Hours{ hours, fraction }) => (hours, 0, 0, fraction),
    // Minutes variant is defined as { hours: u32, minutes: u32, fraction: u64 }
    Some(TimeDurationRecord::Minutes{ hours, minutes, fraction }) => (hours, minutes, 0, fraction),
    // Seconds variant is defined as { hours: u32, minutes: u32, seconds: u32, fraction: u32 }
    Some(TimeDurationRecord::Seconds{ hours, minutes, seconds, fraction }) => (hours, minutes, seconds, fraction as u64),
    None => (0,0,0,0),
};
assert!(result.date.is_none());
assert_eq!(hours, 2);
assert_eq!(minutes, 10);
assert_eq!(seconds, 30);
assert_eq!(fraction, 0);

Trait Implementations§

source§

impl<'a> Debug for IsoDurationParser<'a>

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for IsoDurationParser<'a>

§

impl<'a> RefUnwindSafe for IsoDurationParser<'a>

§

impl<'a> Send for IsoDurationParser<'a>

§

impl<'a> Sync for IsoDurationParser<'a>

§

impl<'a> Unpin for IsoDurationParser<'a>

§

impl<'a> UnwindSafe for IsoDurationParser<'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> 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, 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.