tor_netdoc/parse2/
impls.rs

1//! Implementations of our useful traits, on external and parsing-mode types
2
3use super::*;
4
5/// Types related to RSA keys
6mod rsa {
7    use super::*;
8    use tor_llcrypto::pk::rsa::PublicKey;
9
10    /// An item which contains an RSA public key as an Object, and no extra arguments
11    #[derive(Deftly)]
12    #[derive_deftly(ItemValueParseable)]
13    #[deftly(netdoc(no_extra_args))]
14    struct ParsePublicKey {
15        /// The public key data
16        #[deftly(netdoc(object))]
17        key: PublicKey,
18    }
19
20    impl ItemObjectParseable for PublicKey {
21        fn check_label(label: &str) -> Result<(), EP> {
22            match label {
23                "SIGNATURE" => Ok(()),
24                _ => Err(EP::ObjectIncorrectLabel),
25            }
26        }
27        fn from_bytes(input: &[u8]) -> Result<Self, EP> {
28            PublicKey::from_der(input).ok_or(EP::ObjectInvalidData)
29        }
30    }
31
32    impl ItemValueParseable for PublicKey {
33        fn from_unparsed(item: UnparsedItem) -> Result<Self, EP> {
34            Ok(ParsePublicKey::from_unparsed(item)?.key)
35        }
36    }
37}
38
39/// Types related to times
40pub(crate) mod times {
41    use super::*;
42
43    /// Date and time in deprecated ISO8601-with-space separate arguments syntax
44    ///
45    /// Eg `dir-key-published` in a dir auth key cert.
46    #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, derive_more::Deref)]
47    #[allow(clippy::exhaustive_structs)]
48    pub struct NdaSystemTimeDeprecatedSyntax(#[deref] pub SystemTime);
49
50    impl ItemArgumentParseable for NdaSystemTimeDeprecatedSyntax {
51        fn from_args<'s>(
52            args: &mut ArgumentStream<'s>,
53            field: &'static str,
54        ) -> Result<Self, ErrorProblem> {
55            let t;
56            (t, *args) = (|| {
57                let args = args.clone().into_remaining();
58                let spc2 = args
59                    .match_indices(WS)
60                    .nth(1)
61                    .map(|(spc2, _)| spc2)
62                    .unwrap_or_else(|| args.len());
63                let (t, rest) = args.split_at(spc2);
64                let t: crate::types::misc::Iso8601TimeSp =
65                    t.parse().map_err(|_| EP::InvalidArgument { field })?;
66                let t = NdaSystemTimeDeprecatedSyntax(t.into());
67                Ok::<_, EP>((t, ArgumentStream::new(rest)))
68            })()?;
69            Ok(t)
70        }
71    }
72}
73
74/// Implementations on `Void`
75pub(crate) mod void_impls {
76    use super::*;
77
78    impl ItemValueParseable for Void {
79        fn from_unparsed(_item: UnparsedItem<'_>) -> Result<Self, ErrorProblem> {
80            Err(EP::ItemForbidden)
81        }
82    }
83}