1pub mod de;
4pub mod enc;
5mod ranges;
6
7pub use self::{de::Decoder, enc::Encoder};
8use crate::error::{DecodeError, EncodeError};
9use crate::types::Constraints;
10pub fn decode<T: crate::Decode>(input: &[u8]) -> Result<T, DecodeError> {
15 T::decode(&mut Decoder::new(
16 crate::types::BitStr::from_slice(input),
17 de::DecoderOptions::oer(),
18 ))
19}
20pub fn encode<T: crate::Encode>(value: &T) -> Result<alloc::vec::Vec<u8>, EncodeError> {
26 let mut enc = Encoder::new(enc::EncoderOptions::coer());
27 value.encode(&mut enc)?;
28 Ok(enc.output())
29}
30#[allow(dead_code)]
35pub fn decode_with_constraints<T: crate::Decode>(
36 constraints: Constraints,
37 input: &[u8],
38) -> Result<T, DecodeError> {
39 T::decode_with_constraints(
40 &mut Decoder::new(
41 crate::types::BitStr::from_slice(input),
42 de::DecoderOptions::oer(),
43 ),
44 constraints,
45 )
46}
47#[allow(dead_code)]
52pub fn encode_with_constraints<T: crate::Encode>(
53 constraints: Constraints,
54 value: &T,
55) -> Result<alloc::vec::Vec<u8>, EncodeError> {
56 let mut enc = Encoder::new(enc::EncoderOptions::coer());
57 value.encode_with_constraints(&mut enc, constraints)?;
58 Ok(enc.output())
59}
60
61#[cfg(test)]
62mod tests {
63 use crate as rasn;
66 use crate::prelude::*;
67
68 #[test]
69 fn test_bool() {
70 for value in 0x01..=0xFEu8 {
71 let bytes = [value];
72 decode_error!(coer, bool, &bytes);
74 decode_ok!(oer, bool, &bytes, true);
75 }
76 }
77 #[test]
78 fn test_length_determinant() {
79 decode_error!(coer, Integer, &[0x00, 0x00, 0x00, 0x01, 0x01]);
81 decode_ok!(oer, Integer, &[0x00, 0x00, 0x00, 0x01, 0x01], 1.into());
82 decode_error!(coer, Integer, &[0x00, 0x00, 0x00, 0x01, 0x01]);
83 decode_error!(coer, Integer, &[0b1000_0001, 0x01, 0x01]);
85 decode_ok!(oer, Integer, &[0b1000_0001, 0x01, 0x01], 1.into());
86 }
87 #[test]
88 fn test_enumerated() {
89 #[derive(AsnType, Decode, Encode, Debug, Clone, Copy, PartialEq)]
91 #[rasn(enumerated)]
92 enum Test {
93 A = 1,
94 B = 2,
95 }
96 #[derive(AsnType, Decode, Encode, Debug, Clone, Copy, PartialEq)]
97 #[rasn(enumerated)]
98 enum TestDefaults {
99 A,
100 B,
101 }
102 decode_error!(coer, Test, &[0x00, 0x00, 0x00, 0x01, 0x01]);
104 round_trip!(oer, Test, Test::A, &[0x01]);
106 round_trip!(oer, TestDefaults, TestDefaults::A, &[0x00]);
107 decode_ok!(oer, TestDefaults, &[0x00, 0x00], TestDefaults::A);
110 decode_error!(oer, Test, &[0x00, 0x01]);
111 decode_error!(oer, Test, &[0x00, 0x81, 0x01]);
112 decode_ok!(oer, Test, &[0x81, 0x01], Test::A);
113 decode_ok!(oer, Test, &[0x01], Test::A);
114 decode_error!(coer, Test, &[0b1000_0001, 0x01]);
116 decode_ok!(oer, Test, &[0b1000_0001, 0x01], Test::A);
117 }
118}