~tdeo/serde_bare

f894a9d61256456d45c5de91cab61d066dda1ec1 — Tadeo Kondrak 11 months ago fdf8d3f
Add i128/u128 support using data<16>
4 files changed, 34 insertions(+), 16 deletions(-)

M src/de.rs
M src/error.rs
M src/lib.rs
M src/ser.rs
M src/de.rs => src/de.rs +18 -6
@@ 82,12 82,14 @@ where
    }

    serde::serde_if_integer128! {
        /// Returns Error::I128Unsupported.
        fn deserialize_i128<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
        /// BARE type: data<16>
        fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
        where
            V: de::Visitor<'de>
        {
            Err(Error::I128Unsupported)
            let mut buf = [0u8; 16];
            self.reader.read_exact(&mut buf).map_err(|e| Error::Io(e))?;
            visitor.visit_i128(i128::from_le_bytes(buf))
        }
    }



@@ 132,12 134,14 @@ where
    }

    serde::serde_if_integer128! {
        /// Returns Error::U128Unsupported.
        fn deserialize_u128<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
        /// BARE type: data<16>
        fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
        where
            V: de::Visitor<'de>
        {
            Err(Error::U128Unsupported)
            let mut buf = [0u8; 16];
            self.reader.read_exact(&mut buf).map_err(|e| Error::Io(e))?;
            visitor.visit_u128(u128::from_le_bytes(buf))
        }
    }



@@ 533,6 537,10 @@ mod test {
            578437695752307201i64,
            from_slice(&[1, 2, 3, 4, 5, 6, 7, 8]).unwrap()
        );
        assert_eq!(
            21345817372864405881847059188222722561i128,
            from_slice(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]).unwrap()
        );
    }

    #[test]


@@ 544,6 552,10 @@ mod test {
            578437695752307201u64,
            from_slice(&[1, 2, 3, 4, 5, 6, 7, 8]).unwrap()
        );
        assert_eq!(
            21345817372864405881847059188222722561u128,
            from_slice(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]).unwrap()
        );
    }

    #[test]

M src/error.rs => src/error.rs +0 -4
@@ 12,8 12,6 @@ pub enum Error {
    Io(io::Error),

    AnyUnsupported,
    I128Unsupported,
    U128Unsupported,
    IdentifierUnsupported,

    InvalidUtf8,


@@ 40,8 38,6 @@ impl Display for Error {
            Error::Message(msg) => formatter.write_str(msg),
            Error::Io(e) => e.fmt(formatter),
            Error::AnyUnsupported => formatter.write_str("BARE does not support any"),
            Error::U128Unsupported => formatter.write_str("Bare does not support u128"),
            Error::I128Unsupported => formatter.write_str("Bare does not support i128"),
            Error::IdentifierUnsupported => formatter.write_str("BARE does not support identifier"),
            Error::InvalidUtf8 => formatter.write_str("invalid utf-8 in string"),
            Error::LengthOverflow => formatter.write_str("length above u32::MAX"),

M src/lib.rs => src/lib.rs +4 -0
@@ 7,6 7,10 @@
//!
//! Mapped exactly.
//!
//! ## `u128`, `i128`
//!
//! Encoded in the same fashion as the other integers, but the type is data<16>.
//!
//! ## `[length]type`, `data<length>`
//! Can be used as a `[T; N]` where Serde has an implementation for it, or manually with deserialize_tuple.
//!

M src/ser.rs => src/ser.rs +12 -6
@@ 64,9 64,12 @@ where
    }

    serde::serde_if_integer128! {
        /// Returns Error::I128Unsupported.
        fn serialize_i128(self, _v: i128) -> Result<Self::Ok, Self::Error> {
            Err(Error::I128Unsupported)
        /// 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(|e| Error::Io(e))?;
            Ok(())
        }
    }



@@ 103,9 106,12 @@ where
    }

    serde::serde_if_integer128! {
        /// Returns Error::U128Unsupported.
        fn serialize_u128(self, _v: u128) -> Result<Self::Ok, Self::Error> {
            Err(Error::U128Unsupported)
        /// 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(|e| Error::Io(e))?;
            Ok(())
        }
    }