mirror of
https://github.com/nushell/nushell.git
synced 2025-08-10 01:27:52 +02:00
Preserve order when serializing/deserialize json by default. (#3126)
This commit is contained in:
committed by
GitHub
parent
9c375b33a6
commit
0b71e45072
@ -8,8 +8,18 @@ version = "0.27.2"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[features]
|
||||
preserve_order = ["linked-hash-map", "linked-hash-map/serde_impl"]
|
||||
default = ["preserve_order"]
|
||||
|
||||
[dependencies]
|
||||
lazy_static = "1"
|
||||
serde = "1.0"
|
||||
num-traits = "0.2.14"
|
||||
regex = "^1.0"
|
||||
serde = "^0.8.0"
|
||||
lazy_static = "1"
|
||||
linked-hash-map = { version = "0.5", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
nu-test-support = { version = "0.27.2", path = "../nu-test-support" }
|
||||
serde_json = "1.0.39"
|
||||
dunce = "1.0.1"
|
@ -1,13 +1,3 @@
|
||||
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
use serde::ser;
|
||||
|
||||
use crate::value::{self, Map, Value};
|
||||
@ -36,7 +26,8 @@ impl ArrayBuilder {
|
||||
|
||||
/// Insert a value into the array.
|
||||
pub fn push<T: ser::Serialize>(mut self, v: T) -> ArrayBuilder {
|
||||
self.array.push(value::to_value(&v));
|
||||
self.array
|
||||
.push(value::to_value(&v).expect("failed to serialize"));
|
||||
self
|
||||
}
|
||||
|
||||
@ -91,7 +82,10 @@ impl ObjectBuilder {
|
||||
S: Into<String>,
|
||||
V: ser::Serialize,
|
||||
{
|
||||
self.object.insert(key.into(), value::to_value(&value));
|
||||
self.object.insert(
|
||||
key.into(),
|
||||
value::to_value(&value).expect("failed to serialize"),
|
||||
);
|
||||
self
|
||||
}
|
||||
|
||||
|
@ -74,9 +74,9 @@ where
|
||||
matches!(ch, b'{' | b'}' | b'[' | b']' | b',' | b':')
|
||||
}
|
||||
|
||||
fn parse_keyname<V>(&mut self, mut visitor: V) -> Result<V::Value>
|
||||
fn parse_keyname<'de, V>(&mut self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: de::Visitor,
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
// quotes for keys are optional in Hjson
|
||||
// unless they include {}[],: or whitespace.
|
||||
@ -117,9 +117,9 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_value<V>(&mut self, mut visitor: V) -> Result<V::Value>
|
||||
fn parse_value<'de, V>(&mut self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: de::Visitor,
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
self.rdr.parse_whitespace()?;
|
||||
|
||||
@ -134,18 +134,18 @@ where
|
||||
}
|
||||
State::Root => {
|
||||
self.state = State::Normal;
|
||||
return visitor.visit_map(MapVisitor::new(self, true));
|
||||
return self.visit_map(true, visitor);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
let value = match self.rdr.peek_or_null()? {
|
||||
match self.rdr.peek_or_null()? {
|
||||
/*
|
||||
b'-' => {
|
||||
self.rdr.eat_char();
|
||||
self.parse_integer(false, visitor)
|
||||
}
|
||||
b'0' ..= b'9' => {
|
||||
b'0' ... b'9' => {
|
||||
self.parse_integer(true, visitor)
|
||||
}
|
||||
*/
|
||||
@ -157,20 +157,45 @@ where
|
||||
}
|
||||
b'[' => {
|
||||
self.rdr.eat_char();
|
||||
visitor.visit_seq(SeqVisitor::new(self))
|
||||
let ret = visitor.visit_seq(SeqVisitor::new(self))?;
|
||||
self.rdr.parse_whitespace()?;
|
||||
match self.rdr.next_char()? {
|
||||
Some(b']') => Ok(ret),
|
||||
Some(_) => Err(self.rdr.error(ErrorCode::TrailingCharacters)),
|
||||
None => Err(self.rdr.error(ErrorCode::EOFWhileParsingList)),
|
||||
}
|
||||
}
|
||||
b'{' => {
|
||||
self.rdr.eat_char();
|
||||
visitor.visit_map(MapVisitor::new(self, false))
|
||||
self.visit_map(false, visitor)
|
||||
}
|
||||
b'\x00' => Err(self.rdr.error(ErrorCode::ExpectedSomeValue)),
|
||||
_ => self.parse_tfnns(visitor),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
match value {
|
||||
Ok(value) => Ok(value),
|
||||
Err(Error::Syntax(code, _, _)) => Err(self.rdr.error(code)),
|
||||
Err(err) => Err(err),
|
||||
fn visit_map<'de, V>(&mut self, root: bool, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
let ret = visitor.visit_map(MapVisitor::new(self, root))?;
|
||||
self.rdr.parse_whitespace()?;
|
||||
match self.rdr.next_char()? {
|
||||
Some(b'}') => {
|
||||
if !root {
|
||||
Ok(ret)
|
||||
} else {
|
||||
Err(self.rdr.error(ErrorCode::TrailingCharacters))
|
||||
} // todo
|
||||
}
|
||||
Some(_) => Err(self.rdr.error(ErrorCode::TrailingCharacters)),
|
||||
None => {
|
||||
if root {
|
||||
Ok(ret)
|
||||
} else {
|
||||
Err(self.rdr.error(ErrorCode::EOFWhileParsingObject))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -184,9 +209,9 @@ where
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_tfnns<V>(&mut self, mut visitor: V) -> Result<V::Value>
|
||||
fn parse_tfnns<'de, V>(&mut self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: de::Visitor,
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
// Hjson strings can be quoteless
|
||||
// returns string, true, false, or null.
|
||||
@ -244,7 +269,7 @@ where
|
||||
}
|
||||
_ => {
|
||||
if chf == b'-' || (b'0'..=b'9').contains(&chf) {
|
||||
let mut pn = ParseNumber::new(self.str_buf.iter().cloned());
|
||||
let mut pn = ParseNumber::new(self.str_buf.iter().copied());
|
||||
match pn.parse(false) {
|
||||
Ok(Number::F64(v)) => {
|
||||
self.rdr.uneat_char(ch);
|
||||
@ -274,7 +299,7 @@ where
|
||||
}
|
||||
self.str_buf.push(ch);
|
||||
|
||||
if self.str_buf == vec![b'\''; 3] {
|
||||
if self.str_buf == b"'''" {
|
||||
return self.parse_ml_string(visitor);
|
||||
}
|
||||
}
|
||||
@ -283,7 +308,7 @@ where
|
||||
fn decode_hex_escape(&mut self) -> Result<u16> {
|
||||
let mut i = 0;
|
||||
let mut n = 0u16;
|
||||
while i < 4 && !(self.rdr.eof()?) {
|
||||
while i < 4 && !self.rdr.eof()? {
|
||||
n = match self.rdr.next_char_or_null()? {
|
||||
c @ b'0'..=b'9' => n * 16_u16 + ((c as u16) - (b'0' as u16)),
|
||||
b'a' | b'A' => n * 16_u16 + 10_u16,
|
||||
@ -326,9 +351,9 @@ where
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_ml_string<V>(&mut self, mut visitor: V) -> Result<V::Value>
|
||||
fn parse_ml_string<'de, V>(&mut self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: de::Visitor,
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
self.str_buf.clear();
|
||||
|
||||
@ -464,11 +489,7 @@ where
|
||||
},
|
||||
};
|
||||
|
||||
// FIXME: this allocation is required in order to be compatible with stable
|
||||
// rust, which doesn't support encoding a `char` into a stack buffer.
|
||||
let mut buf = String::new();
|
||||
buf.push(c);
|
||||
self.str_buf.extend(buf.bytes());
|
||||
self.str_buf.extend(c.encode_utf8(&mut [0; 4]).as_bytes());
|
||||
}
|
||||
_ => {
|
||||
return Err(self.rdr.error(ErrorCode::InvalidEscape));
|
||||
@ -493,25 +514,27 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<Iter> de::Deserializer for Deserializer<Iter>
|
||||
impl<'de, 'a, Iter> de::Deserializer<'de> for &'a mut Deserializer<Iter>
|
||||
where
|
||||
Iter: Iterator<Item = u8>,
|
||||
{
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn deserialize<V>(&mut self, visitor: V) -> Result<V::Value>
|
||||
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: de::Visitor,
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
if let State::Root = self.state {}
|
||||
|
||||
self.parse_value(visitor)
|
||||
}
|
||||
|
||||
/// Parses a `null` as a None, and any other values as a `Some(...)`.
|
||||
#[inline]
|
||||
fn deserialize_option<V>(&mut self, mut visitor: V) -> Result<V::Value>
|
||||
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: de::Visitor,
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
self.rdr.parse_whitespace()?;
|
||||
|
||||
@ -527,42 +550,17 @@ where
|
||||
|
||||
/// Parses a newtype struct as the underlying value.
|
||||
#[inline]
|
||||
fn deserialize_newtype_struct<V>(&mut self, _name: &str, mut visitor: V) -> Result<V::Value>
|
||||
fn deserialize_newtype_struct<V>(self, _name: &str, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: de::Visitor,
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
visitor.visit_newtype_struct(self)
|
||||
}
|
||||
|
||||
forward_to_deserialize! {
|
||||
deserialize_bool();
|
||||
deserialize_usize();
|
||||
deserialize_u8();
|
||||
deserialize_u16();
|
||||
deserialize_u32();
|
||||
deserialize_u64();
|
||||
deserialize_isize();
|
||||
deserialize_i8();
|
||||
deserialize_i16();
|
||||
deserialize_i32();
|
||||
deserialize_i64();
|
||||
deserialize_f32();
|
||||
deserialize_f64();
|
||||
deserialize_char();
|
||||
deserialize_str();
|
||||
deserialize_string();
|
||||
deserialize_unit();
|
||||
deserialize_seq();
|
||||
deserialize_seq_fixed_size(len: usize);
|
||||
deserialize_bytes();
|
||||
deserialize_map();
|
||||
deserialize_unit_struct(name: &'static str);
|
||||
deserialize_tuple_struct(name: &'static str, len: usize);
|
||||
deserialize_struct(name: &'static str, fields: &'static [&'static str]);
|
||||
deserialize_struct_field();
|
||||
deserialize_tuple(len: usize);
|
||||
deserialize_enum(name: &'static str, variants: &'static [&'static str]);
|
||||
deserialize_ignored_any();
|
||||
serde::forward_to_deserialize_any! {
|
||||
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
|
||||
bytes byte_buf unit unit_struct seq tuple map
|
||||
tuple_struct struct enum identifier ignored_any
|
||||
}
|
||||
}
|
||||
|
||||
@ -576,15 +574,15 @@ impl<'a, Iter: Iterator<Item = u8>> SeqVisitor<'a, Iter> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Iter> de::SeqVisitor for SeqVisitor<'a, Iter>
|
||||
impl<'de, 'a, Iter> de::SeqAccess<'de> for SeqVisitor<'a, Iter>
|
||||
where
|
||||
Iter: Iterator<Item = u8>,
|
||||
{
|
||||
type Error = Error;
|
||||
|
||||
fn visit<T>(&mut self) -> Result<Option<T>>
|
||||
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
|
||||
where
|
||||
T: de::Deserialize,
|
||||
T: de::DeserializeSeed<'de>,
|
||||
{
|
||||
self.de.rdr.parse_whitespace()?;
|
||||
|
||||
@ -598,7 +596,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
let value = de::Deserialize::deserialize(self.de)?;
|
||||
let value = seed.deserialize(&mut *self.de)?;
|
||||
|
||||
// in Hjson the comma is optional and trailing commas are allowed
|
||||
self.de.rdr.parse_whitespace()?;
|
||||
@ -609,16 +607,6 @@ where
|
||||
|
||||
Ok(Some(value))
|
||||
}
|
||||
|
||||
fn end(&mut self) -> Result<()> {
|
||||
self.de.rdr.parse_whitespace()?;
|
||||
|
||||
match self.de.rdr.next_char()? {
|
||||
Some(b']') => Ok(()),
|
||||
Some(_) => Err(self.de.rdr.error(ErrorCode::TrailingCharacters)),
|
||||
None => Err(self.de.rdr.error(ErrorCode::EOFWhileParsingList)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct MapVisitor<'a, Iter: 'a + Iterator<Item = u8>> {
|
||||
@ -637,15 +625,15 @@ impl<'a, Iter: Iterator<Item = u8>> MapVisitor<'a, Iter> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Iter> de::MapVisitor for MapVisitor<'a, Iter>
|
||||
impl<'de, 'a, Iter> de::MapAccess<'de> for MapVisitor<'a, Iter>
|
||||
where
|
||||
Iter: Iterator<Item = u8>,
|
||||
{
|
||||
type Error = Error;
|
||||
|
||||
fn visit_key<K>(&mut self) -> Result<Option<K>>
|
||||
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
|
||||
where
|
||||
K: de::Deserialize,
|
||||
K: de::DeserializeSeed<'de>,
|
||||
{
|
||||
self.de.rdr.parse_whitespace()?;
|
||||
|
||||
@ -676,146 +664,51 @@ where
|
||||
} else {
|
||||
State::Keyname
|
||||
};
|
||||
Ok(Some(de::Deserialize::deserialize(self.de)?))
|
||||
Ok(Some(seed.deserialize(&mut *self.de)?))
|
||||
}
|
||||
None => Err(self.de.rdr.error(ErrorCode::EOFWhileParsingValue)),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_value<V>(&mut self) -> Result<V>
|
||||
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
|
||||
where
|
||||
V: de::Deserialize,
|
||||
V: de::DeserializeSeed<'de>,
|
||||
{
|
||||
self.de.parse_object_colon()?;
|
||||
|
||||
de::Deserialize::deserialize(self.de)
|
||||
}
|
||||
|
||||
fn end(&mut self) -> Result<()> {
|
||||
self.de.rdr.parse_whitespace()?;
|
||||
|
||||
match self.de.rdr.next_char()? {
|
||||
Some(b'}') => {
|
||||
if !self.root {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(self.de.rdr.error(ErrorCode::TrailingCharacters))
|
||||
} // todo
|
||||
}
|
||||
Some(_) => Err(self.de.rdr.error(ErrorCode::TrailingCharacters)),
|
||||
None => {
|
||||
if self.root {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(self.de.rdr.error(ErrorCode::EOFWhileParsingObject))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn missing_field<V>(&mut self, field: &'static str) -> Result<V>
|
||||
where
|
||||
V: de::Deserialize,
|
||||
{
|
||||
struct MissingFieldDeserializer(&'static str);
|
||||
|
||||
impl de::Deserializer for MissingFieldDeserializer {
|
||||
type Error = de::value::Error;
|
||||
|
||||
fn deserialize<V>(&mut self, _visitor: V) -> std::result::Result<V::Value, Self::Error>
|
||||
where
|
||||
V: de::Visitor,
|
||||
{
|
||||
let &mut MissingFieldDeserializer(field) = self;
|
||||
Err(de::value::Error::MissingField(field))
|
||||
}
|
||||
|
||||
fn deserialize_option<V>(
|
||||
&mut self,
|
||||
mut visitor: V,
|
||||
) -> std::result::Result<V::Value, Self::Error>
|
||||
where
|
||||
V: de::Visitor,
|
||||
{
|
||||
visitor.visit_none()
|
||||
}
|
||||
|
||||
forward_to_deserialize! {
|
||||
deserialize_bool();
|
||||
deserialize_usize();
|
||||
deserialize_u8();
|
||||
deserialize_u16();
|
||||
deserialize_u32();
|
||||
deserialize_u64();
|
||||
deserialize_isize();
|
||||
deserialize_i8();
|
||||
deserialize_i16();
|
||||
deserialize_i32();
|
||||
deserialize_i64();
|
||||
deserialize_f32();
|
||||
deserialize_f64();
|
||||
deserialize_char();
|
||||
deserialize_str();
|
||||
deserialize_string();
|
||||
deserialize_unit();
|
||||
deserialize_seq();
|
||||
deserialize_seq_fixed_size(len: usize);
|
||||
deserialize_bytes();
|
||||
deserialize_map();
|
||||
deserialize_unit_struct(name: &'static str);
|
||||
deserialize_newtype_struct(name: &'static str);
|
||||
deserialize_tuple_struct(name: &'static str, len: usize);
|
||||
deserialize_struct(name: &'static str, fields: &'static [&'static str]);
|
||||
deserialize_struct_field();
|
||||
deserialize_tuple(len: usize);
|
||||
deserialize_enum(name: &'static str, variants: &'static [&'static str]);
|
||||
deserialize_ignored_any();
|
||||
}
|
||||
}
|
||||
|
||||
let mut de = MissingFieldDeserializer(field);
|
||||
Ok(de::Deserialize::deserialize(&mut de)?)
|
||||
Ok(seed.deserialize(&mut *self.de)?)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Iter> de::VariantVisitor for Deserializer<Iter>
|
||||
impl<'de, 'a, Iter> de::VariantAccess<'de> for &'a mut Deserializer<Iter>
|
||||
where
|
||||
Iter: Iterator<Item = u8>,
|
||||
{
|
||||
type Error = Error;
|
||||
|
||||
fn visit_variant<V>(&mut self) -> Result<V>
|
||||
where
|
||||
V: de::Deserialize,
|
||||
{
|
||||
let val = de::Deserialize::deserialize(self)?;
|
||||
self.parse_object_colon()?;
|
||||
Ok(val)
|
||||
}
|
||||
|
||||
fn visit_unit(&mut self) -> Result<()> {
|
||||
fn unit_variant(self) -> Result<()> {
|
||||
de::Deserialize::deserialize(self)
|
||||
}
|
||||
|
||||
fn visit_newtype<T>(&mut self) -> Result<T>
|
||||
fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value>
|
||||
where
|
||||
T: de::Deserialize,
|
||||
T: de::DeserializeSeed<'de>,
|
||||
{
|
||||
de::Deserialize::deserialize(self)
|
||||
seed.deserialize(self)
|
||||
}
|
||||
|
||||
fn visit_tuple<V>(&mut self, _len: usize, visitor: V) -> Result<V::Value>
|
||||
fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: de::Visitor,
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
de::Deserializer::deserialize(self, visitor)
|
||||
de::Deserializer::deserialize_any(self, visitor)
|
||||
}
|
||||
|
||||
fn visit_struct<V>(&mut self, _fields: &'static [&'static str], visitor: V) -> Result<V::Value>
|
||||
fn struct_variant<V>(self, _fields: &'static [&'static str], visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: de::Visitor,
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
de::Deserializer::deserialize(self, visitor)
|
||||
de::Deserializer::deserialize_any(self, visitor)
|
||||
}
|
||||
}
|
||||
|
||||
@ -825,7 +718,7 @@ where
|
||||
pub struct StreamDeserializer<T, Iter>
|
||||
where
|
||||
Iter: Iterator<Item = u8>,
|
||||
T: de::Deserialize,
|
||||
T: de::DeserializeOwned,
|
||||
{
|
||||
deser: Deserializer<Iter>,
|
||||
_marker: PhantomData<T>,
|
||||
@ -834,7 +727,7 @@ where
|
||||
impl<T, Iter> StreamDeserializer<T, Iter>
|
||||
where
|
||||
Iter: Iterator<Item = u8>,
|
||||
T: de::Deserialize,
|
||||
T: de::DeserializeOwned,
|
||||
{
|
||||
/// Returns an `Iterator` of decoded Hjson values from an iterator over
|
||||
/// `Iterator<Item=u8>`.
|
||||
@ -849,7 +742,7 @@ where
|
||||
impl<T, Iter> Iterator for StreamDeserializer<T, Iter>
|
||||
where
|
||||
Iter: Iterator<Item = u8>,
|
||||
T: de::Deserialize,
|
||||
T: de::DeserializeOwned,
|
||||
{
|
||||
type Item = Result<T>;
|
||||
|
||||
@ -879,9 +772,10 @@ where
|
||||
pub fn from_iter<I, T>(iter: I) -> Result<T>
|
||||
where
|
||||
I: Iterator<Item = io::Result<u8>>,
|
||||
T: de::Deserialize,
|
||||
T: de::DeserializeOwned,
|
||||
{
|
||||
let fold: io::Result<Vec<_>> = iter.collect();
|
||||
|
||||
if let Err(e) = fold {
|
||||
return Err(Error::Io(e));
|
||||
}
|
||||
@ -893,43 +787,31 @@ where
|
||||
// todo: add compile switch
|
||||
|
||||
// deserialize and make sure the whole stream has been consumed
|
||||
let mut de = Deserializer::new_for_root(bytes.iter().cloned());
|
||||
let value = match de::Deserialize::deserialize(&mut de).and_then(|x| {
|
||||
de.end()?;
|
||||
Ok(x)
|
||||
}) {
|
||||
Ok(v) => Ok(v),
|
||||
Err(_) => {
|
||||
let mut de2 = Deserializer::new(bytes.iter().cloned());
|
||||
match de::Deserialize::deserialize(&mut de2).and_then(|x| {
|
||||
de2.end()?;
|
||||
Ok(x)
|
||||
}) {
|
||||
Ok(v) => Ok(v),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
};
|
||||
let mut de = Deserializer::new_for_root(bytes.iter().copied());
|
||||
de::Deserialize::deserialize(&mut de)
|
||||
.and_then(|x| de.end().map(|()| x))
|
||||
.or_else(|_| {
|
||||
let mut de2 = Deserializer::new(bytes.iter().copied());
|
||||
de::Deserialize::deserialize(&mut de2).and_then(|x| de2.end().map(|()| x))
|
||||
})
|
||||
|
||||
/* without legacy support:
|
||||
// deserialize and make sure the whole stream has been consumed
|
||||
let mut de = Deserializer::new(bytes.iter().map(|b| *b));
|
||||
let value = match de::Deserialize::deserialize(&mut de)
|
||||
.and_then(|x| { de.end()); Ok(x) })
|
||||
.and_then(|x| { try!(de.end()); Ok(x) })
|
||||
{
|
||||
Ok(v) => Ok(v),
|
||||
Err(e) => Err(e),
|
||||
};
|
||||
*/
|
||||
|
||||
value
|
||||
}
|
||||
|
||||
/// Decodes a Hjson value from a `std::io::Read`.
|
||||
pub fn from_reader<R, T>(rdr: R) -> Result<T>
|
||||
where
|
||||
R: io::Read,
|
||||
T: de::Deserialize,
|
||||
T: de::DeserializeOwned,
|
||||
{
|
||||
from_iter(rdr.bytes())
|
||||
}
|
||||
@ -937,7 +819,7 @@ where
|
||||
/// Decodes a Hjson value from a byte slice `&[u8]`.
|
||||
pub fn from_slice<T>(v: &[u8]) -> Result<T>
|
||||
where
|
||||
T: de::Deserialize,
|
||||
T: de::DeserializeOwned,
|
||||
{
|
||||
from_iter(v.iter().map(|byte| Ok(*byte)))
|
||||
}
|
||||
@ -945,7 +827,7 @@ where
|
||||
/// Decodes a Hjson value from a `&str`.
|
||||
pub fn from_str<T>(s: &str) -> Result<T>
|
||||
where
|
||||
T: de::Deserialize,
|
||||
T: de::DeserializeOwned,
|
||||
{
|
||||
from_slice(s.as_bytes())
|
||||
}
|
||||
|
@ -18,24 +18,6 @@ pub enum ErrorCode {
|
||||
/// Catchall for syntax error messages
|
||||
Custom(String),
|
||||
|
||||
/// Incorrect type from value
|
||||
InvalidType(de::Type),
|
||||
|
||||
/// Incorrect value
|
||||
InvalidValue(String),
|
||||
|
||||
/// Invalid length
|
||||
InvalidLength(usize),
|
||||
|
||||
/// Unknown variant in an enum.
|
||||
UnknownVariant(String),
|
||||
|
||||
/// Unknown field in struct.
|
||||
UnknownField(String),
|
||||
|
||||
/// Struct is missing a field.
|
||||
MissingField(&'static str),
|
||||
|
||||
/// EOF while parsing a list.
|
||||
EOFWhileParsingList,
|
||||
|
||||
@ -94,12 +76,6 @@ impl fmt::Debug for ErrorCode {
|
||||
|
||||
match *self {
|
||||
ErrorCode::Custom(ref msg) => write!(f, "{}", msg),
|
||||
ErrorCode::InvalidType(ref ty) => write!(f, "invalid type: {:?}", ty),
|
||||
ErrorCode::InvalidValue(ref msg) => write!(f, "invalid value: {}", msg),
|
||||
ErrorCode::InvalidLength(ref len) => write!(f, "invalid value length {}", len),
|
||||
ErrorCode::UnknownVariant(ref variant) => write!(f, "unknown variant \"{}\"", variant),
|
||||
ErrorCode::UnknownField(ref field) => write!(f, "unknown field \"{}\"", field),
|
||||
ErrorCode::MissingField(ref field) => write!(f, "missing field \"{}\"", field),
|
||||
ErrorCode::EOFWhileParsingList => "EOF while parsing a list".fmt(f),
|
||||
ErrorCode::EOFWhileParsingObject => "EOF while parsing an object".fmt(f),
|
||||
ErrorCode::EOFWhileParsingString => "EOF while parsing a string".fmt(f),
|
||||
@ -173,69 +149,16 @@ impl From<FromUtf8Error> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<de::value::Error> for Error {
|
||||
fn from(error: de::value::Error) -> Error {
|
||||
match error {
|
||||
de::value::Error::Custom(e) => Error::Syntax(ErrorCode::Custom(e), 0, 0),
|
||||
de::value::Error::EndOfStream => de::Error::end_of_stream(),
|
||||
de::value::Error::InvalidType(ty) => Error::Syntax(ErrorCode::InvalidType(ty), 0, 0),
|
||||
de::value::Error::InvalidValue(msg) => {
|
||||
Error::Syntax(ErrorCode::InvalidValue(msg), 0, 0)
|
||||
}
|
||||
de::value::Error::InvalidLength(len) => {
|
||||
Error::Syntax(ErrorCode::InvalidLength(len), 0, 0)
|
||||
}
|
||||
de::value::Error::UnknownVariant(variant) => {
|
||||
Error::Syntax(ErrorCode::UnknownVariant(variant), 0, 0)
|
||||
}
|
||||
de::value::Error::UnknownField(field) => {
|
||||
Error::Syntax(ErrorCode::UnknownField(field), 0, 0)
|
||||
}
|
||||
de::value::Error::MissingField(field) => {
|
||||
Error::Syntax(ErrorCode::MissingField(field), 0, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl de::Error for Error {
|
||||
fn custom<T: Into<String>>(msg: T) -> Error {
|
||||
Error::Syntax(ErrorCode::Custom(msg.into()), 0, 0)
|
||||
}
|
||||
|
||||
fn end_of_stream() -> Error {
|
||||
Error::Syntax(ErrorCode::EOFWhileParsingValue, 0, 0)
|
||||
}
|
||||
|
||||
fn invalid_type(ty: de::Type) -> Error {
|
||||
Error::Syntax(ErrorCode::InvalidType(ty), 0, 0)
|
||||
}
|
||||
|
||||
fn invalid_value(msg: &str) -> Error {
|
||||
Error::Syntax(ErrorCode::InvalidValue(msg.to_owned()), 0, 0)
|
||||
}
|
||||
|
||||
fn invalid_length(len: usize) -> Error {
|
||||
Error::Syntax(ErrorCode::InvalidLength(len), 0, 0)
|
||||
}
|
||||
|
||||
fn unknown_variant(variant: &str) -> Error {
|
||||
Error::Syntax(ErrorCode::UnknownVariant(String::from(variant)), 0, 0)
|
||||
}
|
||||
|
||||
fn unknown_field(field: &str) -> Error {
|
||||
Error::Syntax(ErrorCode::UnknownField(String::from(field)), 0, 0)
|
||||
}
|
||||
|
||||
fn missing_field(field: &'static str) -> Error {
|
||||
Error::Syntax(ErrorCode::MissingField(field), 0, 0)
|
||||
fn custom<T: fmt::Display>(msg: T) -> Error {
|
||||
Error::Syntax(ErrorCode::Custom(msg.to_string()), 0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
impl ser::Error for Error {
|
||||
/// Raised when there is general error when deserializing a type.
|
||||
fn custom<T: Into<String>>(msg: T) -> Error {
|
||||
Error::Syntax(ErrorCode::Custom(msg.into()), 0, 0)
|
||||
fn custom<T: fmt::Display>(msg: T) -> Error {
|
||||
Error::Syntax(ErrorCode::Custom(msg.to_string()), 0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,38 +0,0 @@
|
||||
#[macro_export]
|
||||
/// Create a function to forward a specific serialize call to the generic deserialize
|
||||
macro_rules! forward_to_deserialize {
|
||||
($(
|
||||
$name:ident ( $( $arg:ident : $ty:ty ),* );
|
||||
)*) => {
|
||||
$(
|
||||
forward_to_deserialize!{
|
||||
func: $name ( $( $arg: $ty ),* );
|
||||
}
|
||||
)*
|
||||
};
|
||||
|
||||
(func: deserialize_enum ( $( $arg:ident : $ty:ty ),* );) => {
|
||||
fn deserialize_enum<V>(
|
||||
&mut self,
|
||||
$(_: $ty,)*
|
||||
_visitor: V,
|
||||
) -> ::std::result::Result<V::Value, Self::Error>
|
||||
where V: ::serde::de::EnumVisitor
|
||||
{
|
||||
Err(::serde::de::Error::invalid_type(::serde::de::Type::Enum))
|
||||
}
|
||||
};
|
||||
|
||||
(func: $name:ident ( $( $arg:ident : $ty:ty ),* );) => {
|
||||
#[inline]
|
||||
fn $name<V>(
|
||||
&mut self,
|
||||
$(_: $ty,)*
|
||||
visitor: V,
|
||||
) -> ::std::result::Result<V::Value, Self::Error>
|
||||
where V: ::serde::de::Visitor
|
||||
{
|
||||
self.deserialize(visitor)
|
||||
}
|
||||
};
|
||||
}
|
@ -5,9 +5,6 @@ pub use self::error::{Error, ErrorCode, Result};
|
||||
pub use self::ser::{to_string, to_vec, to_writer, Serializer};
|
||||
pub use self::value::{from_value, to_value, Map, Value};
|
||||
|
||||
#[macro_use]
|
||||
mod forward;
|
||||
|
||||
pub mod builder;
|
||||
pub mod de;
|
||||
pub mod error;
|
||||
|
@ -59,23 +59,30 @@ pub enum State {
|
||||
Rest,
|
||||
}
|
||||
|
||||
impl<W, F> ser::Serializer for Serializer<W, F>
|
||||
#[doc(hidden)]
|
||||
pub struct Compound<'a, W, F> {
|
||||
ser: &'a mut Serializer<W, F>,
|
||||
state: State,
|
||||
}
|
||||
|
||||
impl<'a, W, F> ser::Serializer for &'a mut Serializer<W, F>
|
||||
where
|
||||
W: io::Write,
|
||||
F: Formatter,
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
type SeqState = State;
|
||||
type TupleState = State;
|
||||
type TupleStructState = State;
|
||||
type TupleVariantState = State;
|
||||
type MapState = State;
|
||||
type StructState = State;
|
||||
type StructVariantState = State;
|
||||
type SerializeSeq = Compound<'a, W, F>;
|
||||
type SerializeTuple = Compound<'a, W, F>;
|
||||
type SerializeTupleStruct = Compound<'a, W, F>;
|
||||
type SerializeTupleVariant = Compound<'a, W, F>;
|
||||
type SerializeMap = Compound<'a, W, F>;
|
||||
type SerializeStruct = Compound<'a, W, F>;
|
||||
type SerializeStructVariant = Compound<'a, W, F>;
|
||||
|
||||
#[inline]
|
||||
fn serialize_bool(&mut self, value: bool) -> Result<()> {
|
||||
fn serialize_bool(self, value: bool) -> Result<()> {
|
||||
self.formatter.start_value(&mut self.writer)?;
|
||||
if value {
|
||||
self.writer.write_all(b"true").map_err(From::from)
|
||||
@ -85,115 +92,103 @@ where
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_isize(&mut self, value: isize) -> Result<()> {
|
||||
fn serialize_i8(self, value: i8) -> Result<()> {
|
||||
self.formatter.start_value(&mut self.writer)?;
|
||||
write!(&mut self.writer, "{}", value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i8(&mut self, value: i8) -> Result<()> {
|
||||
fn serialize_i16(self, value: i16) -> Result<()> {
|
||||
self.formatter.start_value(&mut self.writer)?;
|
||||
write!(&mut self.writer, "{}", value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i16(&mut self, value: i16) -> Result<()> {
|
||||
fn serialize_i32(self, value: i32) -> Result<()> {
|
||||
self.formatter.start_value(&mut self.writer)?;
|
||||
write!(&mut self.writer, "{}", value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i32(&mut self, value: i32) -> Result<()> {
|
||||
fn serialize_i64(self, value: i64) -> Result<()> {
|
||||
self.formatter.start_value(&mut self.writer)?;
|
||||
write!(&mut self.writer, "{}", value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i64(&mut self, value: i64) -> Result<()> {
|
||||
fn serialize_u8(self, value: u8) -> Result<()> {
|
||||
self.formatter.start_value(&mut self.writer)?;
|
||||
write!(&mut self.writer, "{}", value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_usize(&mut self, value: usize) -> Result<()> {
|
||||
fn serialize_u16(self, value: u16) -> Result<()> {
|
||||
self.formatter.start_value(&mut self.writer)?;
|
||||
write!(&mut self.writer, "{}", value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u8(&mut self, value: u8) -> Result<()> {
|
||||
fn serialize_u32(self, value: u32) -> Result<()> {
|
||||
self.formatter.start_value(&mut self.writer)?;
|
||||
write!(&mut self.writer, "{}", value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u16(&mut self, value: u16) -> Result<()> {
|
||||
fn serialize_u64(self, value: u64) -> Result<()> {
|
||||
self.formatter.start_value(&mut self.writer)?;
|
||||
write!(&mut self.writer, "{}", value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u32(&mut self, value: u32) -> Result<()> {
|
||||
self.formatter.start_value(&mut self.writer)?;
|
||||
write!(&mut self.writer, "{}", value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u64(&mut self, value: u64) -> Result<()> {
|
||||
self.formatter.start_value(&mut self.writer)?;
|
||||
write!(&mut self.writer, "{}", value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_f32(&mut self, value: f32) -> Result<()> {
|
||||
fn serialize_f32(self, value: f32) -> Result<()> {
|
||||
self.formatter.start_value(&mut self.writer)?;
|
||||
fmt_f32_or_null(&mut self.writer, if value == -0f32 { 0f32 } else { value })
|
||||
.map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_f64(&mut self, value: f64) -> Result<()> {
|
||||
fn serialize_f64(self, value: f64) -> Result<()> {
|
||||
self.formatter.start_value(&mut self.writer)?;
|
||||
fmt_f64_or_null(&mut self.writer, if value == -0f64 { 0f64 } else { value })
|
||||
.map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_char(&mut self, value: char) -> Result<()> {
|
||||
fn serialize_char(self, value: char) -> Result<()> {
|
||||
self.formatter.start_value(&mut self.writer)?;
|
||||
escape_char(&mut self.writer, value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_str(&mut self, value: &str) -> Result<()> {
|
||||
fn serialize_str(self, value: &str) -> Result<()> {
|
||||
quote_str(&mut self.writer, &mut self.formatter, value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_bytes(&mut self, value: &[u8]) -> Result<()> {
|
||||
let mut state = self.serialize_seq(Some(value.len()))?;
|
||||
fn serialize_bytes(self, value: &[u8]) -> Result<()> {
|
||||
let mut seq = self.serialize_seq(Some(value.len()))?;
|
||||
for byte in value {
|
||||
self.serialize_seq_elt(&mut state, byte)?;
|
||||
ser::SerializeSeq::serialize_element(&mut seq, byte)?
|
||||
}
|
||||
self.serialize_seq_end(state)
|
||||
ser::SerializeSeq::end(seq)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_unit(&mut self) -> Result<()> {
|
||||
fn serialize_unit(self) -> Result<()> {
|
||||
self.formatter.start_value(&mut self.writer)?;
|
||||
self.writer.write_all(b"null").map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_unit_struct(&mut self, _name: &'static str) -> Result<()> {
|
||||
fn serialize_unit_struct(self, _name: &'static str) -> Result<()> {
|
||||
self.serialize_unit()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_unit_variant(
|
||||
&mut self,
|
||||
self,
|
||||
_name: &'static str,
|
||||
_variant_index: usize,
|
||||
_variant_index: u32,
|
||||
variant: &'static str,
|
||||
) -> Result<()> {
|
||||
self.serialize_str(variant)
|
||||
@ -201,127 +196,80 @@ where
|
||||
|
||||
/// Serialize newtypes without an object wrapper.
|
||||
#[inline]
|
||||
fn serialize_newtype_struct<T>(&mut self, _name: &'static str, value: T) -> Result<()>
|
||||
fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<()>
|
||||
where
|
||||
T: ser::Serialize,
|
||||
T: ?Sized + ser::Serialize,
|
||||
{
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_newtype_variant<T>(
|
||||
&mut self,
|
||||
self,
|
||||
_name: &'static str,
|
||||
_variant_index: usize,
|
||||
_variant_index: u32,
|
||||
variant: &'static str,
|
||||
value: T,
|
||||
value: &T,
|
||||
) -> Result<()>
|
||||
where
|
||||
T: ser::Serialize,
|
||||
T: ?Sized + ser::Serialize,
|
||||
{
|
||||
self.formatter.open(&mut self.writer, b'{')?;
|
||||
self.formatter.comma(&mut self.writer, true)?;
|
||||
escape_key(&mut self.writer, variant)?;
|
||||
self.formatter.colon(&mut self.writer)?;
|
||||
value.serialize(self)?;
|
||||
value.serialize(&mut *self)?;
|
||||
self.formatter.close(&mut self.writer, b'}')
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_none(&mut self) -> Result<()> {
|
||||
fn serialize_none(self) -> Result<()> {
|
||||
self.serialize_unit()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_some<V>(&mut self, value: V) -> Result<()>
|
||||
fn serialize_some<V>(self, value: &V) -> Result<()>
|
||||
where
|
||||
V: ser::Serialize,
|
||||
V: ?Sized + ser::Serialize,
|
||||
{
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_seq(&mut self, len: Option<usize>) -> Result<State> {
|
||||
if len == Some(0) {
|
||||
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
|
||||
let state = if len == Some(0) {
|
||||
self.formatter.start_value(&mut self.writer)?;
|
||||
self.writer.write_all(b"[]")?;
|
||||
Ok(State::Empty)
|
||||
State::Empty
|
||||
} else {
|
||||
self.formatter.open(&mut self.writer, b'[')?;
|
||||
Ok(State::First)
|
||||
}
|
||||
State::First
|
||||
};
|
||||
Ok(Compound { ser: self, state })
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_seq_elt<T: ser::Serialize>(&mut self, state: &mut State, value: T) -> Result<()>
|
||||
where
|
||||
T: ser::Serialize,
|
||||
{
|
||||
self.formatter
|
||||
.comma(&mut self.writer, *state == State::First)?;
|
||||
*state = State::Rest;
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_seq_end(&mut self, state: State) -> Result<()> {
|
||||
match state {
|
||||
State::Empty => Ok(()),
|
||||
_ => self.formatter.close(&mut self.writer, b']'),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_seq_fixed_size(&mut self, size: usize) -> Result<State> {
|
||||
self.serialize_seq(Some(size))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_tuple(&mut self, len: usize) -> Result<State> {
|
||||
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
|
||||
self.serialize_seq(Some(len))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_tuple_elt<T: ser::Serialize>(
|
||||
&mut self,
|
||||
state: &mut State,
|
||||
value: T,
|
||||
) -> Result<()> {
|
||||
self.serialize_seq_elt(state, value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_tuple_end(&mut self, state: State) -> Result<()> {
|
||||
self.serialize_seq_end(state)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_tuple_struct(&mut self, _name: &'static str, len: usize) -> Result<State> {
|
||||
fn serialize_tuple_struct(
|
||||
self,
|
||||
_name: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeTupleStruct> {
|
||||
self.serialize_seq(Some(len))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_tuple_struct_elt<T: ser::Serialize>(
|
||||
&mut self,
|
||||
state: &mut State,
|
||||
value: T,
|
||||
) -> Result<()> {
|
||||
self.serialize_seq_elt(state, value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_tuple_struct_end(&mut self, state: State) -> Result<()> {
|
||||
self.serialize_seq_end(state)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_tuple_variant(
|
||||
&mut self,
|
||||
self,
|
||||
_name: &'static str,
|
||||
_variant_index: usize,
|
||||
_variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
) -> Result<State> {
|
||||
) -> Result<Self::SerializeTupleVariant> {
|
||||
self.formatter.open(&mut self.writer, b'{')?;
|
||||
self.formatter.comma(&mut self.writer, true)?;
|
||||
escape_key(&mut self.writer, variant)?;
|
||||
@ -330,106 +278,208 @@ where
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_tuple_variant_elt<T: ser::Serialize>(
|
||||
&mut self,
|
||||
state: &mut State,
|
||||
value: T,
|
||||
) -> Result<()> {
|
||||
self.serialize_seq_elt(state, value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_tuple_variant_end(&mut self, state: State) -> Result<()> {
|
||||
self.serialize_seq_end(state)?;
|
||||
self.formatter.close(&mut self.writer, b'}')
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_map(&mut self, len: Option<usize>) -> Result<State> {
|
||||
if len == Some(0) {
|
||||
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> {
|
||||
let state = if len == Some(0) {
|
||||
self.formatter.start_value(&mut self.writer)?;
|
||||
self.writer.write_all(b"{}")?;
|
||||
Ok(State::Empty)
|
||||
State::Empty
|
||||
} else {
|
||||
self.formatter.open(&mut self.writer, b'{')?;
|
||||
Ok(State::First)
|
||||
}
|
||||
State::First
|
||||
};
|
||||
Ok(Compound { ser: self, state })
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_map_key<T: ser::Serialize>(&mut self, state: &mut State, key: T) -> Result<()> {
|
||||
self.formatter
|
||||
.comma(&mut self.writer, *state == State::First)?;
|
||||
*state = State::Rest;
|
||||
|
||||
key.serialize(&mut MapKeySerializer { ser: self })?;
|
||||
|
||||
self.formatter.colon(&mut self.writer)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_map_value<T: ser::Serialize>(&mut self, _: &mut State, value: T) -> Result<()> {
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_map_end(&mut self, state: State) -> Result<()> {
|
||||
match state {
|
||||
State::Empty => Ok(()),
|
||||
_ => self.formatter.close(&mut self.writer, b'}'),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_struct(&mut self, _name: &'static str, len: usize) -> Result<State> {
|
||||
fn serialize_struct(self, _name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
|
||||
self.serialize_map(Some(len))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_struct_elt<V: ser::Serialize>(
|
||||
&mut self,
|
||||
state: &mut State,
|
||||
key: &'static str,
|
||||
value: V,
|
||||
) -> Result<()> {
|
||||
self.serialize_map_key(state, key)?;
|
||||
self.serialize_map_value(state, value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_struct_end(&mut self, state: State) -> Result<()> {
|
||||
self.serialize_map_end(state)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_struct_variant(
|
||||
&mut self,
|
||||
self,
|
||||
_name: &'static str,
|
||||
_variant_index: usize,
|
||||
_variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
) -> Result<State> {
|
||||
) -> Result<Self::SerializeStructVariant> {
|
||||
self.formatter.open(&mut self.writer, b'{')?;
|
||||
self.formatter.comma(&mut self.writer, true)?;
|
||||
escape_key(&mut self.writer, variant)?;
|
||||
self.formatter.colon(&mut self.writer)?;
|
||||
self.serialize_map(Some(len))
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_struct_variant_elt<V: ser::Serialize>(
|
||||
&mut self,
|
||||
state: &mut State,
|
||||
key: &'static str,
|
||||
value: V,
|
||||
) -> Result<()> {
|
||||
self.serialize_struct_elt(state, key, value)
|
||||
impl<'a, W, F> ser::SerializeSeq for Compound<'a, W, F>
|
||||
where
|
||||
W: io::Write,
|
||||
F: Formatter,
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<()>
|
||||
where
|
||||
T: serde::Serialize,
|
||||
{
|
||||
self.ser
|
||||
.formatter
|
||||
.comma(&mut self.ser.writer, self.state == State::First)?;
|
||||
self.state = State::Rest;
|
||||
value.serialize(&mut *self.ser)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_struct_variant_end(&mut self, state: State) -> Result<()> {
|
||||
self.serialize_struct_end(state)?;
|
||||
self.formatter.close(&mut self.writer, b'}')
|
||||
fn end(self) -> Result<Self::Ok> {
|
||||
match self.state {
|
||||
State::Empty => Ok(()),
|
||||
_ => self.ser.formatter.close(&mut self.ser.writer, b']'),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, W, F> ser::SerializeTuple for Compound<'a, W, F>
|
||||
where
|
||||
W: io::Write,
|
||||
F: Formatter,
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<()>
|
||||
where
|
||||
T: serde::Serialize,
|
||||
{
|
||||
ser::SerializeSeq::serialize_element(self, value)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<Self::Ok> {
|
||||
ser::SerializeSeq::end(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, W, F> ser::SerializeTupleStruct for Compound<'a, W, F>
|
||||
where
|
||||
W: io::Write,
|
||||
F: Formatter,
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<()>
|
||||
where
|
||||
T: serde::Serialize,
|
||||
{
|
||||
ser::SerializeSeq::serialize_element(self, value)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<Self::Ok> {
|
||||
ser::SerializeSeq::end(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, W, F> ser::SerializeTupleVariant for Compound<'a, W, F>
|
||||
where
|
||||
W: io::Write,
|
||||
F: Formatter,
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<()>
|
||||
where
|
||||
T: serde::Serialize,
|
||||
{
|
||||
ser::SerializeSeq::serialize_element(self, value)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<Self::Ok> {
|
||||
match self.state {
|
||||
State::Empty => {}
|
||||
_ => self.ser.formatter.close(&mut self.ser.writer, b']')?,
|
||||
}
|
||||
self.ser.formatter.close(&mut self.ser.writer, b'}')
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, W, F> ser::SerializeMap for Compound<'a, W, F>
|
||||
where
|
||||
W: io::Write,
|
||||
F: Formatter,
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<()>
|
||||
where
|
||||
T: serde::Serialize,
|
||||
{
|
||||
self.ser
|
||||
.formatter
|
||||
.comma(&mut self.ser.writer, self.state == State::First)?;
|
||||
self.state = State::Rest;
|
||||
|
||||
key.serialize(MapKeySerializer { ser: self.ser })?;
|
||||
|
||||
self.ser.formatter.colon(&mut self.ser.writer)
|
||||
}
|
||||
|
||||
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<()>
|
||||
where
|
||||
T: serde::Serialize,
|
||||
{
|
||||
value.serialize(&mut *self.ser)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<Self::Ok> {
|
||||
match self.state {
|
||||
State::Empty => Ok(()),
|
||||
_ => self.ser.formatter.close(&mut self.ser.writer, b'}'),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, W, F> ser::SerializeStruct for Compound<'a, W, F>
|
||||
where
|
||||
W: io::Write,
|
||||
F: Formatter,
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<()>
|
||||
where
|
||||
T: serde::Serialize,
|
||||
{
|
||||
ser::SerializeMap::serialize_entry(self, key, value)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<Self::Ok> {
|
||||
ser::SerializeMap::end(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, W, F> ser::SerializeStructVariant for Compound<'a, W, F>
|
||||
where
|
||||
W: io::Write,
|
||||
F: Formatter,
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<()>
|
||||
where
|
||||
T: serde::Serialize,
|
||||
{
|
||||
ser::SerializeStruct::serialize_field(self, key, value)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<Self::Ok> {
|
||||
match self.state {
|
||||
State::Empty => {}
|
||||
_ => self.ser.formatter.close(&mut self.ser.writer, b'}')?,
|
||||
}
|
||||
self.ser.formatter.close(&mut self.ser.writer, b'}')
|
||||
}
|
||||
}
|
||||
|
||||
@ -442,251 +492,163 @@ where
|
||||
W: io::Write,
|
||||
F: Formatter,
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn serialize_str(&mut self, value: &str) -> Result<()> {
|
||||
fn serialize_str(self, value: &str) -> Result<()> {
|
||||
escape_key(&mut self.ser.writer, value).map_err(From::from)
|
||||
}
|
||||
|
||||
type SeqState = ();
|
||||
type TupleState = ();
|
||||
type TupleStructState = ();
|
||||
type TupleVariantState = ();
|
||||
type MapState = ();
|
||||
type StructState = ();
|
||||
type StructVariantState = ();
|
||||
type SerializeSeq = ser::Impossible<(), Error>;
|
||||
type SerializeTuple = ser::Impossible<(), Error>;
|
||||
type SerializeTupleStruct = ser::Impossible<(), Error>;
|
||||
type SerializeTupleVariant = ser::Impossible<(), Error>;
|
||||
type SerializeMap = ser::Impossible<(), Error>;
|
||||
type SerializeStruct = ser::Impossible<(), Error>;
|
||||
type SerializeStructVariant = ser::Impossible<(), Error>;
|
||||
|
||||
fn serialize_bool(&mut self, _value: bool) -> Result<()> {
|
||||
fn serialize_bool(self, _value: bool) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_isize(&mut self, _value: isize) -> Result<()> {
|
||||
fn serialize_i8(self, _value: i8) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_i8(&mut self, _value: i8) -> Result<()> {
|
||||
fn serialize_i16(self, _value: i16) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_i16(&mut self, _value: i16) -> Result<()> {
|
||||
fn serialize_i32(self, _value: i32) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_i32(&mut self, _value: i32) -> Result<()> {
|
||||
fn serialize_i64(self, _value: i64) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_i64(&mut self, _value: i64) -> Result<()> {
|
||||
fn serialize_u8(self, _value: u8) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_usize(&mut self, _value: usize) -> Result<()> {
|
||||
fn serialize_u16(self, _value: u16) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_u8(&mut self, _value: u8) -> Result<()> {
|
||||
fn serialize_u32(self, _value: u32) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_u16(&mut self, _value: u16) -> Result<()> {
|
||||
fn serialize_u64(self, _value: u64) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_u32(&mut self, _value: u32) -> Result<()> {
|
||||
fn serialize_f32(self, _value: f32) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_u64(&mut self, _value: u64) -> Result<()> {
|
||||
fn serialize_f64(self, _value: f64) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_f32(&mut self, _value: f32) -> Result<()> {
|
||||
fn serialize_char(self, _value: char) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_f64(&mut self, _value: f64) -> Result<()> {
|
||||
fn serialize_bytes(self, _value: &[u8]) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_char(&mut self, _value: char) -> Result<()> {
|
||||
fn serialize_unit(self) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_bytes(&mut self, _value: &[u8]) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_unit(&mut self) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_unit_struct(&mut self, _name: &'static str) -> Result<()> {
|
||||
fn serialize_unit_struct(self, _name: &'static str) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_unit_variant(
|
||||
&mut self,
|
||||
self,
|
||||
_name: &'static str,
|
||||
_variant_index: usize,
|
||||
_variant_index: u32,
|
||||
_variant: &'static str,
|
||||
) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_newtype_struct<T>(&mut self, _name: &'static str, _value: T) -> Result<()>
|
||||
fn serialize_newtype_struct<T>(self, _name: &'static str, _value: &T) -> Result<()>
|
||||
where
|
||||
T: ser::Serialize,
|
||||
T: ?Sized + ser::Serialize,
|
||||
{
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_newtype_variant<T>(
|
||||
&mut self,
|
||||
self,
|
||||
_name: &'static str,
|
||||
_variant_index: usize,
|
||||
_variant_index: u32,
|
||||
_variant: &'static str,
|
||||
_value: T,
|
||||
_value: &T,
|
||||
) -> Result<()>
|
||||
where
|
||||
T: ser::Serialize,
|
||||
T: ?Sized + ser::Serialize,
|
||||
{
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_none(&mut self) -> Result<()> {
|
||||
fn serialize_none(self) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_some<T>(&mut self, _value: T) -> Result<()>
|
||||
fn serialize_some<T>(self, _value: &T) -> Result<()>
|
||||
where
|
||||
T: ser::Serialize,
|
||||
T: ?Sized + ser::Serialize,
|
||||
{
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_seq(&mut self, _len: Option<usize>) -> Result<()> {
|
||||
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeStruct> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_seq_elt<T: ser::Serialize>(&mut self, _state: &mut (), _value: T) -> Result<()>
|
||||
where
|
||||
T: ser::Serialize,
|
||||
{
|
||||
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_seq_end(&mut self, _state: ()) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_seq_fixed_size(&mut self, _size: usize) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_tuple(&mut self, _len: usize) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_tuple_elt<T: ser::Serialize>(&mut self, _state: &mut (), _value: T) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_tuple_end(&mut self, _state: ()) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct(&mut self, _name: &'static str, _len: usize) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct_elt<T: ser::Serialize>(
|
||||
&mut self,
|
||||
_state: &mut (),
|
||||
_value: T,
|
||||
) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct_end(&mut self, _state: ()) -> Result<()> {
|
||||
fn serialize_tuple_struct(
|
||||
self,
|
||||
_name: &'static str,
|
||||
_len: usize,
|
||||
) -> Result<Self::SerializeTupleStruct> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_tuple_variant(
|
||||
&mut self,
|
||||
self,
|
||||
_name: &'static str,
|
||||
_variant_index: usize,
|
||||
_variant_index: u32,
|
||||
_variant: &'static str,
|
||||
_len: usize,
|
||||
) -> Result<()> {
|
||||
) -> Result<Self::SerializeTupleVariant> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_tuple_variant_elt<T: ser::Serialize>(
|
||||
&mut self,
|
||||
_state: &mut (),
|
||||
_value: T,
|
||||
) -> Result<()> {
|
||||
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_tuple_variant_end(&mut self, _state: ()) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_map(&mut self, _len: Option<usize>) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_map_key<T: ser::Serialize>(&mut self, _state: &mut (), _key: T) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_map_value<T: ser::Serialize>(&mut self, _state: &mut (), _value: T) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_map_end(&mut self, _state: ()) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_struct(&mut self, _name: &'static str, _len: usize) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_struct_elt<V: ser::Serialize>(
|
||||
&mut self,
|
||||
_state: &mut (),
|
||||
_key: &'static str,
|
||||
_value: V,
|
||||
) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_struct_end(&mut self, _state: ()) -> Result<()> {
|
||||
fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_struct_variant(
|
||||
&mut self,
|
||||
self,
|
||||
_name: &'static str,
|
||||
_variant_index: usize,
|
||||
_variant_index: u32,
|
||||
_variant: &'static str,
|
||||
_len: usize,
|
||||
) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_struct_variant_elt<V: ser::Serialize>(
|
||||
&mut self,
|
||||
_state: &mut (),
|
||||
_key: &'static str,
|
||||
_value: V,
|
||||
) -> Result<()> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn serialize_struct_variant_end(&mut self, _state: ()) -> Result<()> {
|
||||
) -> Result<Self::SerializeStructVariant> {
|
||||
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
}
|
||||
|
@ -58,6 +58,10 @@ where
|
||||
Ok(Some(self.ch[idx]))
|
||||
}
|
||||
|
||||
// pub fn peek_next_or_null(&mut self, idx: usize) -> Result<u8> {
|
||||
// Ok(try!(self.peek_next(idx)).unwrap_or(b'\x00'))
|
||||
// }
|
||||
|
||||
pub fn peek(&mut self) -> Result<Option<u8>> {
|
||||
self.peek_next(0)
|
||||
}
|
||||
@ -234,6 +238,7 @@ impl<Iter: Iterator<Item = u8>> ParseNumber<Iter> {
|
||||
if self.rdr.peek_or_null()? == b'0' {
|
||||
self.result.push(self.rdr.eat_char());
|
||||
has_value = true;
|
||||
|
||||
// There can be only one leading '0'.
|
||||
if let b'0'..=b'9' = self.rdr.peek_or_null()? {
|
||||
return Err(Error::Syntax(ErrorCode::InvalidNumber, 0, 0));
|
||||
|
File diff suppressed because it is too large
Load Diff
214
crates/nu-json/tests/main.rs
Normal file
214
crates/nu-json/tests/main.rs
Normal file
@ -0,0 +1,214 @@
|
||||
extern crate nu_json;
|
||||
extern crate nu_test_support;
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
|
||||
use nu_json::Value;
|
||||
use regex::Regex;
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
fn txt(text: &str) -> String {
|
||||
let out = String::from_utf8_lossy(text.as_bytes());
|
||||
|
||||
#[cfg(windows)]
|
||||
{
|
||||
out.replace("\r\n", "").replace("\n", "")
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
{
|
||||
out.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
fn hjson_expectations() -> PathBuf {
|
||||
let assets = nu_test_support::fs::assets().join("nu_json");
|
||||
|
||||
dunce::canonicalize(assets.clone()).unwrap_or_else(|e| {
|
||||
panic!(
|
||||
"Couldn't canonicalize hjson assets path {}: {:?}",
|
||||
assets.display(),
|
||||
e
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn get_test_content(name: &str) -> io::Result<String> {
|
||||
let expectations = hjson_expectations();
|
||||
|
||||
let mut p = format!("{}/{}_test.hjson", expectations.display(), name);
|
||||
|
||||
if !Path::new(&p).exists() {
|
||||
p = format!("{}/{}_test.json", expectations.display(), name);
|
||||
}
|
||||
|
||||
fs::read_to_string(&p)
|
||||
}
|
||||
|
||||
fn get_result_content(name: &str) -> io::Result<(String, String)> {
|
||||
let expectations = hjson_expectations();
|
||||
|
||||
let p1 = format!("{}/{}_result.json", expectations.display(), name);
|
||||
let p2 = format!("{}/{}_result.hjson", expectations.display(), name);
|
||||
|
||||
Ok((fs::read_to_string(&p1)?, fs::read_to_string(&p2)?))
|
||||
}
|
||||
|
||||
macro_rules! run_test {
|
||||
// {{ is a workaround for rust stable
|
||||
($v: ident, $list: expr, $fix: expr) => {{
|
||||
let name = stringify!($v);
|
||||
$list.push(format!("{}_test", name));
|
||||
println!("- running {}", name);
|
||||
let should_fail = name.starts_with("fail");
|
||||
let test_content = get_test_content(name).unwrap();
|
||||
let data: nu_json::Result<Value> = nu_json::from_str(&test_content);
|
||||
assert!(should_fail == data.is_err());
|
||||
|
||||
if !should_fail {
|
||||
let udata = data.unwrap();
|
||||
let (rjson, rhjson) = get_result_content(name).unwrap();
|
||||
let rjson = txt(&rjson);
|
||||
let rhjson = txt(&rhjson);
|
||||
let actual_hjson = nu_json::to_string(&udata).unwrap();
|
||||
let actual_hjson = txt(&actual_hjson);
|
||||
let actual_json = $fix(serde_json::to_string_pretty(&udata).unwrap());
|
||||
let actual_json = txt(&actual_json);
|
||||
if rhjson != actual_hjson {
|
||||
println!(
|
||||
"{:?}\n---hjson expected\n{}\n---hjson actual\n{}\n---\n",
|
||||
name, rhjson, actual_hjson
|
||||
);
|
||||
}
|
||||
if rjson != actual_json {
|
||||
println!(
|
||||
"{:?}\n---json expected\n{}\n---json actual\n{}\n---\n",
|
||||
name, rjson, actual_json
|
||||
);
|
||||
}
|
||||
assert!(rhjson == actual_hjson && rjson == actual_json);
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
// add fixes where rust's json differs from javascript
|
||||
|
||||
fn std_fix(json: String) -> String {
|
||||
// serde_json serializes integers with a superfluous .0 suffix
|
||||
let re = Regex::new(r"(?m)(?P<d>\d)\.0(?P<s>,?)$").unwrap();
|
||||
re.replace_all(&json, "$d$s").to_string()
|
||||
}
|
||||
|
||||
fn fix_kan(json: String) -> String {
|
||||
std_fix(json).replace(" -0,", " 0,")
|
||||
}
|
||||
|
||||
fn fix_pass1(json: String) -> String {
|
||||
std_fix(json)
|
||||
.replace("1.23456789e34", "1.23456789e+34")
|
||||
.replace("2.3456789012e76", "2.3456789012e+76")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hjson() {
|
||||
let mut done: Vec<String> = Vec::new();
|
||||
|
||||
println!("");
|
||||
run_test!(charset, done, std_fix);
|
||||
run_test!(comments, done, std_fix);
|
||||
run_test!(empty, done, std_fix);
|
||||
run_test!(failCharset1, done, std_fix);
|
||||
run_test!(failJSON02, done, std_fix);
|
||||
run_test!(failJSON05, done, std_fix);
|
||||
run_test!(failJSON06, done, std_fix);
|
||||
run_test!(failJSON07, done, std_fix);
|
||||
run_test!(failJSON08, done, std_fix);
|
||||
run_test!(failJSON10, done, std_fix);
|
||||
run_test!(failJSON11, done, std_fix);
|
||||
run_test!(failJSON12, done, std_fix);
|
||||
run_test!(failJSON13, done, std_fix);
|
||||
run_test!(failJSON14, done, std_fix);
|
||||
run_test!(failJSON15, done, std_fix);
|
||||
run_test!(failJSON16, done, std_fix);
|
||||
run_test!(failJSON17, done, std_fix);
|
||||
run_test!(failJSON19, done, std_fix);
|
||||
run_test!(failJSON20, done, std_fix);
|
||||
run_test!(failJSON21, done, std_fix);
|
||||
run_test!(failJSON22, done, std_fix);
|
||||
run_test!(failJSON23, done, std_fix);
|
||||
run_test!(failJSON24, done, std_fix);
|
||||
run_test!(failJSON26, done, std_fix);
|
||||
run_test!(failJSON28, done, std_fix);
|
||||
run_test!(failJSON29, done, std_fix);
|
||||
run_test!(failJSON30, done, std_fix);
|
||||
run_test!(failJSON31, done, std_fix);
|
||||
run_test!(failJSON32, done, std_fix);
|
||||
run_test!(failJSON33, done, std_fix);
|
||||
run_test!(failJSON34, done, std_fix);
|
||||
run_test!(failKey1, done, std_fix);
|
||||
run_test!(failKey2, done, std_fix);
|
||||
run_test!(failKey3, done, std_fix);
|
||||
run_test!(failKey4, done, std_fix);
|
||||
run_test!(failMLStr1, done, std_fix);
|
||||
run_test!(failObj1, done, std_fix);
|
||||
run_test!(failObj2, done, std_fix);
|
||||
run_test!(failObj3, done, std_fix);
|
||||
run_test!(failStr1a, done, std_fix);
|
||||
run_test!(failStr1b, done, std_fix);
|
||||
run_test!(failStr1c, done, std_fix);
|
||||
run_test!(failStr1d, done, std_fix);
|
||||
run_test!(failStr2a, done, std_fix);
|
||||
run_test!(failStr2b, done, std_fix);
|
||||
run_test!(failStr2c, done, std_fix);
|
||||
run_test!(failStr2d, done, std_fix);
|
||||
run_test!(failStr3a, done, std_fix);
|
||||
run_test!(failStr3b, done, std_fix);
|
||||
run_test!(failStr3c, done, std_fix);
|
||||
run_test!(failStr3d, done, std_fix);
|
||||
run_test!(failStr4a, done, std_fix);
|
||||
run_test!(failStr4b, done, std_fix);
|
||||
run_test!(failStr4c, done, std_fix);
|
||||
run_test!(failStr4d, done, std_fix);
|
||||
run_test!(failStr5a, done, std_fix);
|
||||
run_test!(failStr5b, done, std_fix);
|
||||
run_test!(failStr5c, done, std_fix);
|
||||
run_test!(failStr5d, done, std_fix);
|
||||
run_test!(failStr6a, done, std_fix);
|
||||
run_test!(failStr6b, done, std_fix);
|
||||
run_test!(failStr6c, done, std_fix);
|
||||
run_test!(failStr6d, done, std_fix);
|
||||
run_test!(kan, done, fix_kan);
|
||||
run_test!(keys, done, std_fix);
|
||||
run_test!(oa, done, std_fix);
|
||||
run_test!(pass1, done, fix_pass1);
|
||||
run_test!(pass2, done, std_fix);
|
||||
run_test!(pass3, done, std_fix);
|
||||
run_test!(pass4, done, std_fix);
|
||||
run_test!(passSingle, done, std_fix);
|
||||
run_test!(root, done, std_fix);
|
||||
run_test!(stringify1, done, std_fix);
|
||||
run_test!(strings, done, std_fix);
|
||||
run_test!(trail, done, std_fix);
|
||||
|
||||
// check if we include all assets
|
||||
let paths = fs::read_dir(hjson_expectations()).unwrap();
|
||||
|
||||
let all = paths
|
||||
.map(|item| String::from(item.unwrap().path().file_stem().unwrap().to_str().unwrap()))
|
||||
.filter(|x| x.contains("_test"))
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
let missing = all
|
||||
.into_iter()
|
||||
.filter(|x| done.iter().find(|y| &x == y) == None)
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
if missing.len() > 0 {
|
||||
for item in missing {
|
||||
println!("missing: {}", item);
|
||||
}
|
||||
assert!(false);
|
||||
}
|
||||
}
|
@ -256,6 +256,10 @@ pub fn fixtures() -> PathBuf {
|
||||
root().join("tests/fixtures")
|
||||
}
|
||||
|
||||
pub fn assets() -> PathBuf {
|
||||
root().join("tests/assets")
|
||||
}
|
||||
|
||||
pub fn in_directory(str: impl AsRef<Path>) -> String {
|
||||
let path = str.as_ref();
|
||||
let path = if path.is_relative() {
|
||||
|
Reference in New Issue
Block a user