New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create a proc-macro attribute which applies a second attribute on all fields of a type. #85
Comments
A supplemental use-case for this is a companion to
Possible issues with this:
I guess the macro could just bail (refuse to compile) if it sees a |
You do not need
Multiple annotations work flawlessly. Their individual meanings get joined.
Similar to
I would not want the |
Well now I'm astonished, I was convinced it'd yield a parsing error. That is rather convenient, thank you very much. |
This `apply` proc-macro takes a list of type patterns and a list of attributes for each. On each field where the type matches, the list of attributes will be added to the existing field attributes.
524: Start work on an `apply` attribute #85 r=jonasbb a=jonasbb This `apply` proc-macro takes a list of type patterns and a list of attributes for each. On each field where the type matches, the list of attributes will be added to the existing field attributes. ```rust #[serde_with::apply( Option => #[serde(default)] #[serde(skip_serializing_if = "Option::is_none")], Option<bool> => #[serde(rename = "bool")], )] #[derive(serde::Serialize)] struct Data { a: Option<String>, b: Option<u64>, c: Option<String>, d: Option<bool>, } ``` The `apply` attribute will expand into this, applying the attributs to the matching fields: ```rust #[derive(serde::Serialize)] struct Data { #[serde(default)] #[serde(skip_serializing_if = "Option::is_none")] a: Option<String>, #[serde(default)] #[serde(skip_serializing_if = "Option::is_none")] b: Option<u64>, #[serde(default)] #[serde(skip_serializing_if = "Option::is_none")] c: Option<String>, #[serde(default)] #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "bool")] d: Option<bool>, } ``` bors r+ Co-authored-by: Jonas Bushart <jonas@bushart.org>
This `apply` proc-macro takes a list of type patterns and a list of attributes for each. On each field where the type matches, the list of attributes will be added to the existing field attributes.
524: Implement `apply` attribute #85 r=jonasbb a=jonasbb This `apply` proc-macro takes a list of type patterns and a list of attributes for each. On each field where the type matches, the list of attributes will be added to the existing field attributes. ```rust #[serde_with::apply( Option => #[serde(default)] #[serde(skip_serializing_if = "Option::is_none")], Option<bool> => #[serde(rename = "bool")], )] #[derive(serde::Serialize)] struct Data { a: Option<String>, b: Option<u64>, c: Option<String>, d: Option<bool>, } ``` The `apply` attribute will expand into this, applying the attributs to the matching fields: ```rust #[derive(serde::Serialize)] struct Data { #[serde(default)] #[serde(skip_serializing_if = "Option::is_none")] a: Option<String>, #[serde(default)] #[serde(skip_serializing_if = "Option::is_none")] b: Option<u64>, #[serde(default)] #[serde(skip_serializing_if = "Option::is_none")] c: Option<String>, #[serde(default)] #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "bool")] d: Option<bool>, } ``` bors r+ Co-authored-by: Jonas Bushart <jonas@bushart.org>
This is available since a bit as |
Serialization code is often repetitive. One helper to improve on this already exists in this crate, the
serde_with::skip_serializing_none
attribute.However, this could be generalized to work with any attribute on a user-specified type (list).
This could look like
This would apply the attribute on all fields of type
Vec
andHashMap
. On the right hand side, the placeholder$TYPE
would be replaced by the type of the field. This would make it easier to skip serializing all empty container types.serde_with::skip_serializing_none
could in the end be implemented on top of this more general proc-macro.The text was updated successfully, but these errors were encountered: