From 2aadc03ef15cb5ca5cc2af8a7c08e070742f0ac4 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 4 May 2024 14:47:55 +0200 Subject: Adding upstream version 0.70.1+ds1. Signed-off-by: Daniel Baumann --- vendor/serde-value/src/de.rs | 541 ++++++++++++++++++++++++++++++++++++++++++ vendor/serde-value/src/lib.rs | 384 ++++++++++++++++++++++++++++++ vendor/serde-value/src/ser.rs | 438 ++++++++++++++++++++++++++++++++++ 3 files changed, 1363 insertions(+) create mode 100644 vendor/serde-value/src/de.rs create mode 100644 vendor/serde-value/src/lib.rs create mode 100644 vendor/serde-value/src/ser.rs (limited to 'vendor/serde-value/src') diff --git a/vendor/serde-value/src/de.rs b/vendor/serde-value/src/de.rs new file mode 100644 index 0000000..97dc115 --- /dev/null +++ b/vendor/serde-value/src/de.rs @@ -0,0 +1,541 @@ +use serde::{forward_to_deserialize_any, de}; +use std::collections::BTreeMap; +use std::error::Error; +use std::fmt; +use std::marker::PhantomData; + +use crate::Value; + +#[derive(Debug)] +pub enum Unexpected { + Bool(bool), + Unsigned(u64), + Signed(i64), + Float(f64), + Char(char), + Str(String), + Bytes(Vec), + Unit, + Option, + NewtypeStruct, + Seq, + Map, + Enum, + UnitVariant, + NewtypeVariant, + TupleVariant, + StructVariant, + Other(String), +} + +impl<'a> From> for Unexpected { + fn from(unexp: de::Unexpected) -> Unexpected { + match unexp { + de::Unexpected::Bool(v) => Unexpected::Bool(v), + de::Unexpected::Unsigned(v) => Unexpected::Unsigned(v), + de::Unexpected::Signed(v) => Unexpected::Signed(v), + de::Unexpected::Float(v) => Unexpected::Float(v), + de::Unexpected::Char(v) => Unexpected::Char(v), + de::Unexpected::Str(v) => Unexpected::Str(v.to_owned()), + de::Unexpected::Bytes(v) => Unexpected::Bytes(v.to_owned()), + de::Unexpected::Unit => Unexpected::Unit, + de::Unexpected::Option => Unexpected::Option, + de::Unexpected::NewtypeStruct => Unexpected::NewtypeStruct, + de::Unexpected::Seq => Unexpected::Seq, + de::Unexpected::Map => Unexpected::Map, + de::Unexpected::Enum => Unexpected::Enum, + de::Unexpected::UnitVariant => Unexpected::UnitVariant, + de::Unexpected::NewtypeVariant => Unexpected::NewtypeVariant, + de::Unexpected::TupleVariant => Unexpected::TupleVariant, + de::Unexpected::StructVariant => Unexpected::StructVariant, + de::Unexpected::Other(v) => Unexpected::Other(v.to_owned()), + } + } +} + +impl Unexpected { + pub fn to_unexpected<'a>(&'a self) -> de::Unexpected<'a> { + match *self { + Unexpected::Bool(v) => de::Unexpected::Bool(v), + Unexpected::Unsigned(v) => de::Unexpected::Unsigned(v), + Unexpected::Signed(v) => de::Unexpected::Signed(v), + Unexpected::Float(v) => de::Unexpected::Float(v), + Unexpected::Char(v) => de::Unexpected::Char(v), + Unexpected::Str(ref v) => de::Unexpected::Str(v), + Unexpected::Bytes(ref v) => de::Unexpected::Bytes(v), + Unexpected::Unit => de::Unexpected::Unit, + Unexpected::Option => de::Unexpected::Option, + Unexpected::NewtypeStruct => de::Unexpected::NewtypeStruct, + Unexpected::Seq => de::Unexpected::Seq, + Unexpected::Map => de::Unexpected::Map, + Unexpected::Enum => de::Unexpected::Enum, + Unexpected::UnitVariant => de::Unexpected::UnitVariant, + Unexpected::NewtypeVariant => de::Unexpected::NewtypeVariant, + Unexpected::TupleVariant => de::Unexpected::TupleVariant, + Unexpected::StructVariant => de::Unexpected::StructVariant, + Unexpected::Other(ref v) => de::Unexpected::Other(v), + } + } + +} + +#[derive(Debug)] +pub enum DeserializerError { + Custom(String), + InvalidType(Unexpected, String), + InvalidValue(Unexpected, String), + InvalidLength(usize, String), + UnknownVariant(String, &'static [&'static str]), + UnknownField(String, &'static [&'static str]), + MissingField(&'static str), + DuplicateField(&'static str), +} + +impl de::Error for DeserializerError { + fn custom(msg: T) -> Self { + DeserializerError::Custom(msg.to_string()) + } + + fn invalid_type(unexp: de::Unexpected, exp: &dyn de::Expected) -> Self { + DeserializerError::InvalidType(unexp.into(), exp.to_string()) + } + + fn invalid_value(unexp: de::Unexpected, exp: &dyn de::Expected) -> Self { + DeserializerError::InvalidValue(unexp.into(), exp.to_string()) + } + + fn invalid_length(len: usize, exp: &dyn de::Expected) -> Self { + DeserializerError::InvalidLength(len, exp.to_string()) + } + + fn unknown_variant(field: &str, expected: &'static [&'static str]) -> Self { + DeserializerError::UnknownVariant(field.into(), expected) + } + + fn unknown_field(field: &str, expected: &'static [&'static str]) -> Self { + DeserializerError::UnknownField(field.into(), expected) + } + + fn missing_field(field: &'static str) -> Self { + DeserializerError::MissingField(field) + } + + fn duplicate_field(field: &'static str) -> Self { + DeserializerError::DuplicateField(field) + } +} + +impl DeserializerError { + pub fn to_error(&self) -> E { + match *self { + DeserializerError::Custom(ref msg) => E::custom(msg.clone()), + DeserializerError::InvalidType(ref unexp, ref exp) => { + E::invalid_type(unexp.to_unexpected(), &&**exp) + } + DeserializerError::InvalidValue(ref unexp, ref exp) => { + E::invalid_value(unexp.to_unexpected(), &&**exp) + } + DeserializerError::InvalidLength(len, ref exp) => E::invalid_length(len, &&**exp), + DeserializerError::UnknownVariant(ref field, exp) => E::unknown_variant(field, exp), + DeserializerError::UnknownField(ref field, exp) => E::unknown_field(field, exp), + DeserializerError::MissingField(field) => E::missing_field(field), + DeserializerError::DuplicateField(field) => E::missing_field(field), + } + } + + pub fn into_error(self) -> E { + self.to_error() + } +} + +impl Error for DeserializerError { + fn description(&self) -> &str { + "Value deserializer error" + } +} + +impl fmt::Display for DeserializerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + DeserializerError::Custom(ref msg) => write!(f, "{}", msg), + DeserializerError::InvalidType(ref unexp, ref exp) => { + write!(f, "Invalid type {}. Expected {}", unexp.to_unexpected(), exp) + } + DeserializerError::InvalidValue(ref unexp, ref exp) => { + write!(f, "Invalid value {}. Expected {}", unexp.to_unexpected(), exp) + } + DeserializerError::InvalidLength(len, ref exp) => { + write!(f, "Invalid length {}. Expected {}", len, exp) + } + DeserializerError::UnknownVariant(ref field, exp) => { + write!(f, "Unknown variant {}. Expected one of {}", field, exp.join(", ")) + }, + DeserializerError::UnknownField(ref field, exp) => { + write!(f, "Unknown field {}. Expected one of {}", field, exp.join(", ")) + } + DeserializerError::MissingField(field) => write!(f, "Missing field {}", field), + DeserializerError::DuplicateField(field) => write!(f, "Duplicate field {}", field), + } + } +} + +impl From for DeserializerError { + fn from(e: de::value::Error) -> DeserializerError { + DeserializerError::Custom(e.to_string()) + } +} + +pub struct ValueVisitor; + +impl<'de> de::Visitor<'de> for ValueVisitor { + type Value = Value; + + fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.write_str("any value") + } + + fn visit_bool(self, value: bool) -> Result { + Ok(Value::Bool(value)) + } + + fn visit_i8(self, value: i8) -> Result { + Ok(Value::I8(value)) + } + + fn visit_i16(self, value: i16) -> Result { + Ok(Value::I16(value)) + } + + fn visit_i32(self, value: i32) -> Result { + Ok(Value::I32(value)) + } + + fn visit_i64(self, value: i64) -> Result { + Ok(Value::I64(value)) + } + + fn visit_u8(self, value: u8) -> Result { + Ok(Value::U8(value)) + } + + fn visit_u16(self, value: u16) -> Result { + Ok(Value::U16(value)) + } + + fn visit_u32(self, value: u32) -> Result { + Ok(Value::U32(value)) + } + + fn visit_u64(self, value: u64) -> Result { + Ok(Value::U64(value)) + } + + fn visit_f32(self, value: f32) -> Result { + Ok(Value::F32(value)) + } + + fn visit_f64(self, value: f64) -> Result { + Ok(Value::F64(value)) + } + + fn visit_char(self, value: char) -> Result { + Ok(Value::Char(value)) + } + + fn visit_str(self, value: &str) -> Result { + Ok(Value::String(value.into())) + } + + fn visit_string(self, value: String) -> Result { + Ok(Value::String(value)) + } + + fn visit_unit(self) -> Result { + Ok(Value::Unit) + } + + fn visit_none(self) -> Result { + Ok(Value::Option(None)) + } + + fn visit_some>(self, d: D) -> Result { + d.deserialize_any(ValueVisitor).map(|v| Value::Option(Some(Box::new(v)))) + } + + fn visit_newtype_struct>(self, d: D) -> Result { + d.deserialize_any(ValueVisitor).map(|v| Value::Newtype(Box::new(v))) + } + + fn visit_seq>(self, mut visitor: V) -> Result { + let mut values = Vec::new(); + while let Some(elem) = visitor.next_element()? { + values.push(elem); + } + Ok(Value::Seq(values)) + } + + fn visit_map>(self, mut visitor: V) -> Result { + let mut values = BTreeMap::new(); + while let Some((key, value)) = visitor.next_entry()? { + values.insert(key, value); + } + Ok(Value::Map(values)) + } + + fn visit_bytes(self, v: &[u8]) -> Result { + Ok(Value::Bytes(v.into())) + } + + fn visit_byte_buf(self, v: Vec) -> Result { + Ok(Value::Bytes(v)) + } +} + +impl<'de> de::Deserialize<'de> for Value { + fn deserialize>(d: D) -> Result { + d.deserialize_any(ValueVisitor) + } +} + +impl<'de> de::IntoDeserializer<'de, DeserializerError> for Value { + type Deserializer = Value; + + fn into_deserializer(self) -> Value { + self + } +} + +pub struct ValueDeserializer { + value: Value, + error: PhantomData E>, +} + +impl ValueDeserializer { + pub fn new(value: Value) -> Self { + ValueDeserializer { + value: value, + error: Default::default(), + } + } + + pub fn into_value(self) -> Value { + self.value + } +} + +impl<'de, E> de::Deserializer<'de> for ValueDeserializer where E: de::Error { + type Error = E; + + fn deserialize_any>(self, visitor: V) -> Result { + match self.value { + Value::Bool(v) => visitor.visit_bool(v), + Value::U8(v) => visitor.visit_u8(v), + Value::U16(v) => visitor.visit_u16(v), + Value::U32(v) => visitor.visit_u32(v), + Value::U64(v) => visitor.visit_u64(v), + Value::I8(v) => visitor.visit_i8(v), + Value::I16(v) => visitor.visit_i16(v), + Value::I32(v) => visitor.visit_i32(v), + Value::I64(v) => visitor.visit_i64(v), + Value::F32(v) => visitor.visit_f32(v), + Value::F64(v) => visitor.visit_f64(v), + Value::Char(v) => visitor.visit_char(v), + Value::String(v) => visitor.visit_string(v), + Value::Unit => visitor.visit_unit(), + Value::Option(None) => visitor.visit_none(), + Value::Option(Some(v)) => visitor.visit_some(ValueDeserializer::new(*v)), + Value::Newtype(v) => visitor.visit_newtype_struct(ValueDeserializer::new(*v)), + Value::Seq(v) => { + visitor.visit_seq(de::value::SeqDeserializer::new(v.into_iter().map(ValueDeserializer::new))) + }, + Value::Map(v) => { + visitor.visit_map(de::value::MapDeserializer::new(v.into_iter().map(|(k, v)| ( + ValueDeserializer::new(k), + ValueDeserializer::new(v), + )))) + }, + Value::Bytes(v) => visitor.visit_byte_buf(v), + } + } + + fn deserialize_option>(self, visitor: V) -> Result { + match self.value { + Value::Option(..) => self.deserialize_any(visitor), + Value::Unit => visitor.visit_unit(), + _ => visitor.visit_some(self) + } + } + + fn deserialize_enum>(self, + _name: &'static str, + _variants: &'static [&'static str], + visitor: V) + -> Result { + let (variant, value) = match self.value { + Value::Map(value) => { + let mut iter = value.into_iter(); + let (variant, value) = match iter.next() { + Some(v) => v, + None => { + return Err(de::Error::invalid_value(de::Unexpected::Map, + &"map with a single key")); + } + }; + // enums are encoded as maps with a single key:value pair + if iter.next().is_some() { + return Err(de::Error::invalid_value(de::Unexpected::Map, + &"map with a single key")); + } + (variant, Some(value)) + } + Value::String(variant) => (Value::String(variant), None), + other => { + return Err(de::Error::invalid_type(other.unexpected(), &"string or map")); + } + }; + + let d = EnumDeserializer { + variant: variant, + value: value, + error: Default::default(), + }; + visitor.visit_enum(d) + } + + fn deserialize_newtype_struct>(self, + _name: &'static str, + visitor: V) + -> Result { + match self.value { + Value::Newtype(v) => visitor.visit_newtype_struct(ValueDeserializer::new(*v)), + _ => visitor.visit_newtype_struct(self), + } + } + + forward_to_deserialize_any! { + bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit + seq bytes byte_buf map unit_struct + tuple_struct struct tuple ignored_any identifier + } +} + +impl<'de, E> de::IntoDeserializer<'de, E> for ValueDeserializer where E: de::Error { + type Deserializer = Self; + + fn into_deserializer(self) -> Self::Deserializer { + self + } +} + +impl<'de> de::Deserializer<'de> for Value { + type Error = DeserializerError; + + fn deserialize_any>(self, visitor: V) -> Result { + ValueDeserializer::new(self).deserialize_any(visitor) + } + + fn deserialize_option>(self, visitor: V) -> Result { + ValueDeserializer::new(self).deserialize_option(visitor) + } + + fn deserialize_enum>(self, + name: &'static str, + variants: &'static [&'static str], + visitor: V) + -> Result { + ValueDeserializer::new(self).deserialize_enum(name, variants, visitor) + } + + fn deserialize_newtype_struct>(self, + name: &'static str, + visitor: V) + -> Result { + ValueDeserializer::new(self).deserialize_newtype_struct(name, visitor) + } + + forward_to_deserialize_any! { + bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit + seq bytes byte_buf map unit_struct + tuple_struct struct tuple ignored_any identifier + } +} + +struct EnumDeserializer { + variant: Value, + value: Option, + error: PhantomData E>, +} + +impl<'de, E> de::EnumAccess<'de> for EnumDeserializer where E: de::Error { + type Error = E; + type Variant = VariantDeserializer; + + fn variant_seed(self, seed: V) -> Result<(V::Value, VariantDeserializer), Self::Error> + where V: de::DeserializeSeed<'de> + { + let visitor = VariantDeserializer { + value: self.value, + error: Default::default(), + }; + seed.deserialize(ValueDeserializer::new(self.variant)).map(|v| (v, visitor)) + } +} + +struct VariantDeserializer { + value: Option, + error: PhantomData E>, +} + +impl<'de, E> de::VariantAccess<'de> for VariantDeserializer where E: de::Error { + type Error = E; + + fn unit_variant(self) -> Result<(), Self::Error> { + match self.value { + Some(value) => de::Deserialize::deserialize(ValueDeserializer::new(value)), + None => Ok(()), + } + } + + fn newtype_variant_seed(self, seed: T) -> Result + where T: de::DeserializeSeed<'de> + { + match self.value { + Some(value) => seed.deserialize(ValueDeserializer::new(value)), + None => Err(de::Error::invalid_type(de::Unexpected::UnitVariant, &"newtype variant")), + } + } + + fn tuple_variant(self, _len: usize, visitor: V) -> Result + where V: de::Visitor<'de> + { + match self.value { + Some(Value::Seq(v)) => { + de::Deserializer::deserialize_any( + de::value::SeqDeserializer::new(v.into_iter().map(ValueDeserializer::new)), + visitor) + } + Some(other) => Err(de::Error::invalid_type(other.unexpected(), &"tuple variant")), + None => Err(de::Error::invalid_type(de::Unexpected::UnitVariant, &"tuple variant")), + } + } + + fn struct_variant(self, + _fields: &'static [&'static str], + visitor: V) + -> Result + where V: de::Visitor<'de> + { + match self.value { + Some(Value::Map(v)) => { + de::Deserializer::deserialize_any( + de::value::MapDeserializer::new(v.into_iter().map(|(k, v)| ( + ValueDeserializer::new(k), + ValueDeserializer::new(v), + ))), + visitor) + } + Some(other) => Err(de::Error::invalid_type(other.unexpected(), &"struct variant")), + None => Err(de::Error::invalid_type(de::Unexpected::UnitVariant, &"struct variant")), + } + } +} diff --git a/vendor/serde-value/src/lib.rs b/vendor/serde-value/src/lib.rs new file mode 100644 index 0000000..7f2544b --- /dev/null +++ b/vendor/serde-value/src/lib.rs @@ -0,0 +1,384 @@ +#![doc(html_root_url="https://docs.rs/serde-value/0.7.0/")] + +use std::collections::BTreeMap; +use std::cmp::Ordering; +use std::hash::{Hash, Hasher}; +use serde::Deserialize; +use ordered_float::OrderedFloat; + +pub use de::*; +pub use ser::*; + +mod de; +mod ser; + +#[derive(Clone, Debug)] +pub enum Value { + Bool(bool), + + U8(u8), + U16(u16), + U32(u32), + U64(u64), + + I8(i8), + I16(i16), + I32(i32), + I64(i64), + + F32(f32), + F64(f64), + + Char(char), + String(String), + + Unit, + Option(Option>), + Newtype(Box), + Seq(Vec), + Map(BTreeMap), + Bytes(Vec), +} + +impl Hash for Value { + fn hash(&self, hasher: &mut H) + where + H: Hasher + { + self.discriminant().hash(hasher); + match *self { + Value::Bool(v) => v.hash(hasher), + Value::U8(v) => v.hash(hasher), + Value::U16(v) => v.hash(hasher), + Value::U32(v) => v.hash(hasher), + Value::U64(v) => v.hash(hasher), + Value::I8(v) => v.hash(hasher), + Value::I16(v) => v.hash(hasher), + Value::I32(v) => v.hash(hasher), + Value::I64(v) => v.hash(hasher), + Value::F32(v) => OrderedFloat(v).hash(hasher), + Value::F64(v) => OrderedFloat(v).hash(hasher), + Value::Char(v) => v.hash(hasher), + Value::String(ref v) => v.hash(hasher), + Value::Unit => ().hash(hasher), + Value::Option(ref v) => v.hash(hasher), + Value::Newtype(ref v) => v.hash(hasher), + Value::Seq(ref v) => v.hash(hasher), + Value::Map(ref v) => v.hash(hasher), + Value::Bytes(ref v) => v.hash(hasher), + } + } +} + +impl PartialEq for Value { + fn eq(&self, rhs: &Self) -> bool { + match (self, rhs) { + (&Value::Bool(v0), &Value::Bool(v1)) if v0 == v1 => true, + (&Value::U8(v0), &Value::U8(v1)) if v0 == v1 => true, + (&Value::U16(v0), &Value::U16(v1)) if v0 == v1 => true, + (&Value::U32(v0), &Value::U32(v1)) if v0 == v1 => true, + (&Value::U64(v0), &Value::U64(v1)) if v0 == v1 => true, + (&Value::I8(v0), &Value::I8(v1)) if v0 == v1 => true, + (&Value::I16(v0), &Value::I16(v1)) if v0 == v1 => true, + (&Value::I32(v0), &Value::I32(v1)) if v0 == v1 => true, + (&Value::I64(v0), &Value::I64(v1)) if v0 == v1 => true, + (&Value::F32(v0), &Value::F32(v1)) if OrderedFloat(v0) == OrderedFloat(v1) => true, + (&Value::F64(v0), &Value::F64(v1)) if OrderedFloat(v0) == OrderedFloat(v1) => true, + (&Value::Char(v0), &Value::Char(v1)) if v0 == v1 => true, + (&Value::String(ref v0), &Value::String(ref v1)) if v0 == v1 => true, + (&Value::Unit, &Value::Unit) => true, + (&Value::Option(ref v0), &Value::Option(ref v1)) if v0 == v1 => true, + (&Value::Newtype(ref v0), &Value::Newtype(ref v1)) if v0 == v1 => true, + (&Value::Seq(ref v0), &Value::Seq(ref v1)) if v0 == v1 => true, + (&Value::Map(ref v0), &Value::Map(ref v1)) if v0 == v1 => true, + (&Value::Bytes(ref v0), &Value::Bytes(ref v1)) if v0 == v1 => true, + _ => false, + } + } +} + +impl Ord for Value { + fn cmp(&self, rhs: &Self) -> Ordering { + match (self, rhs) { + (&Value::Bool(v0), &Value::Bool(ref v1)) => v0.cmp(v1), + (&Value::U8(v0), &Value::U8(ref v1)) => v0.cmp(v1), + (&Value::U16(v0), &Value::U16(ref v1)) => v0.cmp(v1), + (&Value::U32(v0), &Value::U32(ref v1)) => v0.cmp(v1), + (&Value::U64(v0), &Value::U64(ref v1)) => v0.cmp(v1), + (&Value::I8(v0), &Value::I8(ref v1)) => v0.cmp(v1), + (&Value::I16(v0), &Value::I16(ref v1)) => v0.cmp(v1), + (&Value::I32(v0), &Value::I32(ref v1)) => v0.cmp(v1), + (&Value::I64(v0), &Value::I64(ref v1)) => v0.cmp(v1), + (&Value::F32(v0), &Value::F32(v1)) => OrderedFloat(v0).cmp(&OrderedFloat(v1)), + (&Value::F64(v0), &Value::F64(v1)) => OrderedFloat(v0).cmp(&OrderedFloat(v1)), + (&Value::Char(v0), &Value::Char(ref v1)) => v0.cmp(v1), + (&Value::String(ref v0), &Value::String(ref v1)) => v0.cmp(v1), + (&Value::Unit, &Value::Unit) => Ordering::Equal, + (&Value::Option(ref v0), &Value::Option(ref v1)) => v0.cmp(v1), + (&Value::Newtype(ref v0), &Value::Newtype(ref v1)) => v0.cmp(v1), + (&Value::Seq(ref v0), &Value::Seq(ref v1)) => v0.cmp(v1), + (&Value::Map(ref v0), &Value::Map(ref v1)) => v0.cmp(v1), + (&Value::Bytes(ref v0), &Value::Bytes(ref v1)) => v0.cmp(v1), + (ref v0, ref v1) => v0.discriminant().cmp(&v1.discriminant()), + } + } +} + +impl Value { + fn discriminant(&self) -> usize { + match *self { + Value::Bool(..) => 0, + Value::U8(..) => 1, + Value::U16(..) => 2, + Value::U32(..) => 3, + Value::U64(..) => 4, + Value::I8(..) => 5, + Value::I16(..) => 6, + Value::I32(..) => 7, + Value::I64(..) => 8, + Value::F32(..) => 9, + Value::F64(..) => 10, + Value::Char(..) => 11, + Value::String(..) => 12, + Value::Unit => 13, + Value::Option(..) => 14, + Value::Newtype(..) => 15, + Value::Seq(..) => 16, + Value::Map(..) => 17, + Value::Bytes(..) => 18, + } + } + + fn unexpected(&self) -> serde::de::Unexpected { + match *self { + Value::Bool(b) => serde::de::Unexpected::Bool(b), + Value::U8(n) => serde::de::Unexpected::Unsigned(n as u64), + Value::U16(n) => serde::de::Unexpected::Unsigned(n as u64), + Value::U32(n) => serde::de::Unexpected::Unsigned(n as u64), + Value::U64(n) => serde::de::Unexpected::Unsigned(n), + Value::I8(n) => serde::de::Unexpected::Signed(n as i64), + Value::I16(n) => serde::de::Unexpected::Signed(n as i64), + Value::I32(n) => serde::de::Unexpected::Signed(n as i64), + Value::I64(n) => serde::de::Unexpected::Signed(n), + Value::F32(n) => serde::de::Unexpected::Float(n as f64), + Value::F64(n) => serde::de::Unexpected::Float(n), + Value::Char(c) => serde::de::Unexpected::Char(c), + Value::String(ref s) => serde::de::Unexpected::Str(s), + Value::Unit => serde::de::Unexpected::Unit, + Value::Option(_) => serde::de::Unexpected::Option, + Value::Newtype(_) => serde::de::Unexpected::NewtypeStruct, + Value::Seq(_) => serde::de::Unexpected::Seq, + Value::Map(_) => serde::de::Unexpected::Map, + Value::Bytes(ref b) => serde::de::Unexpected::Bytes(b), + } + } + + pub fn deserialize_into<'de, T: Deserialize<'de>>(self) -> Result { + T::deserialize(self) + } +} + +impl Eq for Value { } +impl PartialOrd for Value { + fn partial_cmp(&self, rhs: &Self) -> Option { + Some(self.cmp(rhs)) + } +} + +#[cfg(test)] +use serde_derive::{Deserialize, Serialize}; + +#[test] +fn de_smoke_test() { + // some convoluted Value + let value = Value::Option(Some(Box::new(Value::Seq(vec![ + Value::U16(8), + Value::Char('a'), + Value::F32(1.0), + Value::String("hello".into()), + Value::Map(vec![ + (Value::Bool(false), Value::Unit), + (Value::Bool(true), Value::Newtype(Box::new( + Value::Bytes(b"hi".as_ref().into()) + ))), + ].into_iter().collect()), + ])))); + + // assert that the value remains unchanged through deserialization + let value_de = Value::deserialize(value.clone()).unwrap(); + assert_eq!(value_de, value); +} + +#[test] +fn ser_smoke_test() { + #[derive(Serialize)] + struct Foo { + a: u32, + b: String, + c: Vec, + } + + let foo = Foo { + a: 15, + b: "hello".into(), + c: vec![true, false], + }; + + let expected = Value::Map(vec![ + (Value::String("a".into()), Value::U32(15)), + (Value::String("b".into()), Value::String("hello".into())), + (Value::String("c".into()), Value::Seq(vec![Value::Bool(true), Value::Bool(false)])), + ].into_iter().collect()); + + let value = to_value(&foo).unwrap(); + assert_eq!(expected, value); +} + +#[test] +fn deserialize_into_enum() { + #[derive(Deserialize, Debug, PartialEq, Eq)] + enum Foo { + Bar, + Baz(u8), + } + + let value = Value::String("Bar".into()); + assert_eq!(Foo::deserialize(value).unwrap(), Foo::Bar); + + let value = Value::Map(vec![ + (Value::String("Baz".into()), Value::U8(1)) + ].into_iter().collect()); + assert_eq!(Foo::deserialize(value).unwrap(), Foo::Baz(1)); +} + +#[test] +fn serialize_from_enum() { + #[derive(Serialize)] + enum Foo { + Bar, + Baz(u8), + Qux { quux: u8 }, + Corge(u8, u8), + } + + let bar = Foo::Bar; + assert_eq!(to_value(&bar).unwrap(), Value::String("Bar".into())); + + let baz = Foo::Baz(1); + assert_eq!( + to_value(&baz).unwrap(), + Value::Map( + vec![(Value::String("Baz".into()), Value::U8(1))] + .into_iter() + .collect(), + ) + ); + + let qux = Foo::Qux { quux: 2 }; + assert_eq!( + to_value(&qux).unwrap(), + Value::Map( + vec![( + Value::String("Qux".into()), + Value::Map( + vec![(Value::String("quux".into()), Value::U8(2))] + .into_iter() + .collect() + ) + )] + .into_iter() + .collect() + ) + ); + + let corge = Foo::Corge(3, 4); + assert_eq!( + to_value(&corge).unwrap(), + Value::Map( + vec![( + Value::String("Corge".into()), + Value::Seq(vec![Value::U8(3), Value::U8(4)]) + )] + .into_iter() + .collect() + ) + ); +} + +#[test] +fn deserialize_inside_deserialize_impl() { + #[derive(Debug, PartialEq, Eq)] + enum Event { + Added(u32), + Error(u8), + } + + impl<'de> serde::Deserialize<'de> for Event { + fn deserialize(deserializer: D) -> Result where D: serde::Deserializer<'de> { + #[derive(Deserialize)] + struct RawEvent { + kind: String, + object: Value, + } + + let raw_event = RawEvent::deserialize(deserializer)?; + + // Cannot directly use Value as Deserializer, since error type needs to be + // generic D::Error rather than specific serde_value::DeserializerError + let object_deserializer = ValueDeserializer::new(raw_event.object); + + Ok(match &*raw_event.kind { + "ADDED" => Event::Added(<_>::deserialize(object_deserializer)?), + "ERROR" => Event::Error(<_>::deserialize(object_deserializer)?), + kind => return Err(serde::de::Error::unknown_variant(kind, &["ADDED", "ERROR"])), + }) + } + } + + let input = Value::Map(vec![ + (Value::String("kind".to_owned()), Value::String("ADDED".to_owned())), + (Value::String("object".to_owned()), Value::U32(5)), + ].into_iter().collect()); + let event = Event::deserialize(input).expect("could not deserialize ADDED event"); + assert_eq!(event, Event::Added(5)); + + let input = Value::Map(vec![ + (Value::String("kind".to_owned()), Value::String("ERROR".to_owned())), + (Value::String("object".to_owned()), Value::U8(5)), + ].into_iter().collect()); + let event = Event::deserialize(input).expect("could not deserialize ERROR event"); + assert_eq!(event, Event::Error(5)); + + let input = Value::Map(vec![ + (Value::String("kind".to_owned()), Value::String("ADDED".to_owned())), + (Value::String("object".to_owned()), Value::Unit), + ].into_iter().collect()); + let _ = Event::deserialize(input).expect_err("expected deserializing bad ADDED event to fail"); +} + +#[test] +fn deserialize_newtype() { + #[derive(Debug, Deserialize, PartialEq)] + struct Foo(i32); + + let input = Value::I32(5); + let foo = Foo::deserialize(input).unwrap(); + assert_eq!(foo, Foo(5)); +} + +#[test] +fn deserialize_newtype2() { + #[derive(Debug, Deserialize, PartialEq)] + struct Foo(i32); + + #[derive(Debug, Deserialize, PartialEq)] + struct Bar { + foo: Foo, + } + + let input = Value::Map(vec![ + (Value::String("foo".to_owned()), Value::I32(5)) + ].into_iter().collect()); + let bar = Bar::deserialize(input).unwrap(); + assert_eq!(bar, Bar { foo: Foo(5) }); +} diff --git a/vendor/serde-value/src/ser.rs b/vendor/serde-value/src/ser.rs new file mode 100644 index 0000000..654fa5d --- /dev/null +++ b/vendor/serde-value/src/ser.rs @@ -0,0 +1,438 @@ +use serde::ser; +use std::collections::BTreeMap; +use std::error::Error; +use std::fmt; + +use crate::Value; + +#[derive(Debug)] +pub enum SerializerError { + Custom(String), +} + +impl fmt::Display for SerializerError { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + match *self { + SerializerError::Custom(ref s) => fmt.write_str(s), + } + } +} + +impl Error for SerializerError { + fn description(&self) -> &str { + "Value serializer error" + } +} + +impl ser::Error for SerializerError { + fn custom(msg: T) -> SerializerError { + SerializerError::Custom(msg.to_string()) + } +} + +impl ser::Serialize for Value { + fn serialize(&self, s: S) -> Result { + match *self { + Value::Bool(v) => s.serialize_bool(v), + Value::U8(v) => s.serialize_u8(v), + Value::U16(v) => s.serialize_u16(v), + Value::U32(v) => s.serialize_u32(v), + Value::U64(v) => s.serialize_u64(v), + Value::I8(v) => s.serialize_i8(v), + Value::I16(v) => s.serialize_i16(v), + Value::I32(v) => s.serialize_i32(v), + Value::I64(v) => s.serialize_i64(v), + Value::F32(v) => s.serialize_f32(v), + Value::F64(v) => s.serialize_f64(v), + Value::Char(v) => s.serialize_char(v), + Value::String(ref v) => s.serialize_str(v), + Value::Unit => s.serialize_unit(), + Value::Option(None) => s.serialize_none(), + Value::Option(Some(ref v)) => s.serialize_some(v), + Value::Newtype(ref v) => s.serialize_newtype_struct("", v), + Value::Seq(ref v) => v.serialize(s), + Value::Map(ref v) => v.serialize(s), + Value::Bytes(ref v) => s.serialize_bytes(v), + } + } +} + +pub fn to_value(value: T) -> Result { + value.serialize(Serializer) +} + +struct Serializer; + +impl ser::Serializer for Serializer { + type Ok = Value; + type Error = SerializerError; + type SerializeSeq = SerializeSeq; + type SerializeTuple = SerializeTuple; + type SerializeTupleStruct = SerializeTupleStruct; + type SerializeTupleVariant = SerializeTupleVariant; + type SerializeMap = SerializeMap; + type SerializeStruct = SerializeStruct; + type SerializeStructVariant = SerializeStructVariant; + + fn serialize_bool(self, v: bool) -> Result { + Ok(Value::Bool(v)) + } + + fn serialize_i8(self, v: i8) -> Result { + Ok(Value::I8(v)) + } + + fn serialize_i16(self, v: i16) -> Result { + Ok(Value::I16(v)) + } + + fn serialize_i32(self, v: i32) -> Result { + Ok(Value::I32(v)) + } + + fn serialize_i64(self, v: i64) -> Result { + Ok(Value::I64(v)) + } + + fn serialize_u8(self, v: u8) -> Result { + Ok(Value::U8(v)) + } + + fn serialize_u16(self, v: u16) -> Result { + Ok(Value::U16(v)) + } + + fn serialize_u32(self, v: u32) -> Result { + Ok(Value::U32(v)) + } + + fn serialize_u64(self, v: u64) -> Result { + Ok(Value::U64(v)) + } + + fn serialize_f32(self, v: f32) -> Result { + Ok(Value::F32(v)) + } + + fn serialize_f64(self, v: f64) -> Result { + Ok(Value::F64(v)) + } + + fn serialize_char(self, v: char) -> Result { + Ok(Value::Char(v)) + } + + fn serialize_str(self, v: &str) -> Result { + Ok(Value::String(v.to_string())) + } + + fn serialize_bytes(self, v: &[u8]) -> Result { + Ok(Value::Bytes(v.to_vec())) + } + + fn serialize_none(self) -> Result { + Ok(Value::Option(None)) + } + + fn serialize_some( + self, + value: &T + ) -> Result + where + T: ser::Serialize + { + value.serialize(Serializer).map(|v| Value::Option(Some(Box::new(v)))) + } + + fn serialize_unit(self) -> Result { + Ok(Value::Unit) + } + + fn serialize_unit_struct( + self, + _name: &'static str + ) -> Result { + Ok(Value::Unit) + } + + fn serialize_unit_variant( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + ) -> Result { + Ok(Value::String(variant.to_string())) + } + + fn serialize_newtype_struct( + self, + _name: &'static str, + value: &T + ) -> Result + where + T: ser::Serialize + { + value.serialize(Serializer).map(|v| Value::Newtype(Box::new(v))) + } + + fn serialize_newtype_variant( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + value: &T + ) -> Result + where + T: ser::Serialize + { + value.serialize(Serializer).map(|v| { + let mut map = BTreeMap::new(); + map.insert(Value::String(variant.to_string()), v); + Value::Map(map) + }) + } + + fn serialize_seq( + self, + _len: Option + ) -> Result { + Ok(SerializeSeq(vec![])) + } + + fn serialize_tuple( + self, + _len: usize + ) -> Result { + Ok(SerializeTuple(vec![])) + } + + fn serialize_tuple_struct( + self, + _name: &'static str, + _len: usize + ) -> Result { + Ok(SerializeTupleStruct(vec![])) + } + + fn serialize_tuple_variant( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + len: usize + ) -> Result { + Ok(SerializeTupleVariant( + Value::String(variant.to_string()), + Vec::with_capacity(len), + )) + } + + fn serialize_map( + self, + _len: Option + ) -> Result { + Ok(SerializeMap { map: BTreeMap::new(), key: None }) + } + + fn serialize_struct( + self, + _name: &'static str, + _len: usize + ) -> Result { + Ok(SerializeStruct(BTreeMap::new())) + } + + fn serialize_struct_variant( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + _len: usize + ) -> Result { + Ok(SerializeStructVariant( + Value::String(variant.to_string()), + BTreeMap::new(), + )) + } +} + +struct SerializeSeq(Vec); + +impl ser::SerializeSeq for SerializeSeq { + type Ok = Value; + type Error = SerializerError; + + fn serialize_element( + &mut self, + value: &T + ) -> Result<(), Self::Error> + where + T: ser::Serialize + { + let value = value.serialize(Serializer)?; + self.0.push(value); + Ok(()) + } + + fn end(self) -> Result { + Ok(Value::Seq(self.0)) + } +} + +struct SerializeTuple(Vec); + +impl ser::SerializeTuple for SerializeTuple { + type Ok = Value; + type Error = SerializerError; + + fn serialize_element( + &mut self, + value: &T + ) -> Result<(), Self::Error> + where + T: ser::Serialize + { + let value = value.serialize(Serializer)?; + self.0.push(value); + Ok(()) + } + + fn end(self) -> Result { + Ok(Value::Seq(self.0)) + } +} + +struct SerializeTupleStruct(Vec); + +impl ser::SerializeTupleStruct for SerializeTupleStruct { + type Ok = Value; + type Error = SerializerError; + + fn serialize_field( + &mut self, + value: &T + ) -> Result<(), Self::Error> + where + T: ser::Serialize + { + let value = value.serialize(Serializer)?; + self.0.push(value); + Ok(()) + } + + fn end(self) -> Result { + Ok(Value::Seq(self.0)) + } +} + +struct SerializeTupleVariant(Value, Vec); + +impl ser::SerializeTupleVariant for SerializeTupleVariant { + type Ok = Value; + type Error = SerializerError; + + fn serialize_field( + &mut self, + value: &T + ) -> Result<(), Self::Error> + where + T: ser::Serialize + { + let value = value.serialize(Serializer)?; + self.1.push(value); + Ok(()) + } + + fn end(self) -> Result { + let mut map = BTreeMap::new(); + map.insert(self.0, Value::Seq(self.1)); + Ok(Value::Map(map)) + } +} + +struct SerializeMap { + map: BTreeMap, + key: Option, +} + +impl ser::SerializeMap for SerializeMap { + type Ok = Value; + type Error = SerializerError; + + fn serialize_key(&mut self, key: &T) -> Result<(), Self::Error> + where + T: ser::Serialize + { + let key = key.serialize(Serializer)?; + self.key = Some(key); + Ok(()) + } + + fn serialize_value( + &mut self, + value: &T + ) -> Result<(), Self::Error> + where + T: ser::Serialize + { + let value = value.serialize(Serializer)?; + self.map.insert(self.key.take().unwrap(), value); + Ok(()) + } + + fn end(self) -> Result { + Ok(Value::Map(self.map)) + } +} + +struct SerializeStruct(BTreeMap); + +impl ser::SerializeStruct for SerializeStruct { + type Ok = Value; + type Error = SerializerError; + + fn serialize_field( + &mut self, + key: &'static str, + value: &T + ) -> Result<(), Self::Error> + where + T: ser::Serialize + { + let key = Value::String(key.to_string()); + let value = value.serialize(Serializer)?; + self.0.insert(key, value); + Ok(()) + } + + fn end(self) -> Result { + Ok(Value::Map(self.0)) + } +} + +struct SerializeStructVariant(Value, BTreeMap); + +impl ser::SerializeStructVariant for SerializeStructVariant { + type Ok = Value; + type Error = SerializerError; + + fn serialize_field( + &mut self, + key: &'static str, + value: &T + ) -> Result<(), Self::Error> + where + T: ser::Serialize + { + let key = Value::String(key.to_string()); + let value = value.serialize(Serializer)?; + self.1.insert(key, value); + Ok(()) + } + + fn end(self) -> Result { + let mut map = BTreeMap::new(); + map.insert(self.0, Value::Map(self.1)); + Ok(Value::Map(map)) + } +} -- cgit v1.2.3