Skip to content

Commit 042f53d

Browse files
committed
feat: rfc8949 and rfc7049 canonicalized output
Signed-off-by: Liam Gray <[email protected]>
1 parent 7c6ba83 commit 042f53d

File tree

4 files changed

+605
-86
lines changed

4 files changed

+605
-86
lines changed

ciborium/src/de/mod.rs

+55
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,24 @@ where
786786
/// If you want to deserialize faster at the cost of more memory, consider using
787787
/// [`from_reader_with_buffer`](from_reader_with_buffer) with a larger buffer,
788788
/// for example 64KB.
789+
///
790+
/// # Example
791+
/// ```rust
792+
/// use ciborium::from_reader;
793+
///
794+
/// #[derive(Debug, serde::Deserialize, Eq, PartialEq)]
795+
/// struct Example {
796+
/// a: u64,
797+
/// aa: u64,
798+
/// b: u64,
799+
/// }
800+
///
801+
/// let cbor = hex::decode("a36161182a61621910686261611901a4").unwrap();
802+
/// let expected = Example { a: 42, aa: 420, b: 4200 };
803+
///
804+
/// let deserialized: Example = from_reader(cbor.as_slice()).unwrap();
805+
/// assert_eq!(deserialized, expected);
806+
/// ```
789807
#[inline]
790808
pub fn from_reader<T: de::DeserializeOwned, R: Read>(reader: R) -> Result<T, Error<R::Error>>
791809
where
@@ -798,6 +816,25 @@ where
798816
/// Deserializes as CBOR from a type with [`impl
799817
/// ciborium_io::Read`](ciborium_io::Read), using a caller-specific buffer as a
800818
/// temporary scratch space.
819+
///
820+
/// # Example
821+
/// ```rust
822+
/// use ciborium::from_reader_with_buffer;
823+
///
824+
/// #[derive(Debug, serde::Deserialize, Eq, PartialEq)]
825+
/// struct Example {
826+
/// a: u64,
827+
/// aa: u64,
828+
/// b: u64,
829+
/// }
830+
///
831+
/// let cbor = hex::decode("a36161182a61621910686261611901a4").unwrap();
832+
/// let expected = Example { a: 42, aa: 420, b: 4200 };
833+
///
834+
/// let mut scratch = [0; 8192];
835+
/// let deserialized: Example = from_reader_with_buffer(cbor.as_slice(), &mut scratch).unwrap();
836+
/// assert_eq!(deserialized, expected);
837+
/// ```
801838
#[inline]
802839
pub fn from_reader_with_buffer<T: de::DeserializeOwned, R: Read>(
803840
reader: R,
@@ -820,6 +857,24 @@ where
820857
/// will result in [`Error::RecursionLimitExceeded`] .
821858
///
822859
/// Set a high recursion limit at your own risk (of stack exhaustion)!
860+
///
861+
/// # Example
862+
/// ```rust
863+
/// use ciborium::de::from_reader_with_recursion_limit;
864+
///
865+
/// #[derive(Debug, serde::Deserialize, Eq, PartialEq)]
866+
/// struct Example {
867+
/// a: u64,
868+
/// aa: u64,
869+
/// b: u64,
870+
/// }
871+
///
872+
/// let cbor = hex::decode("a36161182a61621910686261611901a4").unwrap();
873+
/// let expected = Example { a: 42, aa: 420, b: 4200 };
874+
///
875+
/// let deserialized: Example = from_reader_with_recursion_limit(cbor.as_slice(), 1024).unwrap();
876+
/// assert_eq!(deserialized, expected);
877+
/// ```
823878
#[inline]
824879
pub fn from_reader_with_recursion_limit<T: de::DeserializeOwned, R: Read>(
825880
reader: R,

ciborium/src/lib.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,14 @@ pub mod value;
9999

100100
// Re-export the [items recommended by serde](https://serde.rs/conventions.html).
101101
#[doc(inline)]
102-
pub use crate::de::from_reader;
102+
pub use crate::de::{from_reader, from_reader_with_buffer, Deserializer};
103+
103104
#[doc(inline)]
104-
pub use crate::de::from_reader_with_buffer;
105+
pub use crate::ser::{into_writer, Serializer};
105106

106107
#[doc(inline)]
107-
pub use crate::ser::into_writer;
108+
#[cfg(feature = "std")]
109+
pub use crate::ser::{into_writer_canonical, to_vec, to_vec_canonical};
108110

109111
#[doc(inline)]
110112
pub use crate::value::Value;

0 commit comments

Comments
 (0)