1const POSITIVE_FIXINT: u8 = 0x00;
6#[allow(dead_code)]
7const NEGATIVE_FIXINT: u8 = 0xe0;
8const FIX_MAP: u8 = 0x80;
9const FIX_ARRAY: u8 = 0x90;
10const FIX_STR: u8 = 0xa0;
11
12const NIL: u8 = 0xc0;
13const NEVER_USED: u8 = 0xc1;
14const FALSE: u8 = 0xc2;
15const TRUE: u8 = 0xc3;
16const BIN8: u8 = 0xc4;
17const BIN16: u8 = 0xc5;
18const BIN32: u8 = 0xc6;
19const EXT8: u8 = 0xc7;
20const EXT16: u8 = 0xc8;
21const EXT32: u8 = 0xc9;
22const FLOAT32: u8 = 0xca;
23const FLOAT64: u8 = 0xcb;
24const UINT8: u8 = 0xcc;
25const UINT16: u8 = 0xcd;
26const UINT32: u8 = 0xce;
27const UINT64: u8 = 0xcf;
28const INT8: u8 = 0xd0;
29const INT16: u8 = 0xd1;
30const INT32: u8 = 0xd2;
31const INT64: u8 = 0xd3;
32const FIXEXT1: u8 = 0xd4;
33const FIXEXT2: u8 = 0xd5;
34const FIXEXT4: u8 = 0xd6;
35const FIXEXT8: u8 = 0xd7;
36const FIXEXT16: u8 = 0xd8;
37const STR8: u8 = 0xd9;
38const STR16: u8 = 0xda;
39const STR32: u8 = 0xdb;
40const ARRAY16: u8 = 0xdc;
41const ARRAY32: u8 = 0xdd;
42const MAP16: u8 = 0xde;
43const MAP32: u8 = 0xdf;
44
45#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq)]
47pub enum Format {
48 PositiveFixInt(u8),
50 FixMap(u8),
52 FixArray(u8),
54 FixStr(u8),
56 Nil,
58 NeverUsed,
60 False,
62 True,
64 Bin8,
66 Bin16,
68 Bin32,
70 Ext8,
72 Ext16,
74 Ext32,
76 Float32,
78 Float64,
80 Uint8,
82 Uint16,
84 Uint32,
86 Uint64,
88 Int8,
90 Int16,
92 Int32,
94 Int64,
96 FixExt1,
98 FixExt2,
100 FixExt4,
102 FixExt8,
104 FixExt16,
106 Str8,
108 Str16,
110 Str32,
112 Array16,
114 Array32,
116 Map16,
118 Map32,
120 NegativeFixInt(i8),
122}
123
124impl Format {
125 pub const fn as_byte(&self) -> u8 {
127 match self {
128 Format::PositiveFixInt(v) => POSITIVE_FIXINT | *v,
129 Format::FixMap(l) => FIX_MAP | *l,
130 Format::FixArray(l) => FIX_ARRAY | *l,
131 Format::FixStr(l) => FIX_STR | *l,
132 Format::Nil => NIL,
133 Format::NeverUsed => NEVER_USED,
134 Format::False => FALSE,
135 Format::True => TRUE,
136 Format::Bin8 => BIN8,
137 Format::Bin16 => BIN16,
138 Format::Bin32 => BIN32,
139 Format::Ext8 => EXT8,
140 Format::Ext16 => EXT16,
141 Format::Ext32 => EXT32,
142 Format::Float32 => FLOAT32,
143 Format::Float64 => FLOAT64,
144 Format::Uint8 => UINT8,
145 Format::Uint16 => UINT16,
146 Format::Uint32 => UINT32,
147 Format::Uint64 => UINT64,
148 Format::Int8 => INT8,
149 Format::Int16 => INT16,
150 Format::Int32 => INT32,
151 Format::Int64 => INT64,
152 Format::FixExt1 => FIXEXT1,
153 Format::FixExt2 => FIXEXT2,
154 Format::FixExt4 => FIXEXT4,
155 Format::FixExt8 => FIXEXT8,
156 Format::FixExt16 => FIXEXT16,
157 Format::Str8 => STR8,
158 Format::Str16 => STR16,
159 Format::Str32 => STR32,
160 Format::Array16 => ARRAY16,
161 Format::Array32 => ARRAY32,
162 Format::Map16 => MAP16,
163 Format::Map32 => MAP32,
164 Format::NegativeFixInt(v) => *v as u8,
165 }
166 }
167
168 pub const fn from_byte(byte: u8) -> Self {
170 match byte {
171 0x00..=0x7f => Self::PositiveFixInt(byte - POSITIVE_FIXINT),
172 0x80..=0x8f => Self::FixMap(byte - FIX_MAP),
173 0x90..=0x9f => Self::FixArray(byte - FIX_ARRAY),
174 0xa0..=0xbf => Self::FixStr(byte - FIX_STR),
175 NIL => Self::Nil,
176 NEVER_USED => Self::NeverUsed,
177 FALSE => Self::False,
178 TRUE => Self::True,
179 BIN8 => Self::Bin8,
180 BIN16 => Self::Bin16,
181 BIN32 => Self::Bin32,
182 EXT8 => Self::Ext8,
183 EXT16 => Self::Ext16,
184 EXT32 => Self::Ext32,
185 FLOAT32 => Self::Float32,
186 FLOAT64 => Self::Float64,
187 UINT8 => Self::Uint8,
188 UINT16 => Self::Uint16,
189 UINT32 => Self::Uint32,
190 UINT64 => Self::Uint64,
191 INT8 => Self::Int8,
192 INT16 => Self::Int16,
193 INT32 => Self::Int32,
194 INT64 => Self::Int64,
195 FIXEXT1 => Self::FixExt1,
196 FIXEXT2 => Self::FixExt2,
197 FIXEXT4 => Self::FixExt4,
198 FIXEXT8 => Self::FixExt8,
199 FIXEXT16 => Self::FixExt16,
200 STR8 => Self::Str8,
201 STR16 => Self::Str16,
202 STR32 => Self::Str32,
203 ARRAY16 => Self::Array16,
204 ARRAY32 => Self::Array32,
205 MAP16 => Self::Map16,
206 MAP32 => Self::Map32,
207 0xe0..=0xff => Self::NegativeFixInt(byte as i8),
208 }
209 }
210
211 pub const fn as_slice(&self) -> [u8; 1] {
213 self.as_byte().to_be_bytes()
214 }
215}
216
217impl IntoIterator for Format {
218 type Item = u8;
219 type IntoIter = core::array::IntoIter<u8, 1>;
220 fn into_iter(self) -> Self::IntoIter {
221 [self.as_byte()].into_iter()
222 }
223}