#serde-serialize #error #serialization #serde #macro #proc-macro

impl_serialize

Macro for common implementing methods in serde::Serializer trait

7 stable releases

3.1.4 Oct 16, 2022
3.1.3 Sep 6, 2022
2.0.1 Sep 3, 2022
1.0.0 Sep 2, 2022

#661 in Encoding


Used in json_map_serializer

MIT/Apache

29KB
685 lines

impl_serialize!

This library provides a simple procedural macro for fast implementing serialize methods in serde::Serializer trait.

[dependencies]
impl_serialize = "3.1"

Example

Read about using metavariables inside impl_serialize!

use impl_serialize::impl_serialize;
use serde::ser;
use thiserror::Error;

#[derive(Debug, Error)]
enum SerializationError {
    #[error("Other error")]
    OtherError,
    #[error("Cannot serialize value from {0}")]
    CannotSerializeFrom(String),
    #[error("Custom({0})")]
    Custom(String)
}

impl serde::ser::Error for SerializationError {
    fn custom<T>(msg:T) -> Self
    where T: std::fmt::Display
    {
        SerializationError::Custom(msg.to_string())
    }
}

struct MySerializer;

impl ser::Serializer for MySerializer {
    type Ok = ();
    type Error = SerializationError;

    type SerializeMap = ser::Impossible<Self::Ok, Self::Error>;
    type SerializeSeq = ser::Impossible<Self::Ok, Self::Error>;
    type SerializeStruct = ser::Impossible<Self::Ok, Self::Error>;
    type SerializeStructVariant = ser::Impossible<Self::Ok, Self::Error>;
    type SerializeTuple = ser::Impossible<Self::Ok, Self::Error>;
    type SerializeTupleStruct = ser::Impossible<Self::Ok, Self::Error>;
    type SerializeTupleVariant = ser::Impossible<Self::Ok, Self::Error>;

    //value_type is metavariable (&str) what represents any serializing value type.
    //for example, value_type will be "i8" when seializing i8 or "bytes" when &[u8] (bytes);

    //with value_type
    impl_serialize!(
        Err(SerializationError::CannotSerializeFrom(value_type.to_string())),
        bool
    );
    
    //without value_type
    impl_serialize!(
        Err(SerializationError::OtherError),
        char
    );
    
    //for many types
    impl_serialize!(
        Err(SerializationError::CannotSerializeFrom(value_type.to_string())),
        [
            bytes,
            i8, i16, i32, i64,
            u8, u16, u32, u64,
            f32, f64,
            str,
            none, some, unit,
            unit_struct, unit_variant,
            newtype_struct, newtype_variant,
            seq, map,
            tuple, tuple_struct, tuple_variant,
            struct, struct_variant
        ]
    );
}

Dependencies

~110–355KB