Debian Codegraph @

third_party/rust/time/src/parsing/mod.rs

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