/rust/registry/src/index.crates.io-6f17d22bba15001f/der-0.7.10/src/asn1/null.rs
Line | Count | Source (jump to first uncovered line) |
1 | | //! ASN.1 `NULL` support. |
2 | | |
3 | | use crate::{ |
4 | | asn1::AnyRef, ord::OrdIsValueOrd, BytesRef, DecodeValue, EncodeValue, Error, ErrorKind, |
5 | | FixedTag, Header, Length, Reader, Result, Tag, Writer, |
6 | | }; |
7 | | |
8 | | /// ASN.1 `NULL` type. |
9 | | #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] |
10 | | pub struct Null; |
11 | | |
12 | | impl_any_conversions!(Null); |
13 | | |
14 | | impl<'a> DecodeValue<'a> for Null { |
15 | 0 | fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> { |
16 | 0 | if header.length.is_zero() { |
17 | 0 | Ok(Null) |
18 | | } else { |
19 | 0 | Err(reader.error(ErrorKind::Length { tag: Self::TAG })) |
20 | | } |
21 | 0 | } |
22 | | } |
23 | | |
24 | | impl EncodeValue for Null { |
25 | 0 | fn value_len(&self) -> Result<Length> { |
26 | 0 | Ok(Length::ZERO) |
27 | 0 | } |
28 | | |
29 | 0 | fn encode_value(&self, _writer: &mut impl Writer) -> Result<()> { |
30 | 0 | Ok(()) |
31 | 0 | } |
32 | | } |
33 | | |
34 | | impl FixedTag for Null { |
35 | | const TAG: Tag = Tag::Null; |
36 | | } |
37 | | |
38 | | impl OrdIsValueOrd for Null {} |
39 | | |
40 | | impl<'a> From<Null> for AnyRef<'a> { |
41 | 0 | fn from(_: Null) -> AnyRef<'a> { |
42 | 0 | AnyRef::from_tag_and_value(Tag::Null, BytesRef::default()) |
43 | 0 | } |
44 | | } |
45 | | |
46 | | impl TryFrom<AnyRef<'_>> for () { |
47 | | type Error = Error; |
48 | | |
49 | 0 | fn try_from(any: AnyRef<'_>) -> Result<()> { |
50 | 0 | Null::try_from(any).map(|_| ()) |
51 | 0 | } |
52 | | } |
53 | | |
54 | | impl<'a> From<()> for AnyRef<'a> { |
55 | 0 | fn from(_: ()) -> AnyRef<'a> { |
56 | 0 | Null.into() |
57 | 0 | } |
58 | | } |
59 | | |
60 | | impl<'a> DecodeValue<'a> for () { |
61 | 0 | fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> { |
62 | 0 | Null::decode_value(reader, header)?; |
63 | 0 | Ok(()) |
64 | 0 | } |
65 | | } |
66 | | |
67 | | impl EncodeValue for () { |
68 | 0 | fn value_len(&self) -> Result<Length> { |
69 | 0 | Ok(Length::ZERO) |
70 | 0 | } |
71 | | |
72 | 0 | fn encode_value(&self, _writer: &mut impl Writer) -> Result<()> { |
73 | 0 | Ok(()) |
74 | 0 | } |
75 | | } |
76 | | |
77 | | impl FixedTag for () { |
78 | | const TAG: Tag = Tag::Null; |
79 | | } |
80 | | |
81 | | #[cfg(test)] |
82 | | mod tests { |
83 | | use super::Null; |
84 | | use crate::{Decode, Encode}; |
85 | | |
86 | | #[test] |
87 | | fn decode() { |
88 | | Null::from_der(&[0x05, 0x00]).unwrap(); |
89 | | } |
90 | | |
91 | | #[test] |
92 | | fn encode() { |
93 | | let mut buffer = [0u8; 2]; |
94 | | assert_eq!(&[0x05, 0x00], Null.encode_to_slice(&mut buffer).unwrap()); |
95 | | assert_eq!(&[0x05, 0x00], ().encode_to_slice(&mut buffer).unwrap()); |
96 | | } |
97 | | |
98 | | #[test] |
99 | | fn reject_non_canonical() { |
100 | | assert!(Null::from_der(&[0x05, 0x81, 0x00]).is_err()); |
101 | | } |
102 | | } |