Skip to main content

time/parsing/
mod.rs

1//! Parsing for various types.
2
3pub(crate) mod combinator;
4pub(crate) mod component;
5mod iso8601;
6pub(crate) mod parsable;
7mod parsed;
8pub(crate) mod shim;
9
10pub use self::parsable::Parsable;
11pub use self::parsed::Parsed;
12
13/// An item that has been parsed. Represented as a `(remaining, value)` pair.
14#[derive(Debug)]
15pub(crate) struct ParsedItem<'a, T>(pub(crate) &'a [u8], pub(crate) T);
16
17impl<'a, T> ParsedItem<'a, T> {
18    /// Map the value to a new value, preserving the remaining input.
19    #[inline]
20    pub(crate) fn map<U>(self, f: impl FnOnce(T) -> U) -> ParsedItem<'a, U> {
21        ParsedItem(self.0, f(self.1))
22    }
23
24    /// Map the value to a new, optional value, preserving the remaining input.
25    #[inline]
26    pub(crate) fn flat_map<U>(self, f: impl FnOnce(T) -> Option<U>) -> Option<ParsedItem<'a, U>> {
27        Some(ParsedItem(self.0, f(self.1)?))
28    }
29
30    /// Consume the stored value with the provided function. The remaining input is returned.
31    #[must_use = "this returns the remaining input"]
32    #[inline]
33    pub(crate) fn consume_value(self, f: impl FnOnce(T) -> Option<()>) -> Option<&'a [u8]> {
34        f(self.1)?;
35        Some(self.0)
36    }
37
38    /// Discard the stored value, returning the remaining input.
39    #[must_use = "this returns the remaining input"]
40    #[inline]
41    pub(crate) fn discard_value(self) -> &'a [u8] {
42        self.0
43    }
44
45    /// Filter the value with the provided function. If the function returns `false`, the value
46    /// is discarded and `None` is returned. Otherwise, the value is preserved and `Some(self)` is
47    /// returned.
48    #[inline]
49    pub(crate) fn filter(self, f: impl FnOnce(&T) -> bool) -> Option<Self> {
50        f(&self.1).then_some(self)
51    }
52}
53
54impl<'a> ParsedItem<'a, ()> {
55    /// Discard the unit value, returning the remaining input.
56    #[must_use = "this returns the remaining input"]
57    #[inline]
58    pub(crate) const fn into_inner(self) -> &'a [u8] {
59        self.0
60    }
61}
62
63impl<'a> ParsedItem<'a, Option<()>> {
64    /// Discard the potential unit value, returning the remaining input.
65    #[must_use = "this returns the remaining input"]
66    #[inline]
67    pub(crate) const fn into_inner(self) -> &'a [u8] {
68        self.0
69    }
70}