1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//! corepack is a no_std support for messagepack in serde.
//
// This Source Code Form is subject to the terms of the Mozilla Public License,
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at https://mozilla.org/MPL/2.0/.

#![cfg_attr(feature = "alloc", feature(alloc))]
#![allow(overflowing_literals)]

// testing requires std to be available
#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
#[cfg(all(not(feature = "std"), not(test)))]
extern crate core as std;
extern crate serde;
extern crate byteorder;
#[cfg(test)]
#[macro_use]
extern crate serde_derive;

#[cfg(feature = "alloc")]
#[macro_use]
extern crate alloc;

#[cfg(feature = "alloc")]
use alloc::vec::Vec;

pub use ser::Serializer;
pub use de::Deserializer;

pub mod error;
pub mod read;

mod defs;
mod seq_serializer;
mod map_serializer;
mod variant_deserializer;
mod ext_deserializer;
mod seq_deserializer;

mod ser;
mod de;

/// Parse V out of a stream of bytes.
pub fn from_iter<I, V>(mut iter: I) -> Result<V, error::Error>
    where I: Iterator<Item = u8>,
          V: serde::de::DeserializeOwned
{
    let mut de = Deserializer::new(read::CopyRead::new(|buf: &mut [u8]| {
        for i in 0..buf.len() {
            if let Some(byte) = iter.next() {
                buf[i] = byte;
            } else {
                return Err(error::Error::EndOfStream);
            }
        }

        Ok(())
    }));

    V::deserialize(&mut de)
}

/// Parse V out of a slice of bytes.
pub fn from_bytes<'a, V>(bytes: &'a [u8]) -> Result<V, error::Error>
    where V: serde::Deserialize<'a>
{
    let mut position: usize = 0;

    let mut de = Deserializer::new(read::BorrowRead::new(|len: usize| if position + len >
                                                                         bytes.len() {
        Err(error::Error::EndOfStream)
    } else {
        let result = &bytes[position..position + len];

        position += len;

        Ok(result)
    }));

    V::deserialize(&mut de)
}

/// Serialize V into a byte buffer.
pub fn to_bytes<V>(value: V) -> Result<Vec<u8>, error::Error>
    where V: serde::Serialize
{
    let mut bytes = vec![];

    {
        let mut ser = Serializer::new(|buf| {
            bytes.extend_from_slice(buf);
            Ok(())
        });

        try!(value.serialize(&mut ser));
    }

    Ok(bytes)
}

#[cfg(test)]
mod test {
    use serde::Serialize;
    use serde::de::DeserializeOwned;
    use std::fmt::Debug;
    use std::ffi::CString;

    #[derive(PartialEq, Eq, Debug, Serialize, Deserialize)]
    enum T {
        A(usize),
        B,
        C(i8, i8),
        D { a: isize, b: String },
    }

    fn test_through<T>(item: T, expected: &[u8])
        where T: Serialize + DeserializeOwned + PartialEq + Debug
    {
        let actual = ::to_bytes(&item).expect("Failed to serialize");

        assert_eq!(expected, &*actual);

        let deserialized_item = ::from_bytes(&actual).expect("Failed to deserialize");

        assert_eq!(item, deserialized_item);
    }

    #[test]
    fn test_str() {
        test_through(format!("Hello World!"),
                     &[0xac, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64,
                       0x21]);
    }

    #[test]
    fn test_enum() {
        test_through(T::B, &[0x92, 0x01, 0xc0])
    }

    #[test]
    fn test_enum_newtype() {
        test_through(T::A(42), &[0x92, 0x00, 0x2a])
    }

    #[test]
    fn test_enum_tuple() {
        test_through(T::C(-3, 22), &[0x92, 0x02, 0x92, 0xfd, 0x16])
    }

    #[test]
    fn test_enum_struct() {
        test_through(T::D {
                         a: 9001,
                         b: "Hello world!".into(),
                     },
                     &[0x92, // array with two elements
                       0x03, // 3 (variant index)
                       0x82, // map with two entries
                       0xa1, // entry one, fixstr length one: 'a'
                       0x61,
                       0xd1, // i16: 9001
                       0x23,
                       0x29,
                       0xa1, // entry two, fixstr length one: 'b'
                       0x62,
                       0xac, // fixstr, length 12: Hello world!
                       0x48,
                       0x65,
                       0x6c,
                       0x6c,
                       0x6f,
                       0x20,
                       0x77,
                       0x6f,
                       0x72,
                       0x6c,
                       0x64,
                       0x21])
    }

    #[test]
    fn test_option() {
        test_through(Some(7), &[0x92, 0xc3, 0x07])
    }

    #[test]
    fn test_option_none() {
        test_through::<Option<usize>>(None, &[0x91, 0xc2])
    }

    #[test]
    fn test_unit_option() {
        test_through(Some(()), &[0x92, 0xc3, 0xc0])
    }

    #[test]
    fn test_char() {
        test_through('b', &[0xa1, 0x62])
    }

    #[test]
    fn test_false() {
        test_through(false, &[0xc2])
    }

    #[test]
    fn test_byte_array() {
        test_through(CString::new("hello").unwrap(),
                     &[0xc4, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f])
    }

    #[test]
    fn test_float() {
        test_through(4.5, &[0xcb, 0x40, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
    }

    #[test]
    fn test_float32() {
        test_through(3.2f32, &[0xca, 0x40, 0x4c, 0xcc, 0xcd])
    }
}