Struct ixdtf::parsers::IxdtfParser

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

IxdtfParser is the primary parser implementation of ixdtf.

This parser provides various options for parsing date/time strings with the extended notation laid out in RFC9557 along with other variations laid out in the Temporal.

use ixdtf::parsers::{
    records::{Sign, TimeZoneRecord, UtcOffsetRecord},
    IxdtfParser,
};

let ixdtf_str = "2024-03-02T08:48:00-05:00[America/New_York]";

let result = IxdtfParser::from_str(ixdtf_str).parse().unwrap();

let date = result.date.unwrap();
let time = result.time.unwrap();
let offset = result.offset.unwrap().resolve_rfc_9557();
let tz_annotation = result.tz.unwrap();

assert_eq!(date.year, 2024);
assert_eq!(date.month, 3);
assert_eq!(date.day, 2);
assert_eq!(time.hour, 8);
assert_eq!(time.minute, 48);
assert_eq!(offset.sign, Sign::Negative);
assert_eq!(offset.hour, 5);
assert_eq!(offset.minute, 0);
assert!(!tz_annotation.critical);
assert_eq!(
    tz_annotation.tz,
    TimeZoneRecord::Name("America/New_York".as_bytes())
);

Implementations§

source§

impl<'a> IxdtfParser<'a>

source

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

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

source

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

Creates a new IxdtfParser from a source &str.

source

pub fn parse(&mut self) -> ParserResult<IxdtfParseRecord<'a>>

Parses the source as an extended Date/Time string.

This is the baseline parse method for ixdtf. For this method, the TimeRecord, UTCOffsetRecord, and all annotations are optional.

§Example
source

pub fn parse_with_annotation_handler( &mut self, handler: impl FnMut(Annotation<'a>) -> Option<Annotation<'a>>, ) -> ParserResult<IxdtfParseRecord<'a>>

Parses the source as an extended Date/Time string with an Annotation handler.

For more, see Implementing Annotation Handlers

source

pub fn parse_year_month(&mut self) -> ParserResult<IxdtfParseRecord<'a>>

Parses the source as an extended YearMonth string.

§Example

let extended_year_month = "2020-11[u-ca=iso8601]";

let result = IxdtfParser::from_str(extended_year_month)
    .parse_year_month()
    .unwrap();

let date = result.date.unwrap();

assert_eq!(date.year, 2020);
assert_eq!(date.month, 11);
source

pub fn parse_year_month_with_annotation_handler( &mut self, handler: impl FnMut(Annotation<'a>) -> Option<Annotation<'a>>, ) -> ParserResult<IxdtfParseRecord<'a>>

Parses the source as an extended YearMonth string with an Annotation handler.

For more, see Implementing Annotation Handlers

source

pub fn parse_month_day(&mut self) -> ParserResult<IxdtfParseRecord<'a>>

Parses the source as an extended MonthDay string.

§Example
let extended_month_day = "1107[+04:00]";

let result = IxdtfParser::from_str(extended_month_day)
    .parse_month_day()
    .unwrap();

let date = result.date.unwrap();

assert_eq!(date.month, 11);
assert_eq!(date.day, 7);
source

pub fn parse_month_day_with_annotation_handler( &mut self, handler: impl FnMut(Annotation<'a>) -> Option<Annotation<'a>>, ) -> ParserResult<IxdtfParseRecord<'a>>

Parses the source as an extended MonthDay string with an Annotation handler.

For more, see Implementing Annotation Handlers

source

pub fn parse_time(&mut self) -> ParserResult<IxdtfParseRecord<'a>>

Parses the source as an extended Time string.

§Example
let extended_time = "12:01:04-05:00[America/New_York][u-ca=iso8601]";

let result = IxdtfParser::from_str(extended_time).parse_time().unwrap();

let time = result.time.unwrap();
let offset = result.offset.unwrap().resolve_rfc_9557();
let tz_annotation = result.tz.unwrap();

assert_eq!(time.hour, 12);
assert_eq!(time.minute, 1);
assert_eq!(time.second, 4);
assert_eq!(offset.sign, Sign::Negative);
assert_eq!(offset.hour, 5);
assert_eq!(offset.minute, 0);
assert!(!tz_annotation.critical);
assert_eq!(
    tz_annotation.tz,
    TimeZoneRecord::Name("America/New_York".as_bytes())
);
source

pub fn parse_time_with_annotation_handler( &mut self, handler: impl FnMut(Annotation<'a>) -> Option<Annotation<'a>>, ) -> ParserResult<IxdtfParseRecord<'a>>

Parses the source as an extended Time string with an Annotation handler.

For more, see Implementing Annotation Handlers

Trait Implementations§

source§

impl<'a> Debug for IxdtfParser<'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 IxdtfParser<'a>

§

impl<'a> RefUnwindSafe for IxdtfParser<'a>

§

impl<'a> Send for IxdtfParser<'a>

§

impl<'a> Sync for IxdtfParser<'a>

§

impl<'a> Unpin for IxdtfParser<'a>

§

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