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    /// Filter the value with the provided function. If the function returns `false`, the value
39    /// is discarded and `None` is returned. Otherwise, the value is preserved and `Some(self)` is
40    /// returned.
41    #[inline]
42    pub(crate) fn filter(self, f: impl FnOnce(&T) -> bool) -> Option<Self> {
43        f(&self.1).then_some(self)
44    }
45}
46
47impl<'a> ParsedItem<'a, ()> {
48    /// Discard the unit value, returning the remaining input.
49    #[must_use = "this returns the remaining input"]
50    #[inline]
51    pub(crate) const fn into_inner(self) -> &'a [u8] {
52        self.0
53    }
54}
55
56impl<'a> ParsedItem<'a, Option<()>> {
57    /// Discard the potential unit value, returning the remaining input.
58    #[must_use = "this returns the remaining input"]
59    #[inline]
60    pub(crate) const fn into_inner(self) -> &'a [u8] {
61        self.0
62    }
63}