~tdeo/serde_bare

04278d356057e5b8c345428139dfeca75a858e4b — Tadeo Kondrak 1 year, 3 months ago 0c8b1c6
Improve documentation
3 files changed, 24 insertions(+), 10 deletions(-)

M src/de.rs
M src/lib.rs
M src/ser.rs
M src/de.rs => src/de.rs +9 -4
@@ 423,27 423,29 @@ where
            R: Read,
        {
            type Error = Error;
            type Variant = Variant<'a, R>;
            type Variant = Self;

            fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
            where
                V: de::DeserializeSeed<'de>,
            {
                let val = seed.deserialize(&mut *self.0)?;
                Ok((val, Variant(self.0)))
                Ok((val, self))
            }
        }
        struct Variant<'a, R>(&'a mut Deserializer<R>);
        impl<'de, 'a, R> de::VariantAccess<'de> for Variant<'a, R>

        impl<'de, 'a, R> de::VariantAccess<'de> for Enum<'a, R>
        where
            R: Read,
        {
            type Error = Error;

            /// Unserialized type.
            fn unit_variant(self) -> Result<(), Self::Error> {
                Ok(())
            }

            /// Bare type: T
            fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error>
            where
                T: de::DeserializeSeed<'de>,


@@ 451,6 453,7 @@ where
                seed.deserialize(self.0)
            }

            /// Bare type: struct
            fn tuple_variant<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
            where
                V: de::Visitor<'de>,


@@ 458,6 461,7 @@ where
                de::Deserializer::deserialize_tuple(self.0, len, visitor)
            }

            /// Bare type: struct
            fn struct_variant<V>(
                self,
                fields: &'static [&'static str],


@@ 488,6 492,7 @@ where
        Err(Error::AnyUnsupported)
    }

    /// Returns false.
    fn is_human_readable(&self) -> bool {
        false
    }

M src/lib.rs => src/lib.rs +12 -3
@@ 1,9 1,18 @@
#![forbid(unsafe_code)]
//!```text
//! serde_bare
//! # serde_bare
//!
//! An implementation of the BARE (https://git.sr.ht/~sircmpwn/bare) encoding format draft.
//!```
//!
//! Notes:
//!     - u8/u16/u32/u64, i8/i16/i32/i64, f32/f64, bool, string, data, optional, []type, map, and struct are mapped exactly.
//!     - e8/e16/e32/e64 have the same representation as the unsigned types.
//!     - Rust enums are serialized as { u32, fields } by default.
//!         - For enums without fields, this can be derived differently with `serde_repr`.
//!         - For enums with fields, this can be overridden with { uN, struct } representation or with a custom Deserialize implementation.
//!     - [length]type, data<length>
//!         - Can be used as a [T; N] where Serde has an implementation for it, or manually with deserialize_tuple.
//!     - (type | type | ...)
//!         - Cannot be derived as an enum directly, but can be a derived { u8, T } or a custom Deserialize implementation on an enum.

pub mod de;
pub mod error;

M src/ser.rs => src/ser.rs +3 -3
@@ 131,7 131,7 @@ where
    }

    /// BARE type: string
    /// Restriction: len < u32::MAX
    /// 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)?;


@@ 142,7 142,7 @@ where
    }

    /// BARE type: data
    /// Restriction: len < u32::MAX
    /// 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)?;


@@ 196,7 196,7 @@ where
        value.serialize(self)
    }

    /// BARE type: { u32, T }
    /// BARE type: { u32, struct }
    fn serialize_newtype_variant<T: ?Sized>(
        self,
        _name: &'static str,