minicbor_ser/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2//!
3//! A simple implementation of [serde] for [minicbor]
4//!
5//! [serde]: https://serde.rs/
6//! [minicbor]: https://crates.io/crates/minicbor
7//!
8//! * serialisation
9//!
10//! ```rust
11//! use serde::Serialize;
12//! #[derive(Debug, Serialize)]
13//! struct TestStruct {
14//!    hello: String,
15//! }
16//!
17//! let test_struct = TestStruct {
18//!         hello: "world".to_string(),
19//! };
20//!
21//! let value = to_vec(&test_struct).unwrap();
22//! assert_eq!(
23//!     [0xA1u8, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x65, 0x77, 0x6F, 0x72, 0x6C, 0x64],
24//!     value.as_slice(),
25//! )
26//! ```
27//!
28//! * Deserialization
29//!
30//! ```rust
31//! use serde::Deserialize;
32//! #[derive(Debug, Deserialize, PartialEq)]
33//! struct TestStruct {
34//!     hello: String,
35//! }
36//!
37//! let data = [0xA1u8, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x65, 0x77, 0x6F, 0x72, 0x6C, 0x64];
38//!
39//! let value: TestStruct = from_slice(&data[..]).unwrap();
40//!
41//! assert_eq!(
42//!     TestStruct {
43//!         hello: "world".to_string(),
44//!     },
45//!     value,
46//! );
47//!
48//! ```
49
50#[cfg(all(feature = "alloc", not(feature = "std")))]
51extern crate alloc;
52
53pub mod de;
54pub mod error;
55pub mod ser;
56pub use minicbor as cbor;
57
58mod lib {
59    mod core {
60        #[cfg(not(feature = "std"))]
61        pub use core::*;
62
63        #[cfg(feature = "std")]
64        pub use std::*;
65    }
66
67    pub use self::core::cell::{Cell, RefCell};
68    pub use self::core::clone::{self, Clone};
69    pub use self::core::convert::{self, From, Into};
70    pub use self::core::default::{self, Default};
71    pub use self::core::fmt::{self, Debug, Display};
72    pub use self::core::hash::{self, Hash};
73    pub use self::core::iter::FusedIterator;
74    pub use self::core::marker::{self, PhantomData};
75    pub use self::core::ops::{Bound, RangeBounds};
76    pub use self::core::result::{self, Result};
77    pub use self::core::{borrow, char, cmp, iter, mem, num, ops, slice, str};
78
79    #[cfg(all(feature = "alloc", not(feature = "std")))]
80    pub use alloc::string::{String, ToString};
81    #[cfg(feature = "std")]
82    pub use std::string::{String, ToString};
83
84    #[cfg(all(feature = "alloc", not(feature = "std")))]
85    pub use alloc::vec::{self, Vec};
86    #[cfg(feature = "std")]
87    pub use std::vec::{self, Vec};
88
89    #[cfg(all(feature = "alloc", not(feature = "std")))]
90    pub use alloc::boxed::Box;
91    #[cfg(feature = "std")]
92    pub use std::boxed::Box;
93
94    #[cfg(all(feature = "alloc", not(feature = "std")))]
95    pub use alloc::collections::{btree_map, BTreeMap};
96    #[cfg(feature = "std")]
97    pub use std::collections::{btree_map, BTreeMap};
98}
99
100#[derive(Debug, Clone, Copy, Default)]
101pub struct Config {
102    top_flatten: bool,
103}
104
105pub use de::from_slice;
106pub use de::from_slice_flat;
107pub use ser::to_writer;
108pub use ser::to_writer_cfg;
109
110#[cfg(feature = "alloc")]
111pub use ser::to_vec;
112#[cfg(feature = "alloc")]
113pub use ser::to_vec_flat;
114
115#[test]
116fn test_ser() {
117    use serde::Serialize;
118    use self::lib::*;
119    #[derive(Debug, Serialize)]
120    struct TestStruct {
121        hello: String,
122    }
123
124    let test_struct = TestStruct {
125        hello: "world".to_string(),
126    };
127
128    let value = to_vec(&test_struct).unwrap();
129    assert_eq!(
130        [0xA1u8, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x65, 0x77, 0x6F, 0x72, 0x6C, 0x64],
131        value.as_slice(),
132    )
133}
134
135#[test]
136fn test_de() {
137    use serde::Deserialize;
138    use self::lib::*;
139
140    #[derive(Debug, Deserialize, PartialEq)]
141    struct TestStruct {
142        hello: String,
143    }
144
145    let data = [
146        0xA1u8, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x65, 0x77, 0x6F, 0x72, 0x6C, 0x64,
147    ];
148
149    let value: TestStruct = from_slice(&data[..]).unwrap();
150
151    assert_eq!(
152        TestStruct {
153            hello: "world".to_string(),
154        },
155        value,
156    );
157}