der/asn1/
internal_macros.rs

1macro_rules! impl_any_conversions {
2    ($type: ty) => {
3        impl_any_conversions!($type, );
4    };
5    ($type: ty, $($li: lifetime)?) => {
6        impl<'__der: $($li),*, $($li),*> TryFrom<$crate::AnyRef<'__der>> for $type {
7            type Error = $crate::Error;
8
9            fn try_from(any: $crate::AnyRef<'__der>) -> Result<$type> {
10                any.decode_as()
11            }
12        }
13
14        #[cfg(feature = "alloc")]
15        impl<'__der: $($li),*, $($li),*> TryFrom<&'__der $crate::Any> for $type {
16            type Error = $crate::Error;
17
18            fn try_from(any: &'__der $crate::Any) -> Result<$type> {
19                any.decode_as()
20            }
21        }
22    };
23}
24
25macro_rules! impl_string_type {
26    ($type: ty, $($li: lifetime)?) => {
27        impl_any_conversions!($type, $($li),*);
28
29        mod __impl_string {
30            use super::*;
31
32            use crate::{
33                ord::OrdIsValueOrd, BytesRef, DecodeValue, EncodeValue, Header, Length, Reader,
34                Result, Writer,
35            };
36            use core::{fmt, str};
37
38            impl<$($li),*> AsRef<str> for $type {
39                fn as_ref(&self) -> &str {
40                    self.as_str()
41                }
42            }
43
44            impl<$($li),*> AsRef<[u8]> for $type {
45                fn as_ref(&self) -> &[u8] {
46                    self.as_bytes()
47                }
48            }
49
50            impl<'__der: $($li),*, $($li),*> DecodeValue<'__der> for $type {
51                fn decode_value<R: Reader<'__der>>(reader: &mut R, header: Header) -> Result<Self> {
52                    Self::new(BytesRef::decode_value(reader, header)?.as_slice())
53                }
54            }
55
56            impl<$($li),*> EncodeValue for $type {
57                fn value_len(&self) -> Result<Length> {
58                    self.inner.value_len()
59                }
60
61                fn encode_value(&self, writer: &mut impl Writer) -> Result<()> {
62                    self.inner.encode_value(writer)
63                }
64            }
65
66            impl<$($li),*> OrdIsValueOrd for $type {}
67
68            impl<$($li),*> fmt::Display for $type {
69                fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70                    f.write_str(self.as_str())
71                }
72            }
73        }
74    };
75}