mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-05-29 21:41:06 +00:00
Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| dc9445f873 | |||
| 17bc40ec1c | |||
| 2184fef82f | |||
| 7e1b5c6ce4 | |||
| 36da8a5cee | |||
| fbe85f399d | |||
| 880b27b19e | |||
| cc06f070d1 | |||
| 2f988aa5e6 | |||
| d294a10e83 | |||
| defcbef7ab | |||
| d90eecd4a2 | |||
| 0d6d077e6a | |||
| be09fc9bbb | |||
| fc9d78e26b | |||
| 9f83164c40 | |||
| 857974ab8a | |||
| d70636f4d4 | |||
| 09e467cc4c | |||
| a9a05350a9 |
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "0.9.10"
|
version = "0.9.11"
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT/Apache-2.0"
|
||||||
description = "A generic serialization/deserialization framework"
|
description = "A generic serialization/deserialization framework"
|
||||||
|
|||||||
@@ -25,6 +25,8 @@ use std::net;
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use std::path;
|
use std::path;
|
||||||
use core::str;
|
use core::str;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
use std::ffi::CString;
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
@@ -53,6 +55,9 @@ use de::{Deserialize, Deserializer, EnumVisitor, Error, MapVisitor, SeqVisitor,
|
|||||||
VariantVisitor, Visitor};
|
VariantVisitor, Visitor};
|
||||||
use de::from_primitive::FromPrimitive;
|
use de::from_primitive::FromPrimitive;
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
use bytes::ByteBuf;
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
/// A visitor that produces a `()`.
|
/// A visitor that produces a `()`.
|
||||||
@@ -295,6 +300,18 @@ impl Deserialize for String {
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
impl Deserialize for CString {
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<CString, D::Error>
|
||||||
|
where D: Deserializer
|
||||||
|
{
|
||||||
|
let bytes = try!(ByteBuf::deserialize(deserializer));
|
||||||
|
CString::new(bytes).map_err(Error::custom)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
struct OptionVisitor<T> {
|
struct OptionVisitor<T> {
|
||||||
marker: PhantomData<T>,
|
marker: PhantomData<T>,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ use core::ops;
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use std::path;
|
use std::path;
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
|
use std::ffi::{CString, CStr};
|
||||||
|
#[cfg(feature = "std")]
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||||
use alloc::rc::Rc;
|
use alloc::rc::Rc;
|
||||||
@@ -98,6 +100,28 @@ impl Serialize for String {
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
impl Serialize for CStr {
|
||||||
|
#[inline]
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where S: Serializer
|
||||||
|
{
|
||||||
|
serializer.serialize_bytes(self.to_bytes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
impl Serialize for CString {
|
||||||
|
#[inline]
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where S: Serializer
|
||||||
|
{
|
||||||
|
serializer.serialize_bytes(self.to_bytes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
impl<T> Serialize for Option<T>
|
impl<T> Serialize for Option<T>
|
||||||
where T: Serialize
|
where T: Serialize
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -98,7 +98,11 @@ use std::error;
|
|||||||
#[cfg(not(feature = "std"))]
|
#[cfg(not(feature = "std"))]
|
||||||
use error;
|
use error;
|
||||||
|
|
||||||
|
#[cfg(all(feature = "collections", not(feature = "std")))]
|
||||||
|
use collections::string::String;
|
||||||
use core::fmt::Display;
|
use core::fmt::Display;
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
|
use core::fmt::Write;
|
||||||
use core::iter::IntoIterator;
|
use core::iter::IntoIterator;
|
||||||
|
|
||||||
mod impls;
|
mod impls;
|
||||||
@@ -616,6 +620,70 @@ pub trait Serializer: Sized {
|
|||||||
}
|
}
|
||||||
serializer.end()
|
serializer.end()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Serialize a string produced by an implementation of `Display`.
|
||||||
|
///
|
||||||
|
/// The default implementation builds a heap-allocated `String` and
|
||||||
|
/// delegates to `serialize_str`. Serializers are encouraged to provide a
|
||||||
|
/// more efficient implementation if possible.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde::{Serialize, Serializer};
|
||||||
|
/// # struct DateTime;
|
||||||
|
/// # impl DateTime {
|
||||||
|
/// # fn naive_local(&self) -> () { () }
|
||||||
|
/// # fn offset(&self) -> () { () }
|
||||||
|
/// # }
|
||||||
|
/// impl Serialize for DateTime {
|
||||||
|
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
/// where S: Serializer
|
||||||
|
/// {
|
||||||
|
/// serializer.collect_str(&format_args!("{:?}{:?}",
|
||||||
|
/// self.naive_local(),
|
||||||
|
/// self.offset()))
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
|
fn collect_str<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
|
||||||
|
where T: Display,
|
||||||
|
{
|
||||||
|
let mut string = String::new();
|
||||||
|
write!(string, "{}", value).unwrap();
|
||||||
|
self.serialize_str(&string)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Serialize a string produced by an implementation of `Display`.
|
||||||
|
///
|
||||||
|
/// The default implementation returns an error unconditionally when
|
||||||
|
/// compiled with `no_std`.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde::{Serialize, Serializer};
|
||||||
|
/// # struct DateTime;
|
||||||
|
/// # impl DateTime {
|
||||||
|
/// # fn naive_local(&self) -> () { () }
|
||||||
|
/// # fn offset(&self) -> () { () }
|
||||||
|
/// # }
|
||||||
|
/// impl Serialize for DateTime {
|
||||||
|
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
/// where S: Serializer
|
||||||
|
/// {
|
||||||
|
/// serializer.collect_str(&format_args!("{:?}{:?}",
|
||||||
|
/// self.naive_local(),
|
||||||
|
/// self.offset()))
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
#[cfg(not(any(feature = "std", feature = "collections")))]
|
||||||
|
fn collect_str<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
|
||||||
|
where T: Display,
|
||||||
|
{
|
||||||
|
// TODO https://github.com/serde-rs/serde/issues/805
|
||||||
|
// Remove this impl and force no_std formats to implement collect_str.
|
||||||
|
let _ = value;
|
||||||
|
Err(Error::custom("this no_std format does not support serializing strings with collect_str"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returned from `Serializer::serialize_seq` and
|
/// Returned from `Serializer::serialize_seq` and
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_derive"
|
name = "serde_derive"
|
||||||
version = "0.9.10"
|
version = "0.9.11"
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT/Apache-2.0"
|
||||||
description = "Macros 1.1 implementation of #[derive(Serialize, Deserialize)]"
|
description = "Macros 1.1 implementation of #[derive(Serialize, Deserialize)]"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_test"
|
name = "serde_test"
|
||||||
version = "0.9.10"
|
version = "0.9.11"
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT/Apache-2.0"
|
||||||
description = "Token De/Serializer for testing De/Serialize implementations"
|
description = "Token De/Serializer for testing De/Serialize implementations"
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use token::Token;
|
|||||||
|
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
|
/// Runs both `assert_ser_tokens` and `assert_de_tokens`.
|
||||||
pub fn assert_tokens<T>(value: &T, tokens: &[Token<'static>])
|
pub fn assert_tokens<T>(value: &T, tokens: &[Token<'static>])
|
||||||
where T: Serialize + Deserialize + PartialEq + Debug
|
where T: Serialize + Deserialize + PartialEq + Debug
|
||||||
{
|
{
|
||||||
@@ -14,6 +15,7 @@ pub fn assert_tokens<T>(value: &T, tokens: &[Token<'static>])
|
|||||||
assert_de_tokens(value, tokens);
|
assert_de_tokens(value, tokens);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Asserts that `value` serializes to the given `tokens`.
|
||||||
pub fn assert_ser_tokens<T>(value: &T, tokens: &[Token])
|
pub fn assert_ser_tokens<T>(value: &T, tokens: &[Token])
|
||||||
where T: Serialize
|
where T: Serialize
|
||||||
{
|
{
|
||||||
@@ -22,7 +24,7 @@ pub fn assert_ser_tokens<T>(value: &T, tokens: &[Token])
|
|||||||
assert_eq!(ser.next_token(), None);
|
assert_eq!(ser.next_token(), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Expect an error serializing `T`.
|
/// Asserts that `value` serializes to the given `tokens`, and then yields `error`.
|
||||||
pub fn assert_ser_tokens_error<T>(value: &T, tokens: &[Token], error: Error)
|
pub fn assert_ser_tokens_error<T>(value: &T, tokens: &[Token], error: Error)
|
||||||
where T: Serialize + PartialEq + Debug
|
where T: Serialize + PartialEq + Debug
|
||||||
{
|
{
|
||||||
@@ -32,6 +34,7 @@ pub fn assert_ser_tokens_error<T>(value: &T, tokens: &[Token], error: Error)
|
|||||||
assert_eq!(ser.next_token(), None);
|
assert_eq!(ser.next_token(), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Asserts that the given `tokens` deserialize into `value`.
|
||||||
pub fn assert_de_tokens<T>(value: &T, tokens: &[Token<'static>])
|
pub fn assert_de_tokens<T>(value: &T, tokens: &[Token<'static>])
|
||||||
where T: Deserialize + PartialEq + Debug
|
where T: Deserialize + PartialEq + Debug
|
||||||
{
|
{
|
||||||
@@ -41,7 +44,7 @@ pub fn assert_de_tokens<T>(value: &T, tokens: &[Token<'static>])
|
|||||||
assert_eq!(de.next_token(), None);
|
assert_eq!(de.next_token(), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Expect an error deserializing tokens into a `T`.
|
/// Asserts that the given `tokens` yield `error` when deserializing.
|
||||||
pub fn assert_de_tokens_error<T>(tokens: &[Token<'static>], error: Error)
|
pub fn assert_de_tokens_error<T>(tokens: &[Token<'static>], error: Error)
|
||||||
where T: Deserialize + PartialEq + Debug
|
where T: Deserialize + PartialEq + Debug
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use serde::de::value::{ValueDeserializer, MapVisitorDeserializer, SeqVisitorDese
|
|||||||
use error::Error;
|
use error::Error;
|
||||||
use token::Token;
|
use token::Token;
|
||||||
|
|
||||||
|
/// A `Deserializer` that reads from a list of tokens.
|
||||||
pub struct Deserializer<I>
|
pub struct Deserializer<I>
|
||||||
where I: Iterator<Item = Token<'static>>
|
where I: Iterator<Item = Token<'static>>
|
||||||
{
|
{
|
||||||
@@ -16,14 +17,17 @@ pub struct Deserializer<I>
|
|||||||
impl<I> Deserializer<I>
|
impl<I> Deserializer<I>
|
||||||
where I: Iterator<Item = Token<'static>>
|
where I: Iterator<Item = Token<'static>>
|
||||||
{
|
{
|
||||||
|
/// Creates the deserializer.
|
||||||
pub fn new(tokens: I) -> Deserializer<I> {
|
pub fn new(tokens: I) -> Deserializer<I> {
|
||||||
Deserializer { tokens: tokens.peekable() }
|
Deserializer { tokens: tokens.peekable() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Pulls the next token off of the deserializer, ignoring it.
|
||||||
pub fn next_token(&mut self) -> Option<Token<'static>> {
|
pub fn next_token(&mut self) -> Option<Token<'static>> {
|
||||||
self.tokens.next()
|
self.tokens.next()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Pulls the next token off of the deserializer and checks if it matches an expected token.
|
||||||
pub fn expect_token(&mut self, expected: Token) -> Result<(), Error> {
|
pub fn expect_token(&mut self, expected: Token) -> Result<(), Error> {
|
||||||
match self.tokens.next() {
|
match self.tokens.next() {
|
||||||
Some(token) => {
|
Some(token) => {
|
||||||
|
|||||||
@@ -5,11 +5,19 @@ use serde::{ser, de};
|
|||||||
|
|
||||||
use token::Token;
|
use token::Token;
|
||||||
|
|
||||||
|
/// Error returned by the test `Serializer` and `Deserializer`.
|
||||||
#[derive(Clone, PartialEq, Debug)]
|
#[derive(Clone, PartialEq, Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
/// A custom error.
|
||||||
Message(String),
|
Message(String),
|
||||||
|
|
||||||
|
/// `Deserialize` was expecting a struct of one name, and another was found.
|
||||||
InvalidName(&'static str),
|
InvalidName(&'static str),
|
||||||
|
|
||||||
|
/// `Serialize` generated a token that didn't match the test.
|
||||||
UnexpectedToken(Token<'static>),
|
UnexpectedToken(Token<'static>),
|
||||||
|
|
||||||
|
/// The expected token list was too short.
|
||||||
EndOfTokens,
|
EndOfTokens,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ use serde::{ser, Serialize};
|
|||||||
use error::Error;
|
use error::Error;
|
||||||
use token::Token;
|
use token::Token;
|
||||||
|
|
||||||
|
/// A `Serializer` that ensures that a value serializes to a given list of tokens.
|
||||||
pub struct Serializer<'a, I>
|
pub struct Serializer<'a, I>
|
||||||
where I: Iterator<Item = &'a Token<'a>>
|
where I: Iterator<Item = &'a Token<'a>>
|
||||||
{
|
{
|
||||||
@@ -15,6 +16,7 @@ pub struct Serializer<'a, I>
|
|||||||
impl<'a, I> Serializer<'a, I>
|
impl<'a, I> Serializer<'a, I>
|
||||||
where I: Iterator<Item = &'a Token<'a>>
|
where I: Iterator<Item = &'a Token<'a>>
|
||||||
{
|
{
|
||||||
|
/// Creates the serializer.
|
||||||
pub fn new(tokens: I) -> Serializer<'a, I> {
|
pub fn new(tokens: I) -> Serializer<'a, I> {
|
||||||
Serializer {
|
Serializer {
|
||||||
tokens: tokens,
|
tokens: tokens,
|
||||||
@@ -22,6 +24,7 @@ impl<'a, I> Serializer<'a, I>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Pulls the next token off of the serializer, ignoring it.
|
||||||
pub fn next_token(&mut self) -> Option<&'a Token<'a>> {
|
pub fn next_token(&mut self) -> Option<&'a Token<'a>> {
|
||||||
self.tokens.next()
|
self.tokens.next()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,59 +1,170 @@
|
|||||||
#[derive(Clone, PartialEq, Debug)]
|
#[derive(Clone, PartialEq, Debug)]
|
||||||
pub enum Token<'a> {
|
pub enum Token<'a> {
|
||||||
|
/// A serialized `bool`.
|
||||||
Bool(bool),
|
Bool(bool),
|
||||||
|
|
||||||
|
/// A serialized `i8`.
|
||||||
I8(i8),
|
I8(i8),
|
||||||
|
|
||||||
|
/// A serialized `i16`.
|
||||||
I16(i16),
|
I16(i16),
|
||||||
|
|
||||||
|
/// A serialized `i32`.
|
||||||
I32(i32),
|
I32(i32),
|
||||||
|
|
||||||
|
/// A serialized `i64`.
|
||||||
I64(i64),
|
I64(i64),
|
||||||
|
|
||||||
|
/// A serialized `u8`.
|
||||||
U8(u8),
|
U8(u8),
|
||||||
|
|
||||||
|
/// A serialized `u16`.
|
||||||
U16(u16),
|
U16(u16),
|
||||||
|
|
||||||
|
/// A serialized `u32`.
|
||||||
U32(u32),
|
U32(u32),
|
||||||
|
|
||||||
|
/// A serialized `u64`.
|
||||||
U64(u64),
|
U64(u64),
|
||||||
|
|
||||||
|
/// A serialized `f32`.
|
||||||
F32(f32),
|
F32(f32),
|
||||||
|
|
||||||
|
/// A serialized `f64`.
|
||||||
F64(f64),
|
F64(f64),
|
||||||
|
|
||||||
|
/// A serialized `char`.
|
||||||
Char(char),
|
Char(char),
|
||||||
|
|
||||||
|
/// A serialized `str`.
|
||||||
Str(&'a str),
|
Str(&'a str),
|
||||||
|
|
||||||
|
/// A serialized `String`.
|
||||||
String(String),
|
String(String),
|
||||||
|
|
||||||
|
/// A serialized `[u8]`
|
||||||
Bytes(&'a [u8]),
|
Bytes(&'a [u8]),
|
||||||
|
|
||||||
|
/// A serialized `ByteBuf`
|
||||||
ByteBuf(Vec<u8>),
|
ByteBuf(Vec<u8>),
|
||||||
|
|
||||||
|
/// The header to a serialized `Option<T>`.
|
||||||
|
///
|
||||||
|
/// `None` is serialized as `Option(false)`, while `Some` is serialized as `Option(true)`, then
|
||||||
|
/// the value contained in the option.
|
||||||
Option(bool),
|
Option(bool),
|
||||||
|
|
||||||
|
/// A serialized `()`.
|
||||||
Unit,
|
Unit,
|
||||||
|
|
||||||
|
/// A serialized unit struct of the given name.
|
||||||
UnitStruct(&'a str),
|
UnitStruct(&'a str),
|
||||||
|
|
||||||
|
/// The header to a serialized newtype struct of the given name.
|
||||||
|
///
|
||||||
|
/// Newtype structs are serialized with this header, followed by the value contained in the
|
||||||
|
/// newtype struct.
|
||||||
StructNewType(&'a str),
|
StructNewType(&'a str),
|
||||||
|
|
||||||
|
/// The header to an enum of the given name.
|
||||||
|
///
|
||||||
|
/// This token is only used for deserializers, and ensures that the following tokens are read as
|
||||||
|
/// an enum. Because this is never emitted by serializers, calling `assert_ser_tokens` or
|
||||||
|
/// `assert_tokens` will fail if this token is used.
|
||||||
|
///
|
||||||
|
/// TODO: Trash this.
|
||||||
EnumStart(&'a str),
|
EnumStart(&'a str),
|
||||||
|
|
||||||
|
/// A unit variant of an enum of the given name, of the given name.
|
||||||
|
///
|
||||||
|
/// The first string represents the name of the enum, and the second represents the name of the
|
||||||
|
/// variant.
|
||||||
EnumUnit(&'a str, &'a str),
|
EnumUnit(&'a str, &'a str),
|
||||||
|
|
||||||
|
/// The header to a newtype variant of an enum of the given name, of the given name.
|
||||||
|
///
|
||||||
|
/// The first string represents the name of the enum, and the second represents the name of the
|
||||||
|
/// variant. The value contained within this enum works the same as `StructNewType`.
|
||||||
EnumNewType(&'a str, &'a str),
|
EnumNewType(&'a str, &'a str),
|
||||||
|
|
||||||
|
/// The header to a sequence of the given length.
|
||||||
|
///
|
||||||
|
/// These are serialized via `serialize_seq`, which takes an optional length. After this
|
||||||
|
/// header is a list of elements, followed by `SeqEnd`.
|
||||||
SeqStart(Option<usize>),
|
SeqStart(Option<usize>),
|
||||||
|
|
||||||
|
/// The header to an array of the given length.
|
||||||
|
///
|
||||||
|
/// These are serialized via `serialize_seq_fized_size`, which requires a length. After this
|
||||||
|
/// header is a list of elements, followed by `SeqEnd`.
|
||||||
SeqArrayStart(usize),
|
SeqArrayStart(usize),
|
||||||
|
|
||||||
|
/// A separator, which occurs *before* every element in a sequence.
|
||||||
|
///
|
||||||
|
/// Elements in sequences are represented by a `SeqSep`, followed by the value of the element.
|
||||||
SeqSep,
|
SeqSep,
|
||||||
|
|
||||||
|
/// An indicator of the end of a sequence.
|
||||||
SeqEnd,
|
SeqEnd,
|
||||||
|
|
||||||
|
/// The header to a tuple of the given length, similar to `SeqArrayStart`.
|
||||||
TupleStart(usize),
|
TupleStart(usize),
|
||||||
|
|
||||||
|
/// A separator, similar to `SeqSep`.
|
||||||
TupleSep,
|
TupleSep,
|
||||||
|
|
||||||
|
/// An indicator of the end of a tuple, similar to `SeqEnd`.
|
||||||
TupleEnd,
|
TupleEnd,
|
||||||
|
|
||||||
|
/// The header to a tuple struct of the given name and length.
|
||||||
TupleStructStart(&'a str, usize),
|
TupleStructStart(&'a str, usize),
|
||||||
|
|
||||||
|
/// A separator, similar to `TupleSep`.
|
||||||
TupleStructSep,
|
TupleStructSep,
|
||||||
|
|
||||||
|
/// An indicator of the end of a tuple struct, similar to `TupleEnd`.
|
||||||
TupleStructEnd,
|
TupleStructEnd,
|
||||||
|
|
||||||
|
/// The header to a map of the given length.
|
||||||
|
///
|
||||||
|
/// These are serialized via `serialize_map`, which takes an optional length. After this header
|
||||||
|
/// is a list of key-value pairs, followed by `MapEnd`.
|
||||||
MapStart(Option<usize>),
|
MapStart(Option<usize>),
|
||||||
|
|
||||||
|
/// A separator, which occurs *before* every key-value pair in a map.
|
||||||
|
///
|
||||||
|
/// Elements in maps are represented by a `MapSep`, followed by a serialized key, followed
|
||||||
|
/// by a serialized value.
|
||||||
MapSep,
|
MapSep,
|
||||||
|
|
||||||
|
/// An indicator of the end of a map.
|
||||||
MapEnd,
|
MapEnd,
|
||||||
|
|
||||||
|
/// The header of a struct of the given name and length, similar to `MapStart`.
|
||||||
StructStart(&'a str, usize),
|
StructStart(&'a str, usize),
|
||||||
|
|
||||||
|
/// A separator, similar to `MapSep`.
|
||||||
StructSep,
|
StructSep,
|
||||||
|
|
||||||
|
/// An indicator of the end of a struct, similar to `MapEnd`.
|
||||||
StructEnd,
|
StructEnd,
|
||||||
|
|
||||||
|
/// The header to a tuple variant of an enum of the given name, of the given name and length.
|
||||||
EnumSeqStart(&'a str, &'a str, usize),
|
EnumSeqStart(&'a str, &'a str, usize),
|
||||||
|
|
||||||
|
/// A separator, similar to `TupleSep`.
|
||||||
EnumSeqSep,
|
EnumSeqSep,
|
||||||
|
|
||||||
|
/// An indicator of the end of a tuple variant, similar to `TupleEnd`.
|
||||||
EnumSeqEnd,
|
EnumSeqEnd,
|
||||||
|
|
||||||
|
/// The header of a struct variant of an enum of the given name, of the given name and length,
|
||||||
|
/// similar to `StructStart`.
|
||||||
EnumMapStart(&'a str, &'a str, usize),
|
EnumMapStart(&'a str, &'a str, usize),
|
||||||
|
|
||||||
|
/// A separator, similar to `StructSep`.
|
||||||
EnumMapSep,
|
EnumMapSep,
|
||||||
|
|
||||||
|
/// An indicator of the end of a struct, similar to `StructEnd`.
|
||||||
EnumMapEnd,
|
EnumMapEnd,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ use std::net;
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
|
use std::ffi::CString;
|
||||||
|
|
||||||
extern crate serde;
|
extern crate serde;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
@@ -878,6 +879,11 @@ declare_tests! {
|
|||||||
Token::String("/usr/local/lib".to_owned()),
|
Token::String("/usr/local/lib".to_owned()),
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
test_cstring {
|
||||||
|
CString::new("abc").unwrap() => &[
|
||||||
|
Token::Bytes(b"abc"),
|
||||||
|
],
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "unstable")]
|
#[cfg(feature = "unstable")]
|
||||||
@@ -995,4 +1001,16 @@ declare_error_tests! {
|
|||||||
],
|
],
|
||||||
Error::Message("invalid length 1, expected an array of length 3".into()),
|
Error::Message("invalid length 1, expected an array of length 3".into()),
|
||||||
}
|
}
|
||||||
|
test_cstring_internal_null<CString> {
|
||||||
|
&[
|
||||||
|
Token::Bytes(b"a\0c"),
|
||||||
|
],
|
||||||
|
Error::Message("nul byte found in provided data at position: 1".into()),
|
||||||
|
}
|
||||||
|
test_cstring_internal_null_end<CString> {
|
||||||
|
&[
|
||||||
|
Token::Bytes(b"ac\0"),
|
||||||
|
],
|
||||||
|
Error::Message("nul byte found in provided data at position: 2".into()),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ use std::net;
|
|||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::str;
|
use std::str;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
use std::ffi::CString;
|
||||||
|
|
||||||
extern crate serde;
|
extern crate serde;
|
||||||
|
|
||||||
@@ -389,6 +390,16 @@ declare_tests! {
|
|||||||
Token::Str("/usr/local/lib"),
|
Token::Str("/usr/local/lib"),
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
test_cstring {
|
||||||
|
CString::new("abc").unwrap() => &[
|
||||||
|
Token::Bytes(b"abc"),
|
||||||
|
],
|
||||||
|
}
|
||||||
|
test_cstr {
|
||||||
|
(&*CString::new("abc").unwrap()) => &[
|
||||||
|
Token::Bytes(b"abc"),
|
||||||
|
],
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user