#serialization #proc-macro #structures #serde #assist #nested #flatly

serde_inner_serialize

A package of proc macros to assist in serializing nested structures flatly via serde

1 unstable release

new 0.1.1 Dec 28, 2024

#252 in Procedural macros

Download history 93/week @ 2024-12-22

95 downloads per month

MIT license

17KB
174 lines

serde_inner_serialize macro crate

Helper for custom Serializers

This library is designed to add some helper methods to a struct making it easier to manually serialize its fields.

pub trait InnerSerializableTrait {
    const TYPE_NAME: &'static str;
    fn count_fields() -> usize;
    fn inner_serialize<S>(&self, state: &mut S) -> Result<(), S::Error>
    where
        S: serde::ser::SerializeStruct;
}

pub trait OuterSerializableTrait<T> where T: InnerSerializableTrait {
    const TYPE_NAME: &'static str;

    fn get_full_type_name(&self) -> &'static str;
}

By deriving from InnerSerializable and OuterSerializable these traits and their implementation can be added to your custom structures, making it easy to represent "alternative versions" of a structure that add additional fields (or computed values that must be serialized) while retaining a "flat" output.

For an example of how to use these types in practice, see this example file

The structure of this repo is based on the guidelines found within https://towardsdatascience.com/nine-rules-for-creating-procedural-macros-in-rust-595aa476a7ff

Dependencies

~0.4–1MB
~21K SLoC