1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use crate::{SIGNED, BYTES, TEXT, ARRAY, MAP, TAGGED, SIMPLE};
use crate::data::Tag;
use crate::encode::{Encode, Error, Write};
#[derive(Debug, Clone)]
pub struct Encoder<W> { writer: W }
impl<W> AsRef<W> for Encoder<W> {
fn as_ref(&self) -> &W {
&self.writer
}
}
impl<W: Write> Encoder<W> {
pub fn new(writer: W) -> Encoder<W> {
Encoder { writer }
}
pub fn into_inner(self) -> W {
self.writer
}
pub fn encode<T: Encode>(&mut self, x: T) -> Result<&mut Self, Error<W::Error>> {
x.encode(self)?;
Ok(self)
}
pub fn u8(&mut self, x: u8) -> Result<&mut Self, Error<W::Error>> {
if let 0 ..= 0x17 = x {
self.put(&[x])
} else {
self.put(&[24, x])
}
}
pub fn i8(&mut self, x: i8) -> Result<&mut Self, Error<W::Error>> {
if x >= 0 {
return self.u8(x as u8)
}
match (-1 - x) as u8 {
n @ 0 ..= 0x17 => self.put(&[SIGNED | n]),
n => self.put(&[SIGNED | 24, n])
}
}
pub fn u16(&mut self, x: u16) -> Result<&mut Self, Error<W::Error>> {
match x {
0 ..= 0x17 => self.put(&[x as u8]),
0x18 ..= 0xff => self.put(&[24, x as u8]),
_ => self.put(&[25])?.put(&x.to_be_bytes()[..])
}
}
pub fn i16(&mut self, x: i16) -> Result<&mut Self, Error<W::Error>> {
if x >= 0 {
return self.u16(x as u16)
}
match (-1 - x) as u16 {
n @ 0 ..= 0x17 => self.put(&[SIGNED | n as u8]),
n @ 0x18 ..= 0xff => self.put(&[SIGNED | 24, n as u8]),
n => self.put(&[SIGNED | 25])?.put(&n.to_be_bytes()[..])
}
}
pub fn u32(&mut self, x: u32) -> Result<&mut Self, Error<W::Error>> {
match x {
0 ..= 0x17 => self.put(&[x as u8]),
0x18 ..= 0xff => self.put(&[24, x as u8]),
0x100 ..= 0xffff => self.put(&[25])?.put(&(x as u16).to_be_bytes()[..]),
_ => self.put(&[26])?.put(&x.to_be_bytes()[..])
}
}
pub fn i32(&mut self, x: i32) -> Result<&mut Self, Error<W::Error>> {
if x >= 0 {
return self.u32(x as u32)
}
match (-1 - x) as u32 {
n @ 0 ..= 0x17 => self.put(&[SIGNED | n as u8]),
n @ 0x18 ..= 0xff => self.put(&[SIGNED | 24, n as u8]),
n @ 0x100 ..= 0xffff => self.put(&[SIGNED | 25])?.put(&(n as u16).to_be_bytes()[..]),
n => self.put(&[SIGNED | 26])?.put(&n.to_be_bytes()[..])
}
}
pub fn u64(&mut self, x: u64) -> Result<&mut Self, Error<W::Error>> {
match x {
0 ..= 0x17 => self.put(&[x as u8]),
0x18 ..= 0xff => self.put(&[24, x as u8]),
0x100 ..= 0xffff => self.put(&[25])?.put(&(x as u16).to_be_bytes()[..]),
0x1_0000 ..= 0xffff_ffff => self.put(&[26])?.put(&(x as u32).to_be_bytes()[..]),
_ => self.put(&[27])?.put(&x.to_be_bytes()[..])
}
}
pub fn i64(&mut self, x: i64) -> Result<&mut Self, Error<W::Error>> {
if x >= 0 {
return self.u64(x as u64)
}
match (-1 - x) as u64 {
n @ 0 ..= 0x17 => self.put(&[SIGNED | n as u8]),
n @ 0x18 ..= 0xff => self.put(&[SIGNED | 24, n as u8]),
n @ 0x100 ..= 0xffff => self.put(&[SIGNED | 25])?.put(&(n as u16).to_be_bytes()[..]),
n @ 0x1_0000 ..= 0xffff_ffff => self.put(&[SIGNED | 26])?.put(&(n as u32).to_be_bytes()[..]),
n => self.put(&[SIGNED | 27])?.put(&n.to_be_bytes()[..])
}
}
pub fn null(&mut self) -> Result<&mut Self, Error<W::Error>> {
self.put(&[SIMPLE | 22])
}
pub fn undefined(&mut self) -> Result<&mut Self, Error<W::Error>> {
self.put(&[SIMPLE | 23])
}
pub fn simple(&mut self, x: u8) -> Result<&mut Self, Error<W::Error>> {
if x < 0x14 {
self.put(&[SIMPLE | x])
} else {
self.put(&[SIMPLE | 24, x])
}
}
#[cfg(feature = "half")]
pub fn f16(&mut self, x: f32) -> Result<&mut Self, Error<W::Error>> {
let n = half::f16::from_f32(x).to_bits();
self.put(&[SIMPLE | 25])?.put(&n.to_be_bytes()[..])
}
pub fn f32(&mut self, x: f32) -> Result<&mut Self, Error<W::Error>> {
self.put(&[SIMPLE | 26])?.put(&x.to_be_bytes()[..])
}
pub fn f64(&mut self, x: f64) -> Result<&mut Self, Error<W::Error>> {
self.put(&[SIMPLE | 27])?.put(&x.to_be_bytes()[..])
}
pub fn bool(&mut self, x: bool) -> Result<&mut Self, Error<W::Error>> {
self.put(&[SIMPLE | if x { 0x15 } else { 0x14 }])
}
pub fn char(&mut self, x: char) -> Result<&mut Self, Error<W::Error>> {
self.u32(u32::from(x))
}
pub fn tag(&mut self, x: Tag) -> Result<&mut Self, Error<W::Error>> {
self.type_len(TAGGED, x.into())
}
pub fn bytes(&mut self, x: &[u8]) -> Result<&mut Self, Error<W::Error>> {
self.type_len(BYTES, x.len() as u64)?.put(x)
}
pub fn str(&mut self, x: &str) -> Result<&mut Self, Error<W::Error>> {
self.type_len(TEXT, x.len() as u64)?.put(x.as_bytes())
}
pub fn array(&mut self, len: u64) -> Result<&mut Self, Error<W::Error>> {
self.type_len(ARRAY, len)
}
pub fn map(&mut self, len: u64) -> Result<&mut Self, Error<W::Error>> {
self.type_len(MAP, len)
}
pub fn begin_array(&mut self) -> Result<&mut Self, Error<W::Error>> {
self.put(&[0x9f])
}
pub fn begin_bytes(&mut self) -> Result<&mut Self, Error<W::Error>> {
self.put(&[0x5f])
}
pub fn begin_map(&mut self) -> Result<&mut Self, Error<W::Error>> {
self.put(&[0xbf])
}
pub fn begin_str(&mut self) -> Result<&mut Self, Error<W::Error>> {
self.put(&[0x7f])
}
pub fn end(&mut self) -> Result<&mut Self, Error<W::Error>> {
self.put(&[0xff])
}
pub fn ok(&mut self) -> Result<(), Error<W::Error>> {
Ok(())
}
fn put(&mut self, b: &[u8]) -> Result<&mut Self, Error<W::Error>> {
self.writer.write_all(b).map_err(Error::Write)?;
Ok(self)
}
fn type_len(&mut self, t: u8, x: u64) -> Result<&mut Self, Error<W::Error>> {
match x {
0 ..= 0x17 => self.put(&[t | x as u8]),
0x18 ..= 0xff => self.put(&[t | 24, x as u8]),
0x100 ..= 0xffff => self.put(&[t | 25])?.put(&(x as u16).to_be_bytes()[..]),
0x1_0000 ..= 0xffff_ffff => self.put(&[t | 26])?.put(&(x as u32).to_be_bytes()),
_ => self.put(&[t | 27])?.put(&x.to_be_bytes()[..])
}
}
}