1//! Parsing for various types.
23pub(crate) mod combinator;
4pub(crate) mod component;
5mod iso8601;
6pub(crate) mod parsable;
7mod parsed;
8pub(crate) mod shim;
910pub use self::parsable::Parsable;
11pub use self::parsed::Parsed;
1213/// 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);
1617impl<'a, T> ParsedItem<'a, T> {
18/// Map the value to a new value, preserving the remaining input.
19pub(crate) fn map<U>(self, f: impl FnOnce(T) -> U) -> ParsedItem<'a, U> {
20 ParsedItem(self.0, f(self.1))
21 }
2223/// Map the value to a new, optional value, preserving the remaining input.
24pub(crate) fn flat_map<U>(self, f: impl FnOnce(T) -> Option<U>) -> Option<ParsedItem<'a, U>> {
25Some(ParsedItem(self.0, f(self.1)?))
26 }
2728/// Consume the stored value with the provided function. The remaining input is returned.
29#[must_use = "this returns the remaining input"]
30pub(crate) fn consume_value(self, f: impl FnOnce(T) -> Option<()>) -> Option<&'a [u8]> {
31 f(self.1)?;
32Some(self.0)
33 }
3435/// Filter the value with the provided function. If the function returns `false`, the value
36 /// is discarded and `None` is returned. Otherwise, the value is preserved and `Some(self)` is
37 /// returned.
38pub(crate) fn filter(self, f: impl FnOnce(&T) -> bool) -> Option<Self> {
39 f(&self.1).then_some(self)
40 }
41}
4243impl<'a> ParsedItem<'a, ()> {
44/// Discard the unit value, returning the remaining input.
45#[must_use = "this returns the remaining input"]
46pub(crate) const fn into_inner(self) -> &'a [u8] {
47self.0
48}
49}
5051impl<'a> ParsedItem<'a, Option<()>> {
52/// Discard the potential unit value, returning the remaining input.
53#[must_use = "this returns the remaining input"]
54pub(crate) const fn into_inner(self) -> &'a [u8] {
55self.0
56}
57}