1#![cfg_attr(feature = "alloc", feature(alloc))]
8#![allow(overflowing_literals)]
9
10#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
12#[cfg(all(not(feature = "std"), not(test)))]
13extern crate core as std;
14extern crate serde;
15extern crate byteorder;
16#[cfg(test)]
17#[macro_use]
18extern crate serde_derive;
19
20#[cfg(feature = "alloc")]
21#[macro_use]
22extern crate alloc;
23
24#[cfg(feature = "alloc")]
25use alloc::vec::Vec;
26
27pub use ser::Serializer;
28pub use de::Deserializer;
29
30pub mod error;
31pub mod read;
32
33mod defs;
34mod seq_serializer;
35mod map_serializer;
36mod variant_deserializer;
37mod ext_deserializer;
38mod seq_deserializer;
39
40mod ser;
41mod de;
42
43pub fn from_iter<I, V>(mut iter: I) -> Result<V, error::Error>
45 where I: Iterator<Item = u8>,
46 V: serde::de::DeserializeOwned
47{
48 let mut de = Deserializer::new(read::CopyRead::new(|buf: &mut [u8]| {
49 for i in 0..buf.len() {
50 if let Some(byte) = iter.next() {
51 buf[i] = byte;
52 } else {
53 return Err(error::Error::EndOfStream);
54 }
55 }
56
57 Ok(())
58 }));
59
60 V::deserialize(&mut de)
61}
62
63pub fn from_bytes<'a, V>(bytes: &'a [u8]) -> Result<V, error::Error>
65 where V: serde::Deserialize<'a>
66{
67 let mut position: usize = 0;
68
69 let mut de = Deserializer::new(read::BorrowRead::new(|len: usize| if position + len >
70 bytes.len() {
71 Err(error::Error::EndOfStream)
72 } else {
73 let result = &bytes[position..position + len];
74
75 position += len;
76
77 Ok(result)
78 }));
79
80 V::deserialize(&mut de)
81}
82
83pub fn to_bytes<V>(value: V) -> Result<Vec<u8>, error::Error>
85 where V: serde::Serialize
86{
87 let mut bytes = vec![];
88
89 {
90 let mut ser = Serializer::new(|buf| {
91 bytes.extend_from_slice(buf);
92 Ok(())
93 });
94
95 try!(value.serialize(&mut ser));
96 }
97
98 Ok(bytes)
99}
100
101#[cfg(test)]
102mod test {
103 use serde::Serialize;
104 use serde::de::DeserializeOwned;
105 use std::fmt::Debug;
106 use std::ffi::CString;
107
108 #[derive(PartialEq, Eq, Debug, Serialize, Deserialize)]
109 enum T {
110 A(usize),
111 B,
112 C(i8, i8),
113 D { a: isize, b: String },
114 }
115
116 fn test_through<T>(item: T, expected: &[u8])
117 where T: Serialize + DeserializeOwned + PartialEq + Debug
118 {
119 let actual = ::to_bytes(&item).expect("Failed to serialize");
120
121 assert_eq!(expected, &*actual);
122
123 let deserialized_item = ::from_bytes(&actual).expect("Failed to deserialize");
124
125 assert_eq!(item, deserialized_item);
126 }
127
128 #[test]
129 fn test_str() {
130 test_through(format!("Hello World!"),
131 &[0xac, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64,
132 0x21]);
133 }
134
135 #[test]
136 fn test_enum() {
137 test_through(T::B, &[0x92, 0x01, 0xc0])
138 }
139
140 #[test]
141 fn test_enum_newtype() {
142 test_through(T::A(42), &[0x92, 0x00, 0x2a])
143 }
144
145 #[test]
146 fn test_enum_tuple() {
147 test_through(T::C(-3, 22), &[0x92, 0x02, 0x92, 0xfd, 0x16])
148 }
149
150 #[test]
151 fn test_enum_struct() {
152 test_through(T::D {
153 a: 9001,
154 b: "Hello world!".into(),
155 },
156 &[0x92, 0x03, 0x82, 0xa1, 0x61,
161 0xd1, 0x23,
163 0x29,
164 0xa1, 0x62,
166 0xac, 0x48,
168 0x65,
169 0x6c,
170 0x6c,
171 0x6f,
172 0x20,
173 0x77,
174 0x6f,
175 0x72,
176 0x6c,
177 0x64,
178 0x21])
179 }
180
181 #[test]
182 fn test_option() {
183 test_through(Some(7), &[0x92, 0xc3, 0x07])
184 }
185
186 #[test]
187 fn test_option_none() {
188 test_through::<Option<usize>>(None, &[0x91, 0xc2])
189 }
190
191 #[test]
192 fn test_unit_option() {
193 test_through(Some(()), &[0x92, 0xc3, 0xc0])
194 }
195
196 #[test]
197 fn test_char() {
198 test_through('b', &[0xa1, 0x62])
199 }
200
201 #[test]
202 fn test_false() {
203 test_through(false, &[0xc2])
204 }
205
206 #[test]
207 fn test_byte_array() {
208 test_through(CString::new("hello").unwrap(),
209 &[0xc4, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f])
210 }
211
212 #[test]
213 fn test_float() {
214 test_through(4.5, &[0xcb, 0x40, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
215 }
216
217 #[test]
218 fn test_float32() {
219 test_through(3.2f32, &[0xca, 0x40, 0x4c, 0xcc, 0xcd])
220 }
221}