@@ -32,20 +32,35 @@ pub enum CanonicalizationScheme {
32
32
33
33
#[ cfg( feature = "std" ) ]
34
34
impl CanonicalizationScheme {
35
- /// Does this canonicalisation scheme require sorting of keys.
35
+ /// Does this canonicalization scheme require sorting of keys.
36
36
pub fn is_sorting ( & self ) -> bool {
37
37
matches ! ( self , Self :: Rfc7049 | Self :: Rfc8049 )
38
38
}
39
-
40
- // pub fn key<K: serde::Serialize>(&self, key: &K) -> Result<Vec<u8>, Error<std::io::Error>> {
41
- // let mut buffer = Vec::new();
42
- // let mut serializer = Serializer::new(&mut buffer, true);
43
- // key.serialize(&mut serializer)?;
44
- // Ok(buffer)
45
- // }
46
39
}
47
40
48
41
/// A serializer for CBOR.
42
+ ///
43
+ /// # Example
44
+ /// ```rust
45
+ /// use serde::Serialize;
46
+ /// use ciborium::Serializer;
47
+ /// use ciborium::ser::CanonicalizationScheme;
48
+ ///
49
+ /// #[derive(serde::Serialize)]
50
+ /// struct Example {
51
+ /// a: u64,
52
+ /// aa: u64,
53
+ /// b: u64,
54
+ /// }
55
+ ///
56
+ /// let example = Example { a: 42, aa: 420, b: 4200 };
57
+ ///
58
+ /// let mut buffer = Vec::with_capacity(1024);
59
+ /// let mut serializer = Serializer::new(&mut buffer, CanonicalizationScheme::Rfc8049);
60
+ /// example.serialize(&mut serializer).unwrap();
61
+ ///
62
+ /// assert_eq!(hex::encode(&buffer), "a36161182a61621910686261611901a4");
63
+ /// ```
49
64
pub struct Serializer < W > {
50
65
encoder : Encoder < W > ,
51
66
@@ -647,6 +662,23 @@ where
647
662
}
648
663
649
664
/// Serializes as CBOR into `Vec<u8>`.
665
+ ///
666
+ /// # Example
667
+ /// ```rust
668
+ /// use ciborium::to_vec;
669
+ ///
670
+ /// #[derive(serde::Serialize)]
671
+ /// struct Example {
672
+ /// a: u64,
673
+ /// aa: u64,
674
+ /// b: u64,
675
+ /// }
676
+ ///
677
+ /// let example = Example { a: 42, aa: 420, b: 4200 };
678
+ /// let bytes = to_vec(&example).unwrap();
679
+ ///
680
+ /// assert_eq!(hex::encode(&bytes), "a36161182a6261611901a46162191068");
681
+ /// ```
650
682
#[ cfg( feature = "std" ) ]
651
683
#[ inline]
652
684
pub fn to_vec < T : ?Sized + ser:: Serialize > ( value : & T ) -> Result < Vec < u8 > , Error < std:: io:: Error > > {
@@ -657,6 +689,24 @@ pub fn to_vec<T: ?Sized + ser::Serialize>(value: &T) -> Result<Vec<u8>, Error<st
657
689
}
658
690
659
691
/// Canonically serializes as CBOR into `Vec<u8>` with a specified [CanonicalizationScheme].
692
+ ///
693
+ /// # Example
694
+ /// ```rust
695
+ /// use ciborium::to_vec_canonical;
696
+ /// use ciborium::ser::CanonicalizationScheme;
697
+ ///
698
+ /// #[derive(serde::Serialize)]
699
+ /// struct Example {
700
+ /// a: u64,
701
+ /// aa: u64,
702
+ /// b: u64,
703
+ /// }
704
+ ///
705
+ /// let example = Example { a: 42, aa: 420, b: 4200 };
706
+ /// let bytes = to_vec_canonical(&example, CanonicalizationScheme::Rfc8049).unwrap();
707
+ ///
708
+ /// assert_eq!(hex::encode(&bytes), "a36161182a61621910686261611901a4");
709
+ /// ```
660
710
#[ cfg( feature = "std" ) ]
661
711
#[ inline]
662
712
pub fn to_vec_canonical < T : ?Sized + ser:: Serialize > ( value : & T , scheme : CanonicalizationScheme ) -> Result < Vec < u8 > , Error < std:: io:: Error > > {
@@ -667,6 +717,25 @@ pub fn to_vec_canonical<T: ?Sized + ser::Serialize>(value: &T, scheme: Canonical
667
717
}
668
718
669
719
/// Serializes as CBOR into a type with [`impl ciborium_io::Write`](ciborium_io::Write)
720
+ ///
721
+ /// # Example
722
+ /// ```rust
723
+ /// use ciborium::into_writer;
724
+ ///
725
+ /// #[derive(serde::Serialize)]
726
+ /// struct Example {
727
+ /// a: u64,
728
+ /// aa: u64,
729
+ /// b: u64,
730
+ /// }
731
+ ///
732
+ /// let example = Example { a: 42, aa: 420, b: 4200 };
733
+ ///
734
+ /// let mut bytes = Vec::new();
735
+ /// into_writer(&example, &mut bytes).unwrap();
736
+ ///
737
+ /// assert_eq!(hex::encode(&bytes), "a36161182a6261611901a46162191068");
738
+ /// ```
670
739
#[ inline]
671
740
pub fn into_writer < T : ?Sized + ser:: Serialize , W : Write > (
672
741
value : & T ,
@@ -682,6 +751,26 @@ where
682
751
/// Canonically serializes as CBOR into a type with [`impl ciborium_io::Write`](ciborium_io::Write)
683
752
///
684
753
/// This will sort map keys in output according to a specified [CanonicalizationScheme].
754
+ ///
755
+ /// # Example
756
+ /// ```rust
757
+ /// use ciborium::into_writer_canonical;
758
+ /// use ciborium::ser::CanonicalizationScheme;
759
+ ///
760
+ /// #[derive(serde::Serialize)]
761
+ /// struct Example {
762
+ /// a: u64,
763
+ /// aa: u64,
764
+ /// b: u64,
765
+ /// }
766
+ ///
767
+ /// let example = Example { a: 42, aa: 420, b: 4200 };
768
+ ///
769
+ /// let mut bytes = Vec::new();
770
+ /// into_writer_canonical(&example, &mut bytes, CanonicalizationScheme::Rfc8049).unwrap();
771
+ ///
772
+ /// assert_eq!(hex::encode(&bytes), "a36161182a61621910686261611901a4");
773
+ /// ```
685
774
#[ cfg( feature = "std" ) ]
686
775
#[ inline]
687
776
pub fn into_writer_canonical < T : ?Sized + ser:: Serialize , W : Write > (
0 commit comments