use super::{BitSequence, Composite, Primitive, Value, ValueDef, Variant};
use serde::{
de::{self, EnumAccess, IntoDeserializer, SeqAccess, VariantAccess},
forward_to_deserialize_any, ser, Deserialize, Deserializer, Serialize, Serializer,
};
use std::borrow::Cow;
use std::fmt::Display;
#[derive(thiserror::Error, Debug, Clone, PartialEq)]
#[error("{0}")]
pub struct Error(Cow<'static, str>);
impl Error {
fn from_string<S: Into<String>>(s: S) -> Error {
Error(Cow::Owned(s.into()))
}
fn from_str(s: &'static str) -> Error {
Error(Cow::Borrowed(s))
}
}
impl de::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Error::from_string(msg.to_string())
}
}
impl ser::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Error::from_string(msg.to_string())
}
}
macro_rules! deserialize_x {
($fn_name:ident) => {
fn $fn_name<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
self.value.$fn_name(visitor)
}
}
}
impl<'de, T> Deserializer<'de> for Value<T> {
type Error = Error;
deserialize_x!(deserialize_any);
deserialize_x!(deserialize_bool);
deserialize_x!(deserialize_i8);
deserialize_x!(deserialize_i16);
deserialize_x!(deserialize_i32);
deserialize_x!(deserialize_i64);
deserialize_x!(deserialize_i128);
deserialize_x!(deserialize_u8);
deserialize_x!(deserialize_u16);
deserialize_x!(deserialize_u32);
deserialize_x!(deserialize_u64);
deserialize_x!(deserialize_u128);
deserialize_x!(deserialize_f32);
deserialize_x!(deserialize_f64);
deserialize_x!(deserialize_char);
deserialize_x!(deserialize_str);
deserialize_x!(deserialize_string);
deserialize_x!(deserialize_bytes);
deserialize_x!(deserialize_byte_buf);
deserialize_x!(deserialize_option);
deserialize_x!(deserialize_unit);
deserialize_x!(deserialize_seq);
deserialize_x!(deserialize_map);
deserialize_x!(deserialize_identifier);
deserialize_x!(deserialize_ignored_any);
fn deserialize_unit_struct<V>(self, name: &'static str, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
self.value.deserialize_unit_struct(name, visitor)
}
fn deserialize_newtype_struct<V>(self, name: &'static str, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
self.value.deserialize_newtype_struct(name, visitor)
}
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
self.value.deserialize_tuple(len, visitor)
}
fn deserialize_tuple_struct<V>(self, name: &'static str, len: usize, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
self.value.deserialize_tuple_struct(name, len, visitor)
}
fn deserialize_struct<V>(
self,
name: &'static str,
fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
self.value.deserialize_struct(name, fields, visitor)
}
fn deserialize_enum<V>(
self,
name: &'static str,
variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
self.value.deserialize_enum(name, variants, visitor)
}
}
macro_rules! delegate_except_bitseq {
(
$name:ident ( $self:ident, $($arg:ident),* ),
$seq:pat => $expr:expr
) => {
match $self {
ValueDef::BitSequence($seq) => {
$expr
},
ValueDef::Composite(composite) => {
composite.$name( $($arg),* )
},
ValueDef::Variant(variant) => {
variant.$name( $($arg),* )
},
ValueDef::Primitive(prim) => {
prim.$name( $($arg),* )
},
}
}
}
impl<'de, T> Deserializer<'de> for ValueDef<T> {
type Error = Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: serde::de::Visitor<'de>,
{
delegate_except_bitseq! { deserialize_any(self, visitor),
seq => {
BitVecPieces::new(seq)?.deserialize_any(visitor)
}
}
}
fn deserialize_newtype_struct<V>(self, name: &'static str, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
delegate_except_bitseq! { deserialize_newtype_struct(self, name, visitor),
_ => {
Err(Error::from_str("Cannot deserialize BitSequence into a newtype struct"))
}
}
}
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
delegate_except_bitseq! { deserialize_tuple(self, len, visitor),
_ => {
Err(Error::from_str("Cannot deserialize BitSequence into a tuple"))
}
}
}
fn deserialize_tuple_struct<V>(self, name: &'static str, len: usize, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
delegate_except_bitseq! { deserialize_tuple_struct(self, name, len, visitor),
_ => {
Err(Error::from_str("Cannot deserialize BitSequence into a tuple struct"))
}
}
}
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
delegate_except_bitseq! { deserialize_unit(self, visitor),
_ => {
Err(Error::from_str("Cannot deserialize BitSequence into a ()"))
}
}
}
fn deserialize_unit_struct<V>(self, name: &'static str, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
delegate_except_bitseq! { deserialize_unit_struct(self, name, visitor),
_ => {
Err(Error::from_string(format!("Cannot deserialize BitSequence into the unit struct {}", name)))
}
}
}
fn deserialize_enum<V>(
self,
name: &'static str,
variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
delegate_except_bitseq! { deserialize_enum(self, name, variants, visitor),
_ => {
Err(Error::from_string(format!("Cannot deserialize BitSequence into the enum {}", name)))
}
}
}
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
delegate_except_bitseq! { deserialize_bytes(self, visitor),
_ => {
Err(Error::from_str("Cannot deserialize BitSequence into raw bytes"))
}
}
}
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
delegate_except_bitseq! { deserialize_byte_buf(self, visitor),
_ => {
Err(Error::from_str("Cannot deserialize BitSequence into raw bytes"))
}
}
}
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
delegate_except_bitseq! { deserialize_seq(self, visitor),
_ => {
Err(Error::from_str("Cannot deserialize BitSequence into a sequence"))
}
}
}
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
delegate_except_bitseq! { deserialize_map(self, visitor),
_ => {
Err(Error::from_str("Cannot deserialize BitSequence into a map"))
}
}
}
forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
option struct identifier ignored_any
}
}
impl<'de, T> IntoDeserializer<'de, Error> for Value<T> {
type Deserializer = Value<T>;
fn into_deserializer(self) -> Self::Deserializer {
self
}
}
impl<'de, T> Deserializer<'de> for Composite<T> {
type Error = Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: serde::de::Visitor<'de>,
{
match self {
Composite::Named(values) => visitor.visit_map(de::value::MapDeserializer::new(values.into_iter())),
Composite::Unnamed(values) => visitor.visit_seq(de::value::SeqDeserializer::new(values.into_iter())),
}
}
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
match self {
Composite::Named(values) => {
visitor.visit_seq(de::value::SeqDeserializer::new(values.into_iter().map(|(_, v)| v)))
}
Composite::Unnamed(values) => visitor.visit_seq(de::value::SeqDeserializer::new(values.into_iter())),
}
}
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
match self {
Composite::Named(values) => {
if values.len() != len {
return Err(Error::from_string(format!(
"Cannot deserialize composite of length {} into tuple of length {}",
values.len(),
len
)));
}
visitor.visit_seq(de::value::SeqDeserializer::new(values.into_iter().map(|(_, v)| v)))
}
Composite::Unnamed(values) => {
if values.len() != len {
return Err(Error::from_string(format!(
"Cannot deserialize composite of length {} into tuple of length {}",
values.len(),
len
)));
}
visitor.visit_seq(de::value::SeqDeserializer::new(values.into_iter()))
}
}
}
fn deserialize_tuple_struct<V>(self, _name: &'static str, len: usize, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
self.deserialize_tuple(len, visitor)
}
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
if self.is_empty() {
visitor.visit_unit()
} else {
Err(Error::from_str("Cannot deserialize non-empty Composite into a unit value"))
}
}
fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
self.deserialize_unit(visitor)
}
fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
visitor.visit_seq(de::value::SeqDeserializer::new(Some(self).into_iter()))
}
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
match self {
Composite::Named(values) => {
let bytes = values
.into_iter()
.map(|(_n, v)| {
if let ValueDef::Primitive(Primitive::U8(byte)) = v.value {
Ok(byte)
} else {
Err(Error::from_str("Cannot deserialize composite that is not entirely U8's into bytes"))
}
})
.collect::<Result<_, Error>>()?;
visitor.visit_byte_buf(bytes)
}
Composite::Unnamed(values) => {
let bytes = values
.into_iter()
.map(|v| {
if let ValueDef::Primitive(Primitive::U8(byte)) = v.value {
Ok(byte)
} else {
Err(Error::from_str("Cannot deserialize composite that is not entirely U8's into bytes"))
}
})
.collect::<Result<_, Error>>()?;
visitor.visit_byte_buf(bytes)
}
}
}
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
self.deserialize_byte_buf(visitor)
}
forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
option struct map
enum identifier ignored_any
}
}
impl<'de, T> IntoDeserializer<'de, Error> for Composite<T> {
type Deserializer = Composite<T>;
fn into_deserializer(self) -> Self::Deserializer {
self
}
}
impl<'de, T> VariantAccess<'de> for Composite<T> {
type Error = Error;
fn unit_variant(self) -> Result<(), Self::Error> {
Deserialize::deserialize(self)
}
fn newtype_variant_seed<S>(self, seed: S) -> Result<S::Value, Self::Error>
where
S: de::DeserializeSeed<'de>,
{
seed.deserialize(self)
}
fn tuple_variant<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
self.deserialize_tuple(len, visitor)
}
fn struct_variant<V>(self, _fields: &'static [&'static str], visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
self.deserialize_any(visitor)
}
}
impl<'de, T> Deserializer<'de> for Variant<T> {
type Error = Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: serde::de::Visitor<'de>,
{
visitor.visit_enum(self)
}
fn deserialize_enum<V>(
self,
_name: &'static str,
_variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
visitor.visit_enum(self)
}
fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
visitor.visit_seq(de::value::SeqDeserializer::new(Some(self).into_iter()))
}
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
self.values.deserialize_tuple(len, visitor)
}
fn deserialize_tuple_struct<V>(self, name: &'static str, len: usize, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
self.values.deserialize_tuple_struct(name, len, visitor)
}
fn deserialize_unit_struct<V>(self, name: &'static str, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
self.values.deserialize_unit_struct(name, visitor)
}
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
self.values.deserialize_unit(visitor)
}
fn deserialize_struct<V>(
self,
name: &'static str,
fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
self.values.deserialize_struct(name, fields, visitor)
}
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
self.values.deserialize_map(visitor)
}
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
self.values.deserialize_seq(visitor)
}
forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
bytes byte_buf option identifier ignored_any
}
}
impl<'de, T> IntoDeserializer<'de, Error> for Variant<T> {
type Deserializer = Variant<T>;
fn into_deserializer(self) -> Self::Deserializer {
self
}
}
impl<'de, T> EnumAccess<'de> for Variant<T> {
type Error = Error;
type Variant = Composite<T>;
fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
where
V: de::DeserializeSeed<'de>,
{
let name = self.name.into_deserializer();
let values = self.values;
seed.deserialize(name).map(|name| (name, values))
}
}
impl<'de> Deserializer<'de> for Primitive {
type Error = Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: serde::de::Visitor<'de>,
{
match self {
Primitive::Bool(v) => visitor.visit_bool(v),
Primitive::Char(v) => visitor.visit_char(v),
Primitive::Str(v) => visitor.visit_string(v),
Primitive::U8(v) => visitor.visit_u8(v),
Primitive::U16(v) => visitor.visit_u16(v),
Primitive::U32(v) => visitor.visit_u32(v),
Primitive::U64(v) => visitor.visit_u64(v),
Primitive::U128(v) => visitor.visit_u128(v),
Primitive::U256(v) => visitor.visit_bytes(&v),
Primitive::I8(v) => visitor.visit_i8(v),
Primitive::I16(v) => visitor.visit_i16(v),
Primitive::I32(v) => visitor.visit_i32(v),
Primitive::I64(v) => visitor.visit_i64(v),
Primitive::I128(v) => visitor.visit_i128(v),
Primitive::I256(v) => visitor.visit_bytes(&v),
}
}
fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
visitor.visit_seq(de::value::SeqDeserializer::new(Some(self).into_iter()))
}
forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
bytes byte_buf option unit unit_struct seq tuple
tuple_struct map struct enum identifier ignored_any
}
}
impl<'de> IntoDeserializer<'de, Error> for Primitive {
type Deserializer = Primitive;
fn into_deserializer(self) -> Self::Deserializer {
self
}
}
struct BitVecPieces {
head: u8,
bits: u64,
data: Vec<u8>,
current_field: Option<Field>,
}
#[derive(PartialEq, Copy, Clone)]
enum Field {
Head,
Bits,
Data,
}
impl<'de> Deserializer<'de> for BitVecPieces {
type Error = Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
visitor.visit_seq(self)
}
forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
bytes byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct map struct enum identifier ignored_any
}
}
impl<'de> SeqAccess<'de> for BitVecPieces {
type Error = Error;
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
where
T: de::DeserializeSeed<'de>,
{
match self.current_field {
Some(Field::Head) => {
let res = seed.deserialize(self.head.into_deserializer()).map(Some);
self.current_field = Some(Field::Bits);
res
}
Some(Field::Bits) => {
let res = seed.deserialize(self.bits.into_deserializer()).map(Some);
self.current_field = Some(Field::Data);
res
}
Some(Field::Data) => {
let bytes = std::mem::take(&mut self.data);
let res = seed.deserialize(bytes.into_deserializer()).map(Some);
self.current_field = None;
res
}
None => Ok(None),
}
}
}
impl BitVecPieces {
fn new(bit_vec: BitSequence) -> Result<BitVecPieces, Error> {
struct BitVecSerializer {
head: Option<u8>,
bits: Option<u64>,
data: Vec<u8>,
current_field: Option<Field>,
}
impl ser::SerializeStruct for &mut BitVecSerializer {
type Ok = ();
type Error = Error;
fn serialize_field<T: ?Sized + Serialize>(
&mut self,
key: &'static str,
value: &T,
) -> Result<(), Self::Error> {
match key {
"head" => {
self.current_field = Some(Field::Head);
}
"bits" => {
self.current_field = Some(Field::Bits);
}
"data" => {
self.current_field = Some(Field::Data);
}
_ => {
return Err(Error::from_string(format!(
"BitVec serialization encountered unexpected field '{}'",
key
)))
}
}
value.serialize(&mut **self)
}
fn end(self) -> Result<Self::Ok, Self::Error> {
self.current_field = None;
Ok(())
}
}
impl ser::SerializeSeq for &mut BitVecSerializer {
type Ok = ();
type Error = Error;
fn serialize_element<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), Self::Error> {
value.serialize(&mut **self)
}
fn end(self) -> Result<Self::Ok, Self::Error> {
Ok(())
}
}
impl Serializer for &mut BitVecSerializer {
type Ok = ();
type Error = Error;
type SerializeStruct = Self;
type SerializeSeq = Self;
type SerializeTuple = serde::ser::Impossible<(), Error>;
type SerializeTupleStruct = serde::ser::Impossible<(), Error>;
type SerializeTupleVariant = serde::ser::Impossible<(), Error>;
type SerializeMap = serde::ser::Impossible<(), Error>;
type SerializeStructVariant = serde::ser::Impossible<(), Error>;
fn serialize_struct(self, _: &'static str, _: usize) -> Result<Self::SerializeStruct, Self::Error> {
Ok(self)
}
fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
match self.current_field {
Some(Field::Data) => Ok(self),
_ => Err(Error::from_str(
"BitVec serialization only expects serialize_seq to be called for 'data' prop",
)),
}
}
fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> {
match self.current_field {
Some(Field::Head) => {
self.head = Some(v);
Ok(())
}
Some(Field::Data) => {
self.data.push(v);
Ok(())
}
_ => Err(Error::from_str(
"BitVec serialization only expects serialize_u8 to be called for 'head' prop",
)),
}
}
fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> {
match self.current_field {
Some(Field::Bits) => {
self.bits = Some(v);
Ok(())
}
_ => Err(Error::from_str(
"BitVec serialization only expects serialize_u64 to be called for 'len' prop",
)),
}
}
fn serialize_bool(self, _v: bool) -> Result<Self::Ok, Self::Error> {
Err(Error::from_str("Unsupported BitVec serialization method"))
}
fn serialize_i8(self, _v: i8) -> Result<Self::Ok, Self::Error> {
Err(Error::from_str("Unsupported BitVec serialization method"))
}
fn serialize_i16(self, _v: i16) -> Result<Self::Ok, Self::Error> {
Err(Error::from_str("Unsupported BitVec serialization method"))
}
fn serialize_i32(self, _v: i32) -> Result<Self::Ok, Self::Error> {
Err(Error::from_str("Unsupported BitVec serialization method"))
}
fn serialize_i64(self, _v: i64) -> Result<Self::Ok, Self::Error> {
Err(Error::from_str("Unsupported BitVec serialization method"))
}
fn serialize_u16(self, _v: u16) -> Result<Self::Ok, Self::Error> {
Err(Error::from_str("Unsupported BitVec serialization method"))
}
fn serialize_u32(self, _v: u32) -> Result<Self::Ok, Self::Error> {
Err(Error::from_str("Unsupported BitVec serialization method"))
}
fn serialize_f32(self, _v: f32) -> Result<Self::Ok, Self::Error> {
Err(Error::from_str("Unsupported BitVec serialization method"))
}
fn serialize_f64(self, _v: f64) -> Result<Self::Ok, Self::Error> {
Err(Error::from_str("Unsupported BitVec serialization method"))
}
fn serialize_char(self, _v: char) -> Result<Self::Ok, Self::Error> {
Err(Error::from_str("Unsupported BitVec serialization method"))
}
fn serialize_str(self, _v: &str) -> Result<Self::Ok, Self::Error> {
Err(Error::from_str("Unsupported BitVec serialization method"))
}
fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok, Self::Error> {
Err(Error::from_str("Unsupported BitVec serialization method"))
}
fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
Err(Error::from_str("Unsupported BitVec serialization method"))
}
fn serialize_some<T: Serialize + ?Sized>(self, _: &T) -> Result<Self::Ok, Self::Error> {
Err(Error::from_str("Unsupported BitVec serialization method"))
}
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
Err(Error::from_str("Unsupported BitVec serialization method"))
}
fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> {
Err(Error::from_str("Unsupported BitVec serialization method"))
}
fn serialize_unit_variant(self, _: &'static str, _: u32, _: &'static str) -> Result<Self::Ok, Self::Error> {
Err(Error::from_str("Unsupported BitVec serialization method"))
}
fn serialize_newtype_struct<T: ?Sized + Serialize>(
self,
_: &'static str,
_: &T,
) -> Result<Self::Ok, Self::Error> {
Err(Error::from_str("Unsupported BitVec serialization method"))
}
fn serialize_newtype_variant<T: ?Sized + Serialize>(
self,
_: &'static str,
_: u32,
_: &'static str,
_: &T,
) -> Result<Self::Ok, Self::Error> {
Err(Error::from_str("Unsupported BitVec serialization method"))
}
fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Self::Error> {
Err(Error::from_str("Unsupported BitVec serialization method"))
}
fn serialize_tuple_struct(
self,
_: &'static str,
_: usize,
) -> Result<Self::SerializeTupleStruct, Self::Error> {
Err(Error::from_str("Unsupported BitVec serialization method"))
}
fn serialize_tuple_variant(
self,
_: &'static str,
_: u32,
_: &'static str,
_: usize,
) -> Result<Self::SerializeTupleVariant, Self::Error> {
Err(Error::from_str("Unsupported BitVec serialization method"))
}
fn serialize_map(self, _: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
Err(Error::from_str("Unsupported BitVec serialization method"))
}
fn serialize_struct_variant(
self,
_: &'static str,
_: u32,
_: &'static str,
_: usize,
) -> Result<Self::SerializeStructVariant, Self::Error> {
Err(Error::from_str("Unsupported BitVec serialization method"))
}
}
let mut se = BitVecSerializer { head: None, bits: None, data: Vec::new(), current_field: None };
bit_vec.serialize(&mut se)?;
match se {
BitVecSerializer { data, bits: Some(bits), head: Some(head), .. } => {
Ok(BitVecPieces { data, bits, head, current_field: Some(Field::Head) })
}
_ => Err(Error::from_str("Could not gather together the BitVec pieces required during serialization")),
}
}
}
#[cfg(test)]
mod test {
use crate::value::BitSequence;
use serde::Deserialize;
use super::*;
#[test]
fn de_into_struct() {
#[derive(Deserialize, Debug, PartialEq)]
struct Foo {
a: u8,
b: bool,
}
let val = ValueDef::Composite(Composite::Named(vec![
("b".into(), Value::bool(true)),
("a".into(), Value::u8(123)),
]));
assert_eq!(Foo::deserialize(val), Ok(Foo { a: 123, b: true }))
}
#[test]
fn de_unwrapped_into_struct() {
#[derive(Deserialize, Debug, PartialEq)]
struct Foo {
a: u8,
b: bool,
}
let val = Composite::Named(vec![
("b".into(), Value::bool(true)),
("a".into(), Value::u8(123)),
]);
assert_eq!(Foo::deserialize(val), Ok(Foo { a: 123, b: true }))
}
#[test]
fn de_into_tuple_struct() {
#[derive(Deserialize, Debug, PartialEq)]
struct Foo(u8, bool, String);
let val = ValueDef::Composite(Composite::Unnamed(vec![
Value::u8(123),
Value::bool(true),
Value::str("hello".into()),
]));
assert_eq!(Foo::deserialize(val), Ok(Foo(123, true, "hello".into())))
}
#[test]
fn de_unwrapped_into_tuple_struct() {
#[derive(Deserialize, Debug, PartialEq)]
struct Foo(u8, bool, String);
let val = Composite::Unnamed(vec![Value::u8(123), Value::bool(true), Value::str("hello".into())]);
assert_eq!(Foo::deserialize(val), Ok(Foo(123, true, "hello".into())))
}
#[test]
fn de_into_newtype_struct() {
#[derive(Deserialize, Debug, PartialEq)]
struct FooStr(String);
let val = ValueDef::<()>::Primitive(Primitive::Str("hello".into()));
assert_eq!(FooStr::deserialize(val), Ok(FooStr("hello".into())));
let val = Value::str("hello".into());
assert_eq!(FooStr::deserialize(val), Ok(FooStr("hello".into())));
#[derive(Deserialize, Debug, PartialEq)]
struct FooVecU8(Vec<u8>);
let val = ValueDef::Composite(Composite::Unnamed(vec![Value::u8(1), Value::u8(2), Value::u8(3)]));
assert_eq!(FooVecU8::deserialize(val), Ok(FooVecU8(vec![1, 2, 3])));
#[derive(Deserialize, Debug, PartialEq)]
enum MyEnum {
Foo(u8, u8, u8),
}
#[derive(Deserialize, Debug, PartialEq)]
struct FooVar(MyEnum);
let val = ValueDef::Variant(Variant {
name: "Foo".into(),
values: Composite::Unnamed(vec![Value::u8(1), Value::u8(2), Value::u8(3)]),
});
assert_eq!(FooVar::deserialize(val), Ok(FooVar(MyEnum::Foo(1, 2, 3))));
}
#[test]
fn de_unwrapped_into_newtype_struct() {
#[derive(Deserialize, Debug, PartialEq)]
struct FooStr(String);
let val = Primitive::Str("hello".into());
assert_eq!(FooStr::deserialize(val), Ok(FooStr("hello".into())));
#[derive(Deserialize, Debug, PartialEq)]
struct FooVecU8(Vec<u8>);
let val = Composite::Unnamed(vec![Value::u8(1), Value::u8(2), Value::u8(3)]);
assert_eq!(FooVecU8::deserialize(val), Ok(FooVecU8(vec![1, 2, 3])));
#[derive(Deserialize, Debug, PartialEq)]
enum MyEnum {
Foo(u8, u8, u8),
}
#[derive(Deserialize, Debug, PartialEq)]
struct FooVar(MyEnum);
let val =
Variant { name: "Foo".into(), values: Composite::Unnamed(vec![Value::u8(1), Value::u8(2), Value::u8(3)]) };
assert_eq!(FooVar::deserialize(val), Ok(FooVar(MyEnum::Foo(1, 2, 3))));
}
#[test]
fn de_into_vec() {
let val = ValueDef::Composite(Composite::Unnamed(vec![Value::u8(1), Value::u8(2), Value::u8(3)]));
assert_eq!(<Vec<u8>>::deserialize(val), Ok(vec![1, 2, 3]));
let val = ValueDef::Composite(Composite::Unnamed(vec![
Value::str("a".into()),
Value::str("b".into()),
Value::str("c".into()),
]));
assert_eq!(<Vec<String>>::deserialize(val), Ok(vec!["a".into(), "b".into(), "c".into()]));
}
#[test]
fn de_unwrapped_into_vec() {
let val = Composite::Unnamed(vec![Value::u8(1), Value::u8(2), Value::u8(3)]);
assert_eq!(<Vec<u8>>::deserialize(val), Ok(vec![1, 2, 3]));
let val =
Composite::Named(vec![("a".into(), Value::u8(1)), ("b".into(), Value::u8(2)), ("c".into(), Value::u8(3))]);
assert_eq!(<Vec<u8>>::deserialize(val), Ok(vec![1, 2, 3]));
let val = Composite::Unnamed(vec![Value::str("a".into()), Value::str("b".into()), Value::str("c".into())]);
assert_eq!(<Vec<String>>::deserialize(val), Ok(vec!["a".into(), "b".into(), "c".into()]));
}
#[test]
fn de_into_map() {
use std::collections::HashMap;
let val = ValueDef::Composite(Composite::Named(vec![
("a".into(), Value::u8(1)),
("b".into(), Value::u8(2)),
("c".into(), Value::u8(3)),
]));
assert_eq!(
<HashMap<String, u8>>::deserialize(val),
Ok(vec![("a".into(), 1), ("b".into(), 2), ("c".into(), 3)].into_iter().collect())
);
let val = ValueDef::Composite(Composite::Unnamed(vec![Value::u8(1), Value::u8(2), Value::u8(3)]));
<HashMap<String, u8>>::deserialize(val).expect_err("no names; can't be map");
}
#[test]
fn de_into_tuple() {
let val = ValueDef::Composite(Composite::Unnamed(vec![Value::str("hello".into()), Value::bool(true)]));
assert_eq!(<(String, bool)>::deserialize(val), Ok(("hello".into(), true)));
let val = ValueDef::Composite(Composite::Named(vec![
("a".into(), Value::str("hello".into())),
("b".into(), Value::bool(true)),
]));
assert_eq!(<(String, bool)>::deserialize(val), Ok(("hello".into(), true)));
let val = ValueDef::Variant(Variant {
name: "Foo".into(),
values: Composite::Unnamed(vec![Value::str("hello".into()), Value::bool(true)]),
});
assert_eq!(<(String, bool)>::deserialize(val), Ok(("hello".into(), true)));
let val = ValueDef::Variant(Variant {
name: "Foo".into(),
values: Composite::Named(vec![("a".into(), Value::str("hello".into())), ("b".into(), Value::bool(true))]),
});
assert_eq!(<(String, bool)>::deserialize(val), Ok(("hello".into(), true)));
let val = ValueDef::Composite(Composite::Unnamed(vec![
Value::str("hello".into()),
Value::bool(true),
Value::u8(123),
]));
<(String, bool)>::deserialize(val).expect_err("Wrong length, should err");
}
#[test]
fn de_unwrapped_into_tuple() {
let val = Composite::Unnamed(vec![Value::str("hello".into()), Value::bool(true)]);
assert_eq!(<(String, bool)>::deserialize(val), Ok(("hello".into(), true)));
let val = Composite::Named(vec![("a".into(), Value::str("hello".into())), ("b".into(), Value::bool(true))]);
assert_eq!(<(String, bool)>::deserialize(val), Ok(("hello".into(), true)));
let val = Composite::Unnamed(vec![Value::str("hello".into()), Value::bool(true), Value::u8(123)]);
<(String, bool)>::deserialize(val).expect_err("Wrong length, should err");
}
#[test]
fn de_bitvec() {
use bitvec::{bitvec, order::Lsb0};
let val = Value::bit_sequence(bitvec![Lsb0, u8; 0, 1, 1, 0, 1, 0, 1, 0]);
assert_eq!(BitSequence::deserialize(val), Ok(bitvec![Lsb0, u8; 0, 1, 1, 0, 1, 0, 1, 0]));
let val = Value::bit_sequence(bitvec![Lsb0, u8; 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0]);
assert_eq!(
BitSequence::deserialize(val),
Ok(bitvec![Lsb0, u8; 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0])
);
}
#[test]
fn de_into_tuple_variant() {
#[derive(Deserialize, Debug, PartialEq)]
enum MyEnum {
Foo(String, bool, u8),
}
let val = ValueDef::Variant(Variant {
name: "Foo".into(),
values: Composite::Unnamed(vec![Value::str("hello".into()), Value::bool(true), Value::u8(123)]),
});
assert_eq!(MyEnum::deserialize(val), Ok(MyEnum::Foo("hello".into(), true, 123)));
let val = ValueDef::Variant(Variant {
name: "Foo".into(),
values: Composite::Named(vec![
("a".into(), Value::str("hello".into())),
("b".into(), Value::bool(true)),
("c".into(), Value::u8(123)),
]),
});
assert_eq!(MyEnum::deserialize(val), Ok(MyEnum::Foo("hello".into(), true, 123)));
}
#[test]
fn de_unwrapped_into_tuple_variant() {
#[derive(Deserialize, Debug, PartialEq)]
enum MyEnum {
Foo(String, bool, u8),
}
let val = Variant {
name: "Foo".into(),
values: Composite::Unnamed(vec![Value::str("hello".into()), Value::bool(true), Value::u8(123)]),
};
assert_eq!(MyEnum::deserialize(val), Ok(MyEnum::Foo("hello".into(), true, 123)));
let val = Variant {
name: "Foo".into(),
values: Composite::Named(vec![
("a".into(), Value::str("hello".into())),
("b".into(), Value::bool(true)),
("c".into(), Value::u8(123)),
]),
};
assert_eq!(MyEnum::deserialize(val), Ok(MyEnum::Foo("hello".into(), true, 123)));
}
#[test]
fn de_into_struct_variant() {
#[derive(Deserialize, Debug, PartialEq)]
enum MyEnum {
Foo { hi: String, a: bool, b: u8 },
}
let val = ValueDef::Variant(Variant {
name: "Foo".into(),
values: Composite::Named(vec![
("b".into(), Value::u8(123)),
("a".into(), Value::bool(true)),
("hi".into(), Value::str("hello".into())),
]),
});
assert_eq!(MyEnum::deserialize(val), Ok(MyEnum::Foo { hi: "hello".into(), a: true, b: 123 }));
let val = ValueDef::Variant(Variant {
name: "Foo".into(),
values: Composite::Unnamed(vec![Value::str("hello".into()), Value::bool(true), Value::u8(123)]),
});
assert_eq!(MyEnum::deserialize(val), Ok(MyEnum::Foo { hi: "hello".into(), a: true, b: 123 }));
let val = ValueDef::Variant(Variant {
name: "Foo".into(),
values: Composite::Unnamed(vec![Value::bool(true), Value::u8(123), Value::str("hello".into())]),
});
MyEnum::deserialize(val).expect_err("Wrong order shouldn't work");
let val = ValueDef::Variant(Variant {
name: "Foo".into(),
values: Composite::Named(vec![
("b".into(), Value::u8(123)),
("c".into(), Value::bool(true)),
("hi".into(), Value::str("hello".into())),
]),
});
MyEnum::deserialize(val).expect_err("Wrong names shouldn't work");
let val = ValueDef::Variant(Variant {
name: "Foo".into(),
values: Composite::Named(vec![
("foo".into(), Value::u8(40)),
("b".into(), Value::u8(123)),
("a".into(), Value::bool(true)),
("bar".into(), Value::bool(false)),
("hi".into(), Value::str("hello".into())),
]),
});
assert_eq!(MyEnum::deserialize(val), Ok(MyEnum::Foo { hi: "hello".into(), a: true, b: 123 }));
}
#[test]
fn de_into_unit_variants() {
let val = Value::variant("Foo".into(), Composite::Named(vec![]));
let unwrapped_val = Variant::<()> { name: "Foo".into(), values: Composite::Named(vec![]) };
#[derive(Deserialize, Debug, PartialEq)]
enum MyEnum {
Foo,
}
assert_eq!(MyEnum::deserialize(val.clone()), Ok(MyEnum::Foo));
assert_eq!(MyEnum::deserialize(unwrapped_val.clone()), Ok(MyEnum::Foo));
#[derive(Deserialize, Debug, PartialEq)]
enum MyEnum2 {
Foo(),
}
assert_eq!(MyEnum2::deserialize(val.clone()), Ok(MyEnum2::Foo()));
assert_eq!(MyEnum2::deserialize(unwrapped_val.clone()), Ok(MyEnum2::Foo()));
#[derive(Deserialize, Debug, PartialEq)]
enum MyEnum3 {
Foo {},
}
assert_eq!(MyEnum3::deserialize(val), Ok(MyEnum3::Foo {}));
assert_eq!(MyEnum3::deserialize(unwrapped_val), Ok(MyEnum3::Foo {}));
}
}