1use super::*;
2
3
4#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
5pub struct TaggedCBOR {
6 tag: BigNum,
7 value: CBORValue,
8}
9
10to_from_bytes!(TaggedCBOR);
11
12
13impl TaggedCBOR {
14 pub fn tag(&self) -> BigNum {
15 self.tag
16 }
17
18 pub fn value(&self) -> CBORValue {
19 self.value.clone()
20 }
21
22 pub fn new(tag: BigNum, value: &CBORValue) -> Self {
23 Self {
24 tag,
25 value: value.clone(),
26 }
27 }
28}
29
30
31#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
32pub struct CBORArray {
33 definite: bool,
34 pub (crate) values: Vec<CBORValue>,
35}
36
37to_from_bytes!(CBORArray);
38
39
40impl CBORArray {
41 pub fn new() -> Self {
42 Self {
43 definite: true,
44 values: Vec::new(),
45 }
46 }
47
48 pub fn len(&self) -> usize {
49 self.values.len()
50 }
51
52 pub fn get(&self, index: usize) -> CBORValue {
53 self.values[index].clone()
54 }
55
56 pub fn add(&mut self, elem: &CBORValue) {
57 self.values.push(elem.clone());
58 }
59
60 pub fn set_definite_encoding(&mut self, use_definite: bool) {
63 self.definite = use_definite
64 }
65
66 pub fn is_definite(&self) -> bool {
69 self.definite
70 }
71}
72
73impl From<Vec<CBORValue>> for CBORArray {
74 fn from(vec: Vec<CBORValue>) -> Self {
75 Self {
76 definite: true,
77 values: vec,
78 }
79 }
80}
81
82
83#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
84pub struct CBORObject {
85 definite: bool,
86 values: LinkedHashMap<CBORValue, CBORValue>,
87}
88
89to_from_bytes!(CBORObject);
90
91
92impl CBORObject {
93 pub fn new() -> Self {
94 Self {
95 definite: true,
96 values: LinkedHashMap::new(),
97 }
98 }
99
100 pub fn len(&self) -> usize {
101 self.values.len()
102 }
103
104 pub fn insert(&mut self, key: &CBORValue, value: &CBORValue) -> Option<CBORValue> {
105 self.values.insert(key.clone(), value.clone())
106 }
107
108 pub fn get(&self, key: &CBORValue) -> Option<CBORValue> {
109 self.values.get(key).map(|v| v.clone())
110 }
111
112 pub fn keys(&self) -> CBORArray {
113 self.values.iter().map(|(k, _v)| k.clone()).collect::<Vec<CBORValue>>().into()
114 }
115
116 pub fn set_definite_encoding(&mut self, use_definite: bool) {
119 self.definite = use_definite
120 }
121
122 pub fn is_definite(&self) -> bool {
125 self.definite
126 }
127}
128
129
130#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
131pub enum CBORSpecialType {
132 Bool,
133 Float,
134 Unassigned,
135 Break,
136 Undefined,
137 Null,
138}
139
140#[derive(Clone, Debug, PartialEq, PartialOrd)]
141enum CBORSpecialEnum {
142 Bool(bool),
143 Float(f64),
144 Unassigned(u8),
145 Break,
146 Undefined,
147 Null,
148}
149
150
151#[derive(Clone, Debug)]
152pub struct CBORSpecial(CBORSpecialEnum);
153
154to_from_bytes!(CBORSpecial);
155
156
157impl CBORSpecial {
158 pub fn new_bool(b: bool) -> Self {
159 Self(CBORSpecialEnum::Bool(b))
160 }
161
162 pub fn new_unassigned(u: u8) -> Self {
171 Self(CBORSpecialEnum::Unassigned(u))
172 }
173
174 pub fn new_break() -> Self {
179 Self(CBORSpecialEnum::Break)
180 }
181
182 pub fn new_null() -> Self {
183 Self(CBORSpecialEnum::Null)
184 }
185
186 pub fn new_undefined() -> Self {
187 Self(CBORSpecialEnum::Undefined)
188 }
189
190 pub fn kind(&self) -> CBORSpecialType {
191 match &self.0 {
192 CBORSpecialEnum::Bool(_) => CBORSpecialType::Bool,
193 CBORSpecialEnum::Float(_) => CBORSpecialType::Float,
194 CBORSpecialEnum::Unassigned(_) => CBORSpecialType::Unassigned,
195 CBORSpecialEnum::Break => CBORSpecialType::Break,
196 CBORSpecialEnum::Undefined => CBORSpecialType::Undefined,
197 CBORSpecialEnum::Null => CBORSpecialType::Null,
198 }
199 }
200
201 pub fn as_bool(&self) -> Option<bool> {
202 match &self.0 {
203 CBORSpecialEnum::Bool(b) => Some(*b),
204 _ => None,
205 }
206 }
207
208 pub fn as_float(&self) -> Option<f64> {
209 match &self.0 {
210 CBORSpecialEnum::Float(f) => Some(*f),
211 _ => None,
212 }
213 }
214
215 pub fn as_unassigned(&self) -> Option<u8> {
216 match &self.0 {
217 CBORSpecialEnum::Unassigned(u) => Some(*u),
218 _ => None,
219 }
220 }
221}
222
223fn f64_to_bytes(f: f64) -> [u8; std::mem::size_of::<f64>()] {
227 use byteorder::{BigEndian, WriteBytesExt};
228
229 let mut bytes = [0u8; std::mem::size_of::<f64>()];
230 bytes.as_mut().write_f64::<BigEndian>(f).unwrap();
231 bytes
232}
233
234impl PartialEq for CBORSpecial {
235 fn eq(&self, other: &Self) -> bool {
236 match (&self.0, &other.0) {
237 (CBORSpecialEnum::Bool(b1), CBORSpecialEnum::Bool(b2)) => *b1 == *b2,
238 (CBORSpecialEnum::Float(f1), CBORSpecialEnum::Float(f2)) => f64_to_bytes(*f1) == f64_to_bytes(*f2),
239 (CBORSpecialEnum::Unassigned(u1), CBORSpecialEnum::Unassigned(u2)) => *u1 == *u2,
240 (CBORSpecialEnum::Break, CBORSpecialEnum::Break) |
241 (CBORSpecialEnum::Undefined, CBORSpecialEnum::Undefined) |
242 (CBORSpecialEnum::Null, CBORSpecialEnum::Null) => true,
243 _mixed_types => false,
244 }
245 }
246}
247
248impl Eq for CBORSpecial {
249}
250
251impl Ord for CBORSpecial {
252 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
253 match (&self.0, &other.0) {
254 (CBORSpecialEnum::Bool(b1), CBORSpecialEnum::Bool(b2)) => b1.cmp(b2),
255 (CBORSpecialEnum::Float(f1), CBORSpecialEnum::Float(f2)) => f64_to_bytes(*f1).cmp(&f64_to_bytes(*f2)),
256 (CBORSpecialEnum::Unassigned(u1), CBORSpecialEnum::Unassigned(u2)) => u1.cmp(u2),
257 (CBORSpecialEnum::Break, CBORSpecialEnum::Break) |
258 (CBORSpecialEnum::Undefined, CBORSpecialEnum::Undefined) |
259 (CBORSpecialEnum::Null, CBORSpecialEnum::Null) => std::cmp::Ordering::Equal,
260 _mixed_types => self.kind().cmp(&other.kind()),
261 }
262 }
263}
264
265impl std::hash::Hash for CBORSpecial {
266 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
267 self.kind().hash(state);
268 match &self.0 {
269 CBORSpecialEnum::Bool(b) => b.hash(state),
270 CBORSpecialEnum::Float(f) => f64_to_bytes(*f).hash(state),
271 CBORSpecialEnum::Unassigned(u) => u.hash(state),
272 _no_extra_data => (),
273 }
274 }
275}
276
277impl PartialOrd for CBORSpecial {
278 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
279 Some(self.cmp(other))
280 }
281}
282
283
284#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
285pub enum CBORValueKind {
286 Int,
287 Bytes,
288 Text,
289 Array,
290 Object,
291 TaggedCBOR,
292 Special,
293}
294
295#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
296pub enum CBORValueEnum {
297 Int(Int),
298 Bytes(Vec<u8>),
299 Text(String),
300 Array(CBORArray),
301 Object(CBORObject),
302 TaggedCBOR(Box<TaggedCBOR>),
303 Special(CBORSpecial),
304}
305
306
307#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
308pub struct CBORValue(pub (crate) CBORValueEnum);
309
310to_from_bytes!(CBORValue);
311
312
313impl CBORValue {
314 pub fn new_int(int: &Int) -> Self {
315 Self(CBORValueEnum::Int(int.clone()))
316 }
317
318 pub fn new_bytes(bytes: Vec<u8>) -> Self {
319 Self(CBORValueEnum::Bytes(bytes))
320 }
321
322 pub fn new_text(text: String) -> Self {
323 Self(CBORValueEnum::Text(text))
324 }
325
326 pub fn new_array(arr: &CBORArray) -> Self {
327 Self(CBORValueEnum::Array(arr.clone()))
328 }
329
330 pub fn new_object(obj: &CBORObject) -> Self {
331 Self(CBORValueEnum::Object(obj.clone()))
332 }
333
334 pub fn new_tagged(tagged: &TaggedCBOR) -> Self {
335 Self(CBORValueEnum::TaggedCBOR(Box::new(tagged.clone())))
336 }
337
338 pub fn new_special(special: &CBORSpecial) -> Self {
339 Self(CBORValueEnum::Special(special.clone()))
340 }
341
342 pub fn from_label(label: &Label) -> Self {
343 match &label.0 {
344 LabelEnum::Int(x) => Self::new_int(x),
345 LabelEnum::Text(x) => Self::new_text(x.clone()),
346 }
347 }
348
349 pub fn kind(&self) -> CBORValueKind {
350 match &self.0 {
351 CBORValueEnum::Int(_) => CBORValueKind::Int,
352 CBORValueEnum::Bytes(_) => CBORValueKind::Bytes,
353 CBORValueEnum::Text(_) => CBORValueKind::Text,
354 CBORValueEnum::Array(_) => CBORValueKind::Array,
355 CBORValueEnum::Object(_) => CBORValueKind::Object,
356 CBORValueEnum::TaggedCBOR(_) => CBORValueKind::TaggedCBOR,
357 CBORValueEnum::Special(_) => CBORValueKind::Special,
358 }
359 }
360
361 pub fn as_int(&self) -> Option<Int> {
362 match &self.0 {
363 CBORValueEnum::Int(x) => Some(x.clone()),
364 _ => None,
365 }
366 }
367
368 pub fn as_bytes(&self) -> Option<Vec<u8>> {
369 match &self.0 {
370 CBORValueEnum::Bytes(x) => Some(x.clone()),
371 _ => None,
372 }
373 }
374
375 pub fn as_text(&self) -> Option<String> {
376 match &self.0 {
377 CBORValueEnum::Text(x) => Some(x.clone()),
378 _ => None,
379 }
380 }
381
382 pub fn as_array(&self) -> Option<CBORArray> {
383 match &self.0 {
384 CBORValueEnum::Array(x) => Some(x.clone()),
385 _ => None,
386 }
387 }
388
389 pub fn as_object(&self) -> Option<CBORObject> {
390 match &self.0 {
391 CBORValueEnum::Object(x) => Some(x.clone()),
392 _ => None,
393 }
394 }
395
396 pub fn as_tagged(&self) -> Option<TaggedCBOR> {
397 use std::ops::Deref;
398 match &self.0 {
399 CBORValueEnum::TaggedCBOR(x) => Some((*x).deref().clone()),
400 _ => None,
401 }
402 }
403
404 pub fn as_special(&self) -> Option<CBORSpecial> {
405 match &self.0 {
406 CBORValueEnum::Special(x) => Some(x.clone()),
407 _ => None,
408 }
409 }
410}
411
412
413use std::io::{Seek, SeekFrom};
416
417impl cbor_event::se::Serialize for TaggedCBOR {
418 fn serialize<'se, W: Write>(&self, serializer: &'se mut Serializer<W>) -> cbor_event::Result<&'se mut Serializer<W>> {
419 serializer.write_array(cbor_event::Len::Len(2))?;
420 self.tag.serialize(serializer)?;
421 self.value.serialize(serializer)?;
422 Ok(serializer)
423 }
424}
425
426impl Deserialize for TaggedCBOR {
427 fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
428 (|| -> Result<_, DeserializeError> {
429 let len = raw.array()?;
430 let mut read_len = CBORReadLen::new(len);
431 read_len.read_elems(2)?;
432 let tag = (|| -> Result<_, DeserializeError> {
433 Ok(BigNum::deserialize(raw)?)
434 })().map_err(|e| e.annotate("tag"))?;
435 let value = (|| -> Result<_, DeserializeError> {
436 Ok(CBORValue::deserialize(raw)?)
437 })().map_err(|e| e.annotate("value"))?;
438 match len {
439 cbor_event::Len::Len(_) => (),
440 cbor_event::Len::Indefinite => match raw.special()? {
441 cbor_event::Special::Break => (),
442 _ => return Err(DeserializeFailure::EndingBreakMissing.into()),
443 },
444 }
445 Ok(TaggedCBOR {
446 tag,
447 value,
448 })
449 })().map_err(|e| e.annotate("TaggedCBOR"))
450 }
451}
452
453impl cbor_event::se::Serialize for CBORArray {
454 fn serialize<'se, W: Write>(&self, serializer: &'se mut Serializer<W>) -> cbor_event::Result<&'se mut Serializer<W>> {
455 if self.definite {
456 serializer.write_array(cbor_event::Len::Len(self.values.len() as u64))?;
457 } else {
458 serializer.write_array(cbor_event::Len::Indefinite)?;
459 }
460 for element in &self.values {
461 element.serialize(serializer)?;
462 }
463 if !self.definite {
464 serializer.write_special(cbor_event::Special::Break)?;
465 }
466 Ok(serializer)
467 }
468}
469
470impl Deserialize for CBORArray {
471 fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
472 let mut arr = Vec::new();
473 let definite = (|| -> Result<_, DeserializeError> {
474 let len = raw.array()?;
475 let definite = len != cbor_event::Len::Indefinite;
476 while match len { cbor_event::Len::Len(n) => arr.len() < n as usize, cbor_event::Len::Indefinite => true, } {
477 let elem = CBORValue::deserialize(raw)?;
478 if !definite && elem.0 == CBORValueEnum::Special(CBORSpecial(CBORSpecialEnum::Break)) {
479 break;
480 }
481 arr.push(elem);
482 }
483 Ok(definite)
484 })().map_err(|e| e.annotate("CBORArray"))?;
485 Ok(Self {
486 definite,
487 values: arr,
488 })
489 }
490}
491
492impl cbor_event::se::Serialize for CBORObject {
493 fn serialize<'se, W: Write>(&self, serializer: &'se mut Serializer<W>) -> cbor_event::Result<&'se mut Serializer<W>> {
494 if self.definite {
495 serializer.write_map(cbor_event::Len::Len(self.values.len() as u64))?;
496 } else {
497 serializer.write_map(cbor_event::Len::Indefinite)?;
498 }
499 for (key, value) in &self.values {
500 key.serialize(serializer)?;
501 value.serialize(serializer)?;
502 }
503 if !self.definite {
504 serializer.write_special(cbor_event::Special::Break)?;
505 }
506 Ok(serializer)
507 }
508}
509
510impl Deserialize for CBORObject {
511 fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
512 let mut table = LinkedHashMap::new();
513 let definite = (|| -> Result<_, DeserializeError> {
514 let len = raw.map()?;
515 let definite = len != cbor_event::Len::Indefinite;
516 while match len { cbor_event::Len::Len(n) => table.len() < n as usize, cbor_event::Len::Indefinite => true, } {
517 if raw.cbor_type()? == cbor_event::Type::Special {
518 assert_eq!(raw.special()?, cbor_event::Special::Break);
519 break;
520 }
521 let key = CBORValue::deserialize(raw)?;
522 let value = CBORValue::deserialize(raw)?;
523 if table.insert(key.clone(), value).is_some() {
524 return Err(DeserializeFailure::DuplicateKey(Key::Str(String::from("some complicated/unsupported type"))).into());
525 }
526 }
527 Ok(definite)
528 })().map_err(|e| e.annotate("CBORObject"))?;
529 Ok(Self {
530 definite,
531 values: table,
532 })
533 }
534}
535
536impl cbor_event::se::Serialize for CBORSpecialEnum {
537 fn serialize<'se, W: Write>(&self, serializer: &'se mut Serializer<W>) -> cbor_event::Result<&'se mut Serializer<W>> {
538 let special = match self {
539 CBORSpecialEnum::Bool(b) => cbor_event::Special::Bool(*b),
540 CBORSpecialEnum::Float(_f) => {
541 return Err(cbor_event::Error::CustomError(String::from("float serialization not supports by cbor_event")));
547 },
548 CBORSpecialEnum::Unassigned(u) => cbor_event::Special::Unassigned(*u),
549 CBORSpecialEnum::Break => cbor_event::Special::Break,
550 CBORSpecialEnum::Undefined => cbor_event::Special::Undefined,
551 CBORSpecialEnum::Null => cbor_event::Special::Null,
552 };
553 serializer.write_special(special)
554 }
555}
556
557impl Deserialize for CBORSpecialEnum {
558 fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
559 (|| -> Result<_, DeserializeError> {
560 Ok(match raw.special()? {
561 cbor_event::Special::Bool(b) => CBORSpecialEnum::Bool(b),
562 cbor_event::Special::Float(f) => CBORSpecialEnum::Float(f),
563 cbor_event::Special::Unassigned(u) => CBORSpecialEnum::Unassigned(u),
564 cbor_event::Special::Break => CBORSpecialEnum::Break,
565 cbor_event::Special::Undefined => CBORSpecialEnum::Undefined,
566 cbor_event::Special::Null => CBORSpecialEnum::Null,
567 })
568 })().map_err(|e| e.annotate("CBORSpecialEnum"))
569 }
570}
571
572impl cbor_event::se::Serialize for CBORSpecial {
573 fn serialize<'se, W: Write>(&self, serializer: &'se mut Serializer<W>) -> cbor_event::Result<&'se mut Serializer<W>> {
574 self.0.serialize(serializer)
575 }
576}
577
578impl Deserialize for CBORSpecial {
579 fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
580 Ok(Self(CBORSpecialEnum::deserialize(raw)?))
581 }
582}
583
584impl cbor_event::se::Serialize for CBORValueEnum {
585 fn serialize<'se, W: Write>(&self, serializer: &'se mut Serializer<W>) -> cbor_event::Result<&'se mut Serializer<W>> {
586 match self {
587 CBORValueEnum::Int(x) => {
588 x.serialize(serializer)
589 },
590 CBORValueEnum::Bytes(x) => {
591 serializer.write_bytes(&x)
592 },
593 CBORValueEnum::Text(x) => {
594 serializer.write_text(&x)
595 },
596 CBORValueEnum::Array(x) => {
597 x.serialize(serializer)
598 },
599 CBORValueEnum::Object(x) => {
600 x.serialize(serializer)
601 },
602 CBORValueEnum::TaggedCBOR(x) => {
603 x.serialize(serializer)
604 },
605 CBORValueEnum::Special(x) => {
606 x.serialize(serializer)
607 },
608 }
609 }
610}
611
612impl Deserialize for CBORValueEnum {
613 fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
614 (|| -> Result<_, DeserializeError> {
615 let initial_position = raw.as_mut_ref().seek(SeekFrom::Current(0)).unwrap();
616 match (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError> {
617 Ok(Int::deserialize(raw)?)
618 })(raw)
619 {
620 Ok(variant) => return Ok(CBORValueEnum::Int(variant)),
621 Err(_) => raw.as_mut_ref().seek(SeekFrom::Start(initial_position)).unwrap(),
622 };
623 match (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError> {
624 Ok(raw.bytes()?)
625 })(raw)
626 {
627 Ok(variant) => return Ok(CBORValueEnum::Bytes(variant)),
628 Err(_) => raw.as_mut_ref().seek(SeekFrom::Start(initial_position)).unwrap(),
629 };
630 match (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError> {
631 Ok(String::deserialize(raw)?)
632 })(raw)
633 {
634 Ok(variant) => return Ok(CBORValueEnum::Text(variant)),
635 Err(_) => raw.as_mut_ref().seek(SeekFrom::Start(initial_position)).unwrap(),
636 };
637 match (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError> {
638 Ok(CBORArray::deserialize(raw)?)
639 })(raw)
640 {
641 Ok(variant) => return Ok(CBORValueEnum::Array(variant)),
642 Err(_) => raw.as_mut_ref().seek(SeekFrom::Start(initial_position)).unwrap(),
643 };
644 match (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError> {
645 Ok(CBORObject::deserialize(raw)?)
646 })(raw)
647 {
648 Ok(variant) => return Ok(CBORValueEnum::Object(variant)),
649 Err(_) => raw.as_mut_ref().seek(SeekFrom::Start(initial_position)).unwrap(),
650 };
651 match (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError> {
652 Ok(TaggedCBOR::deserialize(raw)?)
653 })(raw)
654 {
655 Ok(variant) => return Ok(CBORValueEnum::TaggedCBOR(Box::new(variant))),
656 Err(_) => raw.as_mut_ref().seek(SeekFrom::Start(initial_position)).unwrap(),
657 };
658 match (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError> {
659 Ok(CBORSpecial::deserialize(raw)?)
660 })(raw)
661 {
662 Ok(variant) => return Ok(CBORValueEnum::Special(variant)),
663 Err(_) => raw.as_mut_ref().seek(SeekFrom::Start(initial_position)).unwrap(),
664 };
665 Err(DeserializeError::new("CBORValueEnum", DeserializeFailure::NoVariantMatched.into()))
666 })().map_err(|e| e.annotate("CBORValueEnum"))
667 }
668}
669
670impl cbor_event::se::Serialize for CBORValue {
671 fn serialize<'se, W: Write>(&self, serializer: &'se mut Serializer<W>) -> cbor_event::Result<&'se mut Serializer<W>> {
672 self.0.serialize(serializer)
673 }
674}
675
676impl Deserialize for CBORValue {
677 fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
678 Ok(Self(CBORValueEnum::deserialize(raw)?))
679 }
680}
681
682#[cfg(test)]
683mod tests {
684 use super::*;
685
686 fn ce_value_to_bytes(val: &cbor_event::Value) -> Vec<u8> {
687 let mut buf = Serializer::new_vec();
688 val.serialize(&mut buf).unwrap();
689 buf.finalize()
690 }
691
692 #[test]
693 fn cbor_special_array() {
694 type CES = cbor_event::Special;
695 type CEV = cbor_event::Value;
696
697 let ce_vals = vec![
698 CEV::Special(CES::Bool(true)),
699 CEV::Special(CES::Undefined),
700 CEV::Special(CES::Unassigned(4u8)),
701 CEV::Special(CES::Null),
702 ];
704
705 let mut vals = CBORArray::new();
706 vals.add(&CBORValue::new_special(&CBORSpecial::new_bool(true)));
707 vals.add(&CBORValue::new_special(&CBORSpecial::new_undefined()));
708 vals.add(&CBORValue::new_special(&CBORSpecial::new_unassigned(4u8)));
709 vals.add(&CBORValue::new_special(&CBORSpecial::new_null()));
710 let ce_spec_arr_def = CEV::Array(ce_vals.clone());
714 let spec_arr_def = CBORValue::new_array(&vals);
715
716 let ce_bytes_def = ce_value_to_bytes(&ce_spec_arr_def);
717 assert_eq!(ce_bytes_def, spec_arr_def.to_bytes());
718
719 let spec_arr_from_ce_def = CBORValue::from_bytes(ce_bytes_def).unwrap();
720 assert_eq!(spec_arr_def, spec_arr_from_ce_def);
721
722 let ce_spec_arr_indef = CEV::IArray(ce_vals);
724 vals.set_definite_encoding(false);
725 let spec_arr_indef = CBORValue::new_array(&vals);
726
727 let ce_bytes_indef = ce_value_to_bytes(&ce_spec_arr_indef);
728 assert_eq!(ce_bytes_indef, spec_arr_indef.to_bytes());
729
730 let spec_arr_from_ce_indef = CBORValue::from_bytes(ce_bytes_indef).unwrap();
731 assert_eq!(spec_arr_indef, spec_arr_from_ce_indef);
732 }
733
734 fn cbor_other_object() {
735 type CES = cbor_event::Special;
736 type CEV = cbor_event::Value;
737 type CEK = cbor_event::ObjectKey;
738
739 let mut ceo = std::collections::BTreeMap::new();
740 ceo.insert(CEK::Bytes(vec![8u8, 7u8, 6u8, 5u8, 4u8, 3u8, 2u8, 1u8, 0u8]), CEV::U64(322u64));
741 ceo.insert(CEK::Text(String::from("some_string_key")), CEV::Tag(100u64, Box::new(CEV::I64(-7))));
742
743 let mut vals = CBORObject::new();
744 vals.insert(&CBORValue::new_bytes(vec![8u8, 7u8, 6u8, 5u8, 4u8, 3u8, 2u8, 1u8, 0u8]), &CBORValue::new_int(&Int::new_i32(322)));
745 vals.insert(&CBORValue::new_text(String::from("some_string_key")), &CBORValue::new_tagged(&TaggedCBOR::new(to_bignum(100u64), &CBORValue::new_int(&Int::new_i32(-7)))));
746
747 let ce_obj_def = CEV::Object(ceo.clone());
749 let obj_def = CBORValue::new_object(&vals);
750
751 let ce_bytes_def = ce_value_to_bytes(&ce_obj_def);
752 assert_eq!(ce_bytes_def, obj_def.to_bytes());
753
754 let obj_from_ce_def = CBORValue::from_bytes(ce_bytes_def).unwrap();
755 assert_eq!(obj_def, obj_from_ce_def);
756
757 let ce_obj_indef = CEV::IObject(ceo);
759 vals.set_definite_encoding(false);
760 let obj_indef = CBORValue::new_object(&vals);
761
762 let ce_bytes_indef = ce_value_to_bytes(&ce_obj_indef);
763 assert_eq!(ce_bytes_indef, obj_indef.to_bytes());
764
765 let obj_from_ce_indef = CBORValue::from_bytes(ce_bytes_indef).unwrap();
766 assert_eq!(obj_indef, obj_from_ce_indef);
767 }
768}