Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_jdat/src/bdat/enc.rs

27.5 KiB, 30 runs

created by r1870400018:449, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1use crate::prelude::*;
2
3use oxedyne_fe2o3_core::{
4 prelude::*,
5 byte::ToBytes,
6};
7use oxedyne_fe2o3_num::float::{
8 Float32,
9 Float64,
10};
11
12
13fn wrong_frame(dat: &Dat) -> Error<ErrTag> {
14 err!(
15 "The daticle kind {:?} was encoded in a frame that does not encode it. The dispatch in \
16 Dat::to_bytes and the frames it dispatches to disagree.", dat.kind();
17 Bug, Mismatch)
18}
19
20impl ToBytes for Dat {
21
22 /// Appends the encoded `Dat` to the given byte buffer.
23 ///
24 /// Every kind that encloses other kinds is encoded in a frame of its own, so that the frame
25 /// this dispatch repeats as it descends holds nothing but the dispatch. A single match over
26 /// every kind would give one frame the locals of all of them, and a value nested a few hundred
27 /// deep would then exhaust the stack while encoding something that decodes perfectly well.
28 /// [`Dat::from_bytes_depth`](crate::Dat) keeps its frames small for the same reason.
29 fn to_bytes(&self, buf: Vec<u8>) -> Outcome<Vec<u8>> {
30 match self {
31 // Molecular Kinds ========================
32 // Unitary
33 Self::Usr(..) => self.to_bytes_usr(buf),
34 Self::Opt(..) => self.to_bytes_opt(buf),
35 Self::Box(..) => self.to_bytes_box(buf),
36 Self::ABox(..) => self.to_bytes_abox(buf),
37 // Heterogenous
38 Self::List(..) |
39 Self::Vek(..) => self.to_bytes_list(buf),
40 Self::Tup2(..) |
41 Self::Tup3(..) |
42 Self::Tup4(..) |
43 Self::Tup5(..) |
44 Self::Tup6(..) |
45 Self::Tup7(..) |
46 Self::Tup8(..) |
47 Self::Tup9(..) |
48 Self::Tup10(..) => self.to_bytes_tuple(buf),
49 Self::Map(..) |
50 Self::OrdMap(..) => self.to_bytes_map(buf),
51 // Every other kind is atomic, enclosing no other kind. Those arms carry the bulk of
52 // the encoder's stack frame, so they too are encoded in a frame of their own, which is
53 // entered at a leaf and left there.
54 _ => self.to_bytes_atomic(buf),
55 }
56 }
57}
58
59impl Dat {
60
61 #[inline(never)]
62 fn to_bytes_usr(&self, mut buf: Vec<u8>) -> Outcome<Vec<u8>> {
63 match self {
64 Self::Usr(ukid, optboxd) => {
65 self.append_code(&mut buf);
66 buf.extend_from_slice(&ukid.code().to_be_bytes());
67 match optboxd {
68 Some(boxd) => {
69 buf.push(Self::OPT_SOME_CODE);
70 buf = res!(boxd.to_bytes(buf));
71 },
72 None => buf.push(Self::OPT_NONE_CODE),
73 }
74 Ok(buf)
75 },
76 _ => Err(wrong_frame(self)),
77 }
78 }
79
80 #[inline(never)]
81 fn to_bytes_opt(&self, mut buf: Vec<u8>) -> Outcome<Vec<u8>> {
82 match self {
83 Self::Opt(boxoptd) => {
84 self.append_code(&mut buf);
85 if let Some(d) = &**boxoptd {
86 buf = res!(d.to_bytes(buf));
87 }
88 Ok(buf)
89 },
90 _ => Err(wrong_frame(self)),
91 }
92 }
93
94 #[inline(never)]
95 fn to_bytes_box(&self, mut buf: Vec<u8>) -> Outcome<Vec<u8>> {
96 match self {
97 Self::Box(boxd) => {
98 self.append_code(&mut buf);
99 Ok(res!(boxd.to_bytes(buf)))
100 },
101 _ => Err(wrong_frame(self)),
102 }
103 }
104
105 #[inline(never)]
106 fn to_bytes_abox(&self, mut buf: Vec<u8>) -> Outcome<Vec<u8>> {
107 match self {
108 Self::ABox(ncfg, boxd, s) => {
109 self.append_code(&mut buf);
110 buf = res!(ncfg.to_bytes(buf));
111 buf = res!(boxd.to_bytes(buf));
112 let b = s.as_bytes();
113 buf = res!(Dat::C64(b.len() as u64).to_bytes(buf));
114 buf.extend_from_slice(b);
115 Ok(buf)
116 },
117 _ => Err(wrong_frame(self)),
118 }
119 }
120
121 #[inline(never)]
122 fn to_bytes_list(&self, mut buf: Vec<u8>) -> Outcome<Vec<u8>> {
123 match self {
124 Self::List(v) => {
125 self.append_code(&mut buf);
126 Ok(res!(Self::vec_to_bytes(v, buf)))
127 },
128 Self::Vek(vek) => {
129 self.append_code(&mut buf);
130 Ok(res!(Self::vec_to_bytes(&*vek, buf)))
131 },
132 _ => Err(wrong_frame(self)),
133 }
134 }
135
136 #[inline(never)]
137 fn to_bytes_tuple(&self, buf: Vec<u8>) -> Outcome<Vec<u8>> {
138 match self {
139 Self::Tup2(a) => Self::tuple_to_bytes::<Dat, 2, {Self::TUP_SERIES_START}>(a, buf),
140 Self::Tup3(a) => Self::tuple_to_bytes::<Dat, 3, {Self::TUP_SERIES_START}>(a, buf),
141 Self::Tup4(a) => Self::tuple_to_bytes::<Dat, 4, {Self::TUP_SERIES_START}>(a, buf),
142 Self::Tup5(a) => Self::tuple_to_bytes::<Dat, 5, {Self::TUP_SERIES_START}>(a, buf),
143 Self::Tup6(a) => Self::tuple_to_bytes::<Dat, 6, {Self::TUP_SERIES_START}>(a, buf),
144 Self::Tup7(a) => Self::tuple_to_bytes::<Dat, 7, {Self::TUP_SERIES_START}>(a, buf),
145 Self::Tup8(a) => Self::tuple_to_bytes::<Dat, 8, {Self::TUP_SERIES_START}>(a, buf),
146 Self::Tup9(a) => Self::tuple_to_bytes::<Dat, 9, {Self::TUP_SERIES_START}>(a, buf),
147 Self::Tup10(a) => Self::tuple_to_bytes::<Dat, 10, {Self::TUP_SERIES_START}>(a, buf),
148 _ => Err(wrong_frame(self)),
149 }
150 }
151
152 #[inline(never)]
153 fn to_bytes_map(&self, mut buf: Vec<u8>) -> Outcome<Vec<u8>> {
154 let mut buf2 = Vec::new();
155 match self {
156 Self::Map(map) => {
157 for (k, v) in map {
158 buf2 = res!(k.to_bytes(buf2));
159 buf2 = res!(v.to_bytes(buf2));
160 }
161 },
162 Self::OrdMap(map) => {
163 for (k, v) in map {
164 buf2 = res!(k.to_bytes(buf2));
165 buf2 = res!(v.to_bytes(buf2));
166 }
167 },
168 _ => return Err(wrong_frame(self)),
169 }
170 self.append_code(&mut buf);
171 buf = res!(Dat::C64(buf2.len() as u64).to_bytes(buf));
172 buf.extend_from_slice(&buf2);
173 Ok(buf)
174 }
175
176 #[inline(never)]
177 fn to_bytes_atomic(&self, mut buf: Vec<u8>) -> Outcome<Vec<u8>> {
178 match self {
179 // Molecular Kinds ========================
180 // Each of these is encoded in a frame of its own, and never here.
181 Self::Usr(..) |
182 Self::Opt(..) |
183 Self::Box(..) |
184 Self::ABox(..) |
185 Self::List(..) |
186 Self::Vek(..) |
187 Self::Tup2(..) |
188 Self::Tup3(..) |
189 Self::Tup4(..) |
190 Self::Tup5(..) |
191 Self::Tup6(..) |
192 Self::Tup7(..) |
193 Self::Tup8(..) |
194 Self::Tup9(..) |
195 Self::Tup10(..) |
196 Self::Map(..) |
197 Self::OrdMap(..) => return Err(wrong_frame(self)),
198 // Atomic Kinds ===========================
199 // Logic
200 Self::Empty => {
201 self.append_code(&mut buf);
202 },
203 Self::Bool(_b) => {
204 self.append_code(&mut buf);
205 },
206 // Fixed
207 Self::U8(n) => {
208 self.append_code(&mut buf);
209 buf.extend_from_slice(&n.to_be_bytes());
210 },
211 Self::U16(n) => {
212 self.append_code(&mut buf);
213 buf.extend_from_slice(&n.to_be_bytes());
214 },
215 Self::U32(n) => {
216 self.append_code(&mut buf);
217 buf.extend_from_slice(&n.to_be_bytes());
218 },
219 Self::U64(n) => {
220 self.append_code(&mut buf);
221 buf.extend_from_slice(&n.to_be_bytes());
222 },
223 Self::U128(n) => {
224 self.append_code(&mut buf);
225 buf.extend_from_slice(&n.to_be_bytes());
226 },
227 Self::I8(n) => {
228 self.append_code(&mut buf);
229 buf.extend_from_slice(&n.to_be_bytes());
230 },
231 Self::I16(n) => {
232 self.append_code(&mut buf);
233 buf.extend_from_slice(&n.to_be_bytes());
234 },
235 Self::I32(n) => {
236 self.append_code(&mut buf);
237 buf.extend_from_slice(&n.to_be_bytes());
238 },
239 Self::I64(n) => {
240 self.append_code(&mut buf);
241 buf.extend_from_slice(&n.to_be_bytes());
242 },
243 Self::I128(n) => {
244 self.append_code(&mut buf);
245 buf.extend_from_slice(&n.to_be_bytes());
246 },
247 Self::F32(Float32(f)) => {
248 self.append_code(&mut buf);
249 buf.extend_from_slice(&f.to_be_bytes());
250 },
251 Self::F64(Float64(f)) => {
252 self.append_code(&mut buf);
253 buf.extend_from_slice(&f.to_be_bytes());
254 },
255 // Variable
256 Self::Aint(bigint) => {
257 let v = bigint.to_signed_bytes_be();
258 self.append_code(&mut buf);
259 buf = res!(Dat::C64(v.len() as u64).to_bytes(buf));
260 buf.extend_from_slice(&v);
261 },
262 Self::Adec(bigdec) => {
263 let (bigint, expi64) = bigdec.as_bigint_and_exponent();
264 let mut vbuf = Vec::new();
265 let v = bigint.to_signed_bytes_be();
266 vbuf.extend_from_slice(&v);
267 vbuf.extend_from_slice(&expi64.to_be_bytes());
268 self.append_code(&mut buf);
269 buf = res!(Dat::C64((v.len() as u64) + 8).to_bytes(buf));
270 buf.extend_from_slice(&vbuf);
271 },
272 Self::C64(n) => {
273 let byts: [u8; 8] = n.to_be_bytes();
274 let mut count: usize = 8;
275 for byt in &byts[..] {
276 if *byt == 0 {
277 count -= 1;
278 } else {
279 break;
280 }
281 }
282 self.append_code(&mut buf);
283 let last = buf.len() - 1 ;
284 buf[last] += count as u8;
285 while count > 0 {
286 buf.push(byts[8-count]);
287 count -= 1;
288 }
289 },
290 Self::Str(s) => {
291 self.append_code(&mut buf);
292 let b = s.as_bytes();
293 buf = res!(Dat::C64(b.len() as u64).to_bytes(buf));
294 buf.extend_from_slice(b);
295 },
296 // Variable length bytes
297 Self::BU8(v) => {
298 self.append_code(&mut buf);
299 // Use a u8 for the data length.
300 buf.extend_from_slice(&(v.len() as u8).to_be_bytes());
301 buf.extend_from_slice(v);
302 },
303 Self::BU16(v) => {
304 self.append_code(&mut buf);
305 // Use a u16 for the data length.
306 buf.extend_from_slice(&(v.len() as u16).to_be_bytes());
307 buf.extend_from_slice(v);
308 },
309 Self::BU32(v) => {
310 self.append_code(&mut buf);
311 // Use a u32 for the data length.
312 buf.extend_from_slice(&(v.len() as u32).to_be_bytes());
313 buf.extend_from_slice(v);
314 },
315 Self::BU64(v) => {
316 self.append_code(&mut buf);
317 // Use a u64 for the data length.
318 buf.extend_from_slice(&(v.len() as u64).to_be_bytes());
319 buf.extend_from_slice(v);
320 },
321 Self::BC64(v) => {
322 self.append_code(&mut buf);
323 // Use a variable length Dat::C64 for the data length.
324 buf = res!(Dat::C64(v.len() as u64).to_bytes(buf));
325 buf.extend_from_slice(v);
326 },
327 // Fixed length bytes
328 Self::B2(a) => {
329 self.append_code(&mut buf);
330 buf.extend_from_slice(&a[..]);
331 },
332 Self::B3(a) => {
333 self.append_code(&mut buf);
334 buf.extend_from_slice(&a[..]);
335 },
336 Self::B4(a) => {
337 self.append_code(&mut buf);
338 buf.extend_from_slice(&a[..]);
339 },
340 Self::B5(a) => {
341 self.append_code(&mut buf);
342 buf.extend_from_slice(&a[..]);
343 },
344 Self::B6(a) => {
345 self.append_code(&mut buf);
346 buf.extend_from_slice(&a[..]);
347 },
348 Self::B7(a) => {
349 self.append_code(&mut buf);
350 buf.extend_from_slice(&a[..]);
351 },
352 Self::B8(a) => {
353 self.append_code(&mut buf);
354 buf.extend_from_slice(&a[..]);
355 },
356 Self::B9(a) => {
357 self.append_code(&mut buf);
358 buf.extend_from_slice(&a[..]);
359 },
360 Self::B10(a) => {
361 self.append_code(&mut buf);
362 buf.extend_from_slice(&a[..]);
363 },
364 Self::B16(a) => {
365 self.append_code(&mut buf);
366 buf.extend_from_slice(&a[..]);
367 },
368 Self::B32(a) => {
369 self.append_code(&mut buf);
370 buf.extend_from_slice(&a[..]);
371 },
372 // Fixed length numbers
373 Self::Tup2u8(a) => {
374 buf = res!(Self::tuple_to_bytes::<u8, 2, {Self::TUP_U8_SERIES_START}>(a, buf));
375 },
376 Self::Tup3u8(a) => {
377 buf = res!(Self::tuple_to_bytes::<u8, 3, {Self::TUP_U8_SERIES_START}>(a, buf));
378 },
379 Self::Tup4u8(a) => {
380 buf = res!(Self::tuple_to_bytes::<u8, 4, {Self::TUP_U8_SERIES_START}>(a, buf));
381 },
382 Self::Tup5u8(a) => {
383 buf = res!(Self::tuple_to_bytes::<u8, 5, {Self::TUP_U8_SERIES_START}>(a, buf));
384 },
385 Self::Tup6u8(a) => {
386 buf = res!(Self::tuple_to_bytes::<u8, 6, {Self::TUP_U8_SERIES_START}>(a, buf));
387 },
388 Self::Tup7u8(a) => {
389 buf = res!(Self::tuple_to_bytes::<u8, 7, {Self::TUP_U8_SERIES_START}>(a, buf));
390 },
391 Self::Tup8u8(a) => {
392 buf = res!(Self::tuple_to_bytes::<u8, 8, {Self::TUP_U8_SERIES_START}>(a, buf));
393 },
394 Self::Tup9u8(a) => {
395 buf = res!(Self::tuple_to_bytes::<u8, 9, {Self::TUP_U8_SERIES_START}>(a, buf));
396 },
397 Self::Tup10u8(a) => {
398 buf = res!(Self::tuple_to_bytes::<u8, 10, {Self::TUP_U8_SERIES_START}>(a, buf));
399 },
400 Self::Tup2u16(a) => {
401 buf = res!(Self::tuple_to_bytes::<u16, 2, {Self::TUP_U16_SERIES_START}>(a, buf));
402 },
403 Self::Tup3u16(a) => {
404 buf = res!(Self::tuple_to_bytes::<u16, 3, {Self::TUP_U16_SERIES_START}>(a, buf));
405 },
406 Self::Tup4u16(a) => {
407 buf = res!(Self::tuple_to_bytes::<u16, 4, {Self::TUP_U16_SERIES_START}>(a, buf));
408 },
409 Self::Tup5u16(a) => {
410 buf = res!(Self::tuple_to_bytes::<u16, 5, {Self::TUP_U16_SERIES_START}>(a, buf));
411 },
412 Self::Tup6u16(a) => {
413 buf = res!(Self::tuple_to_bytes::<u16, 6, {Self::TUP_U16_SERIES_START}>(a, buf));
414 },
415 Self::Tup7u16(a) => {
416 buf = res!(Self::tuple_to_bytes::<u16, 7, {Self::TUP_U16_SERIES_START}>(a, buf));
417 },
418 Self::Tup8u16(a) => {
419 buf = res!(Self::tuple_to_bytes::<u16, 8, {Self::TUP_U16_SERIES_START}>(a, buf));
420 },
421 Self::Tup9u16(a) => {
422 buf = res!(Self::tuple_to_bytes::<u16, 9, {Self::TUP_U16_SERIES_START}>(a, buf));
423 },
424 Self::Tup10u16(a) => {
425 buf = res!(Self::tuple_to_bytes::<u16, 10, {Self::TUP_U16_SERIES_START}>(a, buf));
426 },
427 Self::Tup2u32(a) => {
428 buf = res!(Self::tuple_to_bytes::<u32, 2, {Self::TUP_U32_SERIES_START}>(a, buf));
429 },
430 Self::Tup3u32(a) => {
431 buf = res!(Self::tuple_to_bytes::<u32, 3, {Self::TUP_U32_SERIES_START}>(a, buf));
432 },
433 Self::Tup4u32(a) => {
434 buf = res!(Self::tuple_to_bytes::<u32, 4, {Self::TUP_U32_SERIES_START}>(a, buf));
435 },
436 Self::Tup5u32(a) => {
437 buf = res!(Self::tuple_to_bytes::<u32, 5, {Self::TUP_U32_SERIES_START}>(a, buf));
438 },
439 Self::Tup6u32(a) => {
440 buf = res!(Self::tuple_to_bytes::<u32, 6, {Self::TUP_U32_SERIES_START}>(a, buf));
441 },
442 Self::Tup7u32(a) => {
443 buf = res!(Self::tuple_to_bytes::<u32, 7, {Self::TUP_U32_SERIES_START}>(a, buf));
444 },
445 Self::Tup8u32(a) => {
446 buf = res!(Self::tuple_to_bytes::<u32, 8, {Self::TUP_U32_SERIES_START}>(a, buf));
447 },
448 Self::Tup9u32(a) => {
449 buf = res!(Self::tuple_to_bytes::<u32, 9, {Self::TUP_U32_SERIES_START}>(a, buf));
450 },
451 Self::Tup10u32(a) => {
452 buf = res!(Self::tuple_to_bytes::<u32, 10, {Self::TUP_U32_SERIES_START}>(a, buf));
453 },
454 Self::Tup2u64(a) => {
455 buf = res!(Self::tuple_to_bytes::<u64, 2, {Self::TUP_U64_SERIES_START}>(a, buf));
456 },
457 Self::Tup3u64(a) => {
458 buf = res!(Self::tuple_to_bytes::<u64, 3, {Self::TUP_U64_SERIES_START}>(a, buf));
459 },
460 Self::Tup4u64(a) => {
461 buf = res!(Self::tuple_to_bytes::<u64, 4, {Self::TUP_U64_SERIES_START}>(a, buf));
462 },
463 Self::Tup5u64(a) => {
464 buf = res!(Self::tuple_to_bytes::<u64, 5, {Self::TUP_U64_SERIES_START}>(a, buf));
465 },
466 Self::Tup6u64(a) => {
467 buf = res!(Self::tuple_to_bytes::<u64, 6, {Self::TUP_U64_SERIES_START}>(a, buf));
468 },
469 Self::Tup7u64(a) => {
470 buf = res!(Self::tuple_to_bytes::<u64, 7, {Self::TUP_U64_SERIES_START}>(a, buf));
471 },
472 Self::Tup8u64(a) => {
473 buf = res!(Self::tuple_to_bytes::<u64, 8, {Self::TUP_U64_SERIES_START}>(a, buf));
474 },
475 Self::Tup9u64(a) => {
476 buf = res!(Self::tuple_to_bytes::<u64, 9, {Self::TUP_U64_SERIES_START}>(a, buf));
477 },
478 Self::Tup10u64(a) => {
479 buf = res!(Self::tuple_to_bytes::<u64, 10, {Self::TUP_U64_SERIES_START}>(a, buf));
480 },
481 Self::Tup2i8(a) => {
482 buf = res!(Self::tuple_to_bytes::<i8, 2, {Self::TUP_I8_SERIES_START}>(a, buf));
483 },
484 Self::Tup3i8(a) => {
485 buf = res!(Self::tuple_to_bytes::<i8, 3, {Self::TUP_I8_SERIES_START}>(a, buf));
486 },
487 Self::Tup4i8(a) => {
488 buf = res!(Self::tuple_to_bytes::<i8, 4, {Self::TUP_I8_SERIES_START}>(a, buf));
489 },
490 Self::Tup5i8(a) => {
491 buf = res!(Self::tuple_to_bytes::<i8, 5, {Self::TUP_I8_SERIES_START}>(a, buf));
492 },
493 Self::Tup6i8(a) => {
494 buf = res!(Self::tuple_to_bytes::<i8, 6, {Self::TUP_I8_SERIES_START}>(a, buf));
495 },
496 Self::Tup7i8(a) => {
497 buf = res!(Self::tuple_to_bytes::<i8, 7, {Self::TUP_I8_SERIES_START}>(a, buf));
498 },
499 Self::Tup8i8(a) => {
500 buf = res!(Self::tuple_to_bytes::<i8, 8, {Self::TUP_I8_SERIES_START}>(a, buf));
501 },
502 Self::Tup9i8(a) => {
503 buf = res!(Self::tuple_to_bytes::<i8, 9, {Self::TUP_I8_SERIES_START}>(a, buf));
504 },
505 Self::Tup10i8(a) => {
506 buf = res!(Self::tuple_to_bytes::<i8, 10, {Self::TUP_I8_SERIES_START}>(a, buf));
507 },
508 Self::Tup2i16(a) => {
509 buf = res!(Self::tuple_to_bytes::<i16, 2, {Self::TUP_I16_SERIES_START}>(a, buf));
510 },
511 Self::Tup3i16(a) => {
512 buf = res!(Self::tuple_to_bytes::<i16, 3, {Self::TUP_I16_SERIES_START}>(a, buf));
513 },
514 Self::Tup4i16(a) => {
515 buf = res!(Self::tuple_to_bytes::<i16, 4, {Self::TUP_I16_SERIES_START}>(a, buf));
516 },
517 Self::Tup5i16(a) => {
518 buf = res!(Self::tuple_to_bytes::<i16, 5, {Self::TUP_I16_SERIES_START}>(a, buf));
519 },
520 Self::Tup6i16(a) => {
521 buf = res!(Self::tuple_to_bytes::<i16, 6, {Self::TUP_I16_SERIES_START}>(a, buf));
522 },
523 Self::Tup7i16(a) => {
524 buf = res!(Self::tuple_to_bytes::<i16, 7, {Self::TUP_I16_SERIES_START}>(a, buf));
525 },
526 Self::Tup8i16(a) => {
527 buf = res!(Self::tuple_to_bytes::<i16, 8, {Self::TUP_I16_SERIES_START}>(a, buf));
528 },
529 Self::Tup9i16(a) => {
530 buf = res!(Self::tuple_to_bytes::<i16, 9, {Self::TUP_I16_SERIES_START}>(a, buf));
531 },
532 Self::Tup10i16(a) => {
533 buf = res!(Self::tuple_to_bytes::<i16, 10, {Self::TUP_I16_SERIES_START}>(a, buf));
534 },
535 Self::Tup2i32(a) => {
536 buf = res!(Self::tuple_to_bytes::<i32, 2, {Self::TUP_I32_SERIES_START}>(a, buf));
537 },
538 Self::Tup3i32(a) => {
539 buf = res!(Self::tuple_to_bytes::<i32, 3, {Self::TUP_I32_SERIES_START}>(a, buf));
540 },
541 Self::Tup4i32(a) => {
542 buf = res!(Self::tuple_to_bytes::<i32, 4, {Self::TUP_I32_SERIES_START}>(a, buf));
543 },
544 Self::Tup5i32(a) => {
545 buf = res!(Self::tuple_to_bytes::<i32, 5, {Self::TUP_I32_SERIES_START}>(a, buf));
546 },
547 Self::Tup6i32(a) => {
548 buf = res!(Self::tuple_to_bytes::<i32, 6, {Self::TUP_I32_SERIES_START}>(a, buf));
549 },
550 Self::Tup7i32(a) => {
551 buf = res!(Self::tuple_to_bytes::<i32, 7, {Self::TUP_I32_SERIES_START}>(a, buf));
552 },
553 Self::Tup8i32(a) => {
554 buf = res!(Self::tuple_to_bytes::<i32, 8, {Self::TUP_I32_SERIES_START}>(a, buf));
555 },
556 Self::Tup9i32(a) => {
557 buf = res!(Self::tuple_to_bytes::<i32, 9, {Self::TUP_I32_SERIES_START}>(a, buf));
558 },
559 Self::Tup10i32(a) => {
560 buf = res!(Self::tuple_to_bytes::<i32, 10, {Self::TUP_I32_SERIES_START}>(a, buf));
561 },
562 Self::Tup2i64(a) => {
563 buf = res!(Self::tuple_to_bytes::<i64, 2, {Self::TUP_I64_SERIES_START}>(a, buf));
564 },
565 Self::Tup3i64(a) => {
566 buf = res!(Self::tuple_to_bytes::<i64, 3, {Self::TUP_I64_SERIES_START}>(a, buf));
567 },
568 Self::Tup4i64(a) => {
569 buf = res!(Self::tuple_to_bytes::<i64, 4, {Self::TUP_I64_SERIES_START}>(a, buf));
570 },
571 Self::Tup5i64(a) => {
572 buf = res!(Self::tuple_to_bytes::<i64, 5, {Self::TUP_I64_SERIES_START}>(a, buf));
573 },
574 Self::Tup6i64(a) => {
575 buf = res!(Self::tuple_to_bytes::<i64, 6, {Self::TUP_I64_SERIES_START}>(a, buf));
576 },
577 Self::Tup7i64(a) => {
578 buf = res!(Self::tuple_to_bytes::<i64, 7, {Self::TUP_I64_SERIES_START}>(a, buf));
579 },
580 Self::Tup8i64(a) => {
581 buf = res!(Self::tuple_to_bytes::<i64, 8, {Self::TUP_I64_SERIES_START}>(a, buf));
582 },
583 Self::Tup9i64(a) => {
584 buf = res!(Self::tuple_to_bytes::<i64, 9, {Self::TUP_I64_SERIES_START}>(a, buf));
585 },
586 Self::Tup10i64(a) => {
587 buf = res!(Self::tuple_to_bytes::<i64, 10, {Self::TUP_I64_SERIES_START}>(a, buf));
588 },
589 }
590 Ok(buf)
591 }
592}
593
594impl Dat {
595
596 /// Appends the kind code to the given byte buffer.
597 fn append_code(&self, buf: &mut Vec<u8>) {
598 match self {
599 Self::Bool(b) => buf.push(if *b { Self::TRUE_CODE } else { Self::FALSE_CODE }),
600 Self::Opt(boxoptd) => {
601 match &**boxoptd {
602 None => buf.push(Self::OPT_NONE_CODE),
603 Some(_) => buf.push(Self::OPT_SOME_CODE),
604 }
605 },
606 _ => buf.push(self.to_code()),
607 }
608 }
609
610 pub fn as_bytes(&self) -> Outcome<Vec<u8>> {
611 self.to_bytes(Vec::new())
612 }
613
614 pub fn wrap_bytes_c64(mut byts: Vec<u8>) -> Outcome<Vec<u8>> {
615 let mut pre = Vec::new();
616 pre.push(Self::BC64_CODE);
617 pre = res!(Dat::C64(byts.len() as u64).to_bytes(pre));
618 pre.append(&mut byts);
619 Ok(pre)
620 }
621
622 pub fn wrap_bytes_u64(mut byts: Vec<u8>) -> Vec<u8> {
623 let mut pre = Vec::new();
624 pre.push(Self::BU64_CODE);
625 pre.extend_from_slice(&(byts.len() as u64).to_be_bytes());
626 pre.append(&mut byts);
627 pre
628 }
629
630 pub fn wrap_bytes_var(mut byts: Vec<u8>) -> Outcome<Vec<u8>> {
631 let mut pre = Vec::new();
632 let len = byts.len();
633 if len < u8::MAX as usize {
634 pre.push(Self::BU8_CODE);
635 pre.extend_from_slice(&(byts.len() as u8).to_be_bytes());
636 } else if len < u16::MAX as usize {
637 pre.push(Self::BU16_CODE);
638 pre.extend_from_slice(&(byts.len() as u16).to_be_bytes());
639 } else if len < u32::MAX as usize {
640 pre.push(Self::BU32_CODE);
641 pre.extend_from_slice(&(byts.len() as u32).to_be_bytes());
642 } else if len < u64::MAX as usize {
643 pre.push(Self::BU64_CODE);
644 pre.extend_from_slice(&(byts.len() as u64).to_be_bytes());
645 } else {
646 return Err(err!(
647 "The byte length of {}, which exceeds the maximum u64::MAX = {},
648 cannot be represented a variable size Dat byte wrapper.",
649 len, u64::MAX;
650 Size, TooBig));
651 }
652 pre.append(&mut byts);
653 Ok(pre)
654 }
655
656 pub fn byte_wrapper_var_len(len: usize) -> Outcome<u8> {
657 if len <= u8::MAX as usize { Ok(2) }
658 else if len <= u16::MAX as usize { Ok(3) }
659 else if len <= u32::MAX as usize { Ok(5) }
660 else if len <= u64::MAX as usize { Ok(9) }
661 else {
662 Err(err!(
663 "The byte length of {}, which exceeds the maximum u64::MAX = {},
664 cannot be represented a variable size Dat byte wrapper.",
665 len, u64::MAX;
666 Size, TooBig))
667 }
668 }
669
670 pub fn wrap_dat(byts: Vec<u8>) -> Self {
671 let len = byts.len();
672 if len < u8::MAX as usize {
673 Dat::BU8(byts)
674 } else if len < u16::MAX as usize {
675 Dat::BU16(byts)
676 } else if len < u32::MAX as usize {
677 Dat::BU32(byts)
678 } else {
679 Dat::BU64(byts)
680 }
681 }
682
683 pub fn vec_to_bytes(v: &Vec<Dat>, mut buf: Vec<u8>) -> Outcome<Vec<u8>> {
684 let mut buf2 = Vec::new();
685 for item in v {
686 buf2 = res!(item.to_bytes(buf2));
687 }
688 buf = res!(Dat::C64(buf2.len() as u64).to_bytes(buf));
689 buf.append(&mut buf2);
690 Ok(buf)
691 }
692
693 pub fn tuple_to_bytes<
694 T: ToBytes,
695 const N: usize,
696 const C: u8,
697 >(
698 //v: &[Dat; N],
699 v: &[T; N],
700 mut buf: Vec<u8>,
701 )
702 -> Outcome<Vec<u8>>
703 {
704 let mut buf2 = Vec::new();
705 for item in v {
706 buf2 = res!(item.to_bytes(buf2));
707 }
708 buf.push(C + N as u8 - 2);
709 buf.append(&mut buf2);
710 Ok(buf)
711 }
712}