~tdeo/serde_bare

615f278f3f7558c7a15ba81b14c6405cfa1c1344 — Tadeo Kondrak 11 months ago d6f414a
Miscellaneous updates
3 files changed, 53 insertions(+), 69 deletions(-)

M src/de.rs
M src/error.rs
M src/ser.rs
M src/de.rs => src/de.rs +14 -5
@@ 171,7 171,7 @@ where
    where
        V: de::Visitor<'de>,
    {
        let codepoint = <u32 as de::Deserialize>::deserialize(&mut *self)?;
        let codepoint = <u32 as de::Deserialize>::deserialize(self)?;
        visitor.visit_char(codepoint.try_into().map_err(|_| Error::InvalidChar)?)
    }



@@ 229,7 229,7 @@ where
        visitor.visit_byte_buf(buf)
    }

    /// BARE type: optional<T>
    /// BARE type: optional<type>
    fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: de::Visitor<'de>,


@@ 241,7 241,7 @@ where
        }
    }

    /// Unserialized type.
    /// BARE type: void
    fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: de::Visitor<'de>,


@@ 249,7 249,7 @@ where
        visitor.visit_unit()
    }

    /// Unserialized type.
    /// BARE type: void
    fn deserialize_unit_struct<V>(
        self,
        _name: &'static str,


@@ 261,7 261,7 @@ where
        visitor.visit_unit()
    }

    /// Unserialized type.
    /// BARE type: void
    fn deserialize_newtype_struct<V>(
        self,
        _name: &'static str,


@@ 541,6 541,7 @@ mod test {
    fn test_bool() {
        assert_eq!(false, from_slice(&[0]).unwrap());
        assert_eq!(true, from_slice(&[1]).unwrap());
        assert_eq!(true, from_slice(&[2]).unwrap());
    }

    #[test]


@@ 621,4 622,12 @@ mod test {
            from_slice::<Option<u32>>(&[1, 1, 2, 3, 4]).unwrap()
        );
    }

    #[test]
    fn test_slice() {
        assert_eq!(
            &[0u8; 4][..],
            &*from_slice::<Box<[u8]>>(&[4, 0, 0, 0, 0]).unwrap()
        );
    }
}

M src/error.rs => src/error.rs +0 -2
@@ 17,7 17,6 @@ pub enum Error {
    InvalidUtf8,
    InvalidChar,

    LengthOverflow,
    MapLengthRequired,
}



@@ 42,7 41,6 @@ impl Display for Error {
            Error::IdentifierUnsupported => formatter.write_str("BARE does not support identifier"),
            Error::InvalidUtf8 => formatter.write_str("invalid utf-8 in string"),
            Error::InvalidChar => formatter.write_str("invalid unicode codepoint in char"),
            Error::LengthOverflow => formatter.write_str("length above u32::MAX"),
            Error::MapLengthRequired => formatter.write_str("map length required"),
        }
    }

M src/ser.rs => src/ser.rs +39 -62
@@ 1,6 1,6 @@
use crate::error::Error;
use crate::{error::Error, Uint};
use serde::{ser, Serialize};
use std::{convert::TryInto, io::Write};
use std::io::Write;

pub struct Serializer<W> {
    writer: W,


@@ 33,82 33,66 @@ where

    /// BARE type: i8
    fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> {
        self.writer.write_all(&v.to_le_bytes()).map_err(Error::Io)?;
        Ok(())
        self.writer.write_all(&v.to_le_bytes()).map_err(Error::Io)
    }

    /// BARE type: i16
    fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> {
        self.writer.write_all(&v.to_le_bytes()).map_err(Error::Io)?;
        Ok(())
        self.writer.write_all(&v.to_le_bytes()).map_err(Error::Io)
    }

    /// BARE type: i32
    fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> {
        self.writer.write_all(&v.to_le_bytes()).map_err(Error::Io)?;
        Ok(())
        self.writer.write_all(&v.to_le_bytes()).map_err(Error::Io)
    }

    /// BARE type: i64
    fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> {
        self.writer.write_all(&v.to_le_bytes()).map_err(Error::Io)?;
        Ok(())
        self.writer.write_all(&v.to_le_bytes()).map_err(Error::Io)
    }

    serde::serde_if_integer128! {
        /// BARE type: data<16>
        /// BARE type: data\<16\>
        fn serialize_i128(self, v: i128) -> Result<Self::Ok, Self::Error> {
            self.writer
                .write_all(&v.to_le_bytes())
                .map_err(Error::Io)?;
            Ok(())
            self.writer.write_all(&v.to_le_bytes()).map_err(Error::Io)
        }
    }

    /// BARE type: u8
    fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> {
        self.writer.write_all(&v.to_le_bytes()).map_err(Error::Io)?;
        Ok(())
        self.writer.write_all(&v.to_le_bytes()).map_err(Error::Io)
    }

    /// BARE type: u16
    fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> {
        self.writer.write_all(&v.to_le_bytes()).map_err(Error::Io)?;
        Ok(())
        self.writer.write_all(&v.to_le_bytes()).map_err(Error::Io)
    }

    /// BARE type: u32
    fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> {
        self.writer.write_all(&v.to_le_bytes()).map_err(Error::Io)?;
        Ok(())
        self.writer.write_all(&v.to_le_bytes()).map_err(Error::Io)
    }

    /// BARE type: u64
    fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> {
        self.writer.write_all(&v.to_le_bytes()).map_err(Error::Io)?;
        Ok(())
        self.writer.write_all(&v.to_le_bytes()).map_err(Error::Io)
    }

    serde::serde_if_integer128! {
        /// BARE type: data<16>
        fn serialize_u128(self, v: u128) -> Result<Self::Ok, Self::Error> {
            self.writer
                .write_all(&v.to_le_bytes())
                .map_err(Error::Io)?;
            Ok(())
            self.writer.write_all(&v.to_le_bytes()).map_err(Error::Io)
        }
    }

    /// BARE type: f32
    fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> {
        self.writer.write_all(&v.to_le_bytes()).map_err(Error::Io)?;
        Ok(())
        self.writer.write_all(&v.to_le_bytes()).map_err(Error::Io)
    }

    /// BARE type: f64
    fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error> {
        self.writer.write_all(&v.to_le_bytes()).map_err(Error::Io)?;
        Ok(())
        self.writer.write_all(&v.to_le_bytes()).map_err(Error::Io)
    }

    /// BARE type: u32


@@ 117,29 101,23 @@ where
    }

    /// BARE type: string
    /// Restriction: len < u32::MAX, or Error::LengthOverflow is returned.
    fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> {
        let len: u32 = v.len().try_into().map_err(|_| Error::LengthOverflow)?;
        self.serialize_u32(len)?;
        self.writer.write_all(v.as_bytes()).map_err(Error::Io)?;
        Ok(())
        Uint(v.len() as u64).serialize(&mut *self)?;
        self.writer.write_all(v.as_bytes()).map_err(Error::Io)
    }

    /// BARE type: data
    /// Restriction: len < u32::MAX, or Error::LengthOverflow is returned.
    fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> {
        let len: u32 = v.len().try_into().map_err(|_| Error::LengthOverflow)?;
        self.serialize_u32(len)?;
        self.writer.write_all(v).map_err(Error::Io)?;
        Ok(())
        Uint(v.len() as u64).serialize(&mut *self)?;
        self.writer.write_all(v).map_err(Error::Io)
    }

    /// BARE type: optional<T>
    /// BARE type: optional\<type\>
    fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
        self.serialize_u8(0)
    }

    /// BARE type: optional<T>
    /// BARE type: optional\<type\>
    fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
    where
        T: Serialize,


@@ 148,27 126,29 @@ where
        value.serialize(self)
    }

    /// No-op.
    /// BARE type: void
    fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
        Ok(())
    }

    /// No-op.
    /// BARE type: void
    fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
        self.serialize_unit()
    }

    /// BARE type: u32
    /// BARE type: uint or (... | void | ...)
    /// `name` and `variant` are ignored.
    fn serialize_unit_variant(
        self,
        _name: &'static str,
        variant_index: u32,
        _variant: &'static str,
    ) -> Result<Self::Ok, Self::Error> {
        self.serialize_u32(variant_index)
        Uint(variant_index.into()).serialize(self)
    }

    /// BARE type: T
    /// `name` is ignored.
    fn serialize_newtype_struct<T: ?Sized>(
        self,
        _name: &'static str,


@@ 180,7 160,8 @@ where
        value.serialize(self)
    }

    /// BARE type: { u32, struct }
    /// BARE type: { uint, T } or (... | T | ...)
    /// `name` and `variant` are ignored.
    fn serialize_newtype_variant<T: ?Sized>(
        self,
        _name: &'static str,


@@ 191,27 172,27 @@ where
    where
        T: Serialize,
    {
        self.serialize_u32(variant_index)?;
        Uint(variant_index.into()).serialize(&mut *self)?;
        value.serialize(self)
    }

    /// BARE type if len is None: []type
    /// BARE type if len is Some: \[len\]type
    /// Error::LengthOverflow if len > u32::MAX
    fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
        if let Some(len) = len {
            self.serialize_u32(len.try_into().map_err(|_| Error::LengthOverflow)?)?;
            Uint(len as u64).serialize(&mut *self)?;
        }
        Ok(self)
    }

    /// BARE type: \[len\]type
    /// Error::LengthOverflow if len > u32::MAX
    /// `len` is ignored.
    fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
        Ok(self)
    }

    /// BARE type: struct
    /// `name` and `len` are ignored.
    fn serialize_tuple_struct(
        self,
        _name: &'static str,


@@ 220,7 201,8 @@ where
        Ok(self)
    }

    /// BARE type: { u32, struct }
    /// BARE type: { uint, T } or (... | T | ...)
    /// `name`, `variant`, and `len` are ignored.
    fn serialize_tuple_variant(
        self,
        _name: &'static str,


@@ 228,19 210,14 @@ where
        _variant: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeTupleVariant, Self::Error> {
        self.serialize_u32(variant_index)?;
        Uint(variant_index.into()).serialize(&mut *self)?;
        Ok(self)
    }

    /// BARE type: map\[T\]U
    /// Error::MapLengthRequired if len is None
    /// Error::LengthOverflow if len > u32::MAX
    fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
        self.serialize_u32(
            len.ok_or(Error::MapLengthRequired)?
                .try_into()
                .map_err(|_| Error::LengthOverflow)?,
        )?;
        Uint(len.ok_or(Error::MapLengthRequired)? as u64).serialize(&mut *self)?;
        Ok(self)
    }



@@ 253,7 230,7 @@ where
        Ok(self)
    }

    /// BARE type: { u32, struct }
    /// BARE type: { uint, T } or (... | T | ...)
    fn serialize_struct_variant(
        self,
        _name: &'static str,


@@ 261,7 238,7 @@ where
        _variant: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeStructVariant, Self::Error> {
        self.serialize_u32(variant_index)?;
        Uint(variant_index.into()).serialize(&mut *self)?;
        Ok(self)
    }