serde_with_macros/lib.rs
1// Cleanup when workspace lints can be overridden
2// https://github.com/rust-lang/cargo/issues/13157
3#![forbid(unsafe_code)]
4#![warn(missing_copy_implementations, missing_debug_implementations)]
5#![doc(test(attr(
6 // Problematic handling for foreign From<T> impls in tests
7 // https://github.com/rust-lang/rust/issues/121621
8 allow(unknown_lints, non_local_definitions),
9 deny(
10 missing_debug_implementations,
11 rust_2018_idioms,
12 trivial_casts,
13 trivial_numeric_casts,
14 unused_extern_crates,
15 unused_import_braces,
16 unused_qualifications,
17 warnings,
18 ),
19 forbid(unsafe_code),
20)))]
21// Not needed for 2018 edition and conflicts with `rust_2018_idioms`
22#![doc(test(no_crate_inject))]
23#![doc(html_root_url = "https://docs.rs/serde_with_macros/3.14.0/")]
24// Tarpaulin does not work well with proc macros and marks most of the lines as uncovered.
25#![cfg(not(tarpaulin_include))]
26
27//! proc-macro extensions for [`serde_with`].
28//!
29//! This crate should **NEVER** be used alone.
30//! All macros **MUST** be used via the re-exports in the [`serde_with`] crate.
31//!
32//! [`serde_with`]: https://crates.io/crates/serde_with/
33
34mod apply;
35mod lazy_bool;
36mod utils;
37
38use crate::utils::{
39 split_with_de_lifetime, DeriveOptions, IteratorExt as _, SchemaFieldCondition,
40 SchemaFieldConfig,
41};
42use darling::{
43 ast::NestedMeta,
44 util::{Flag, Override},
45 Error as DarlingError, FromField, FromMeta,
46};
47use proc_macro::TokenStream;
48use proc_macro2::{Span, TokenStream as TokenStream2};
49use quote::quote;
50use syn::{
51 parse::Parser,
52 parse_macro_input, parse_quote,
53 punctuated::{Pair, Punctuated},
54 spanned::Spanned,
55 DeriveInput, Error, Field, Fields, GenericArgument, ItemEnum, ItemStruct, Meta, Path,
56 PathArguments, ReturnType, Token, Type,
57};
58
59/// Apply function on every field of structs or enums
60fn apply_function_to_struct_and_enum_fields<F>(
61 input: TokenStream,
62 function: F,
63) -> Result<TokenStream2, Error>
64where
65 F: Copy,
66 F: Fn(&mut Field) -> Result<(), String>,
67{
68 /// Handle a single struct or a single enum variant
69 fn apply_on_fields<F>(fields: &mut Fields, function: F) -> Result<(), Error>
70 where
71 F: Fn(&mut Field) -> Result<(), String>,
72 {
73 match fields {
74 // simple, no fields, do nothing
75 Fields::Unit => Ok(()),
76 Fields::Named(fields) => fields
77 .named
78 .iter_mut()
79 .map(|field| function(field).map_err(|err| Error::new(field.span(), err)))
80 .collect_error(),
81 Fields::Unnamed(fields) => fields
82 .unnamed
83 .iter_mut()
84 .map(|field| function(field).map_err(|err| Error::new(field.span(), err)))
85 .collect_error(),
86 }
87 }
88
89 // For each field in the struct given by `input`, add the `skip_serializing_if` attribute,
90 // if and only if, it is of type `Option`
91 if let Ok(mut input) = syn::parse::<ItemStruct>(input.clone()) {
92 apply_on_fields(&mut input.fields, function)?;
93 Ok(quote!(#input))
94 } else if let Ok(mut input) = syn::parse::<ItemEnum>(input) {
95 input
96 .variants
97 .iter_mut()
98 .map(|variant| apply_on_fields(&mut variant.fields, function))
99 .collect_error()?;
100 Ok(quote!(#input))
101 } else {
102 Err(Error::new(
103 Span::call_site(),
104 "The attribute can only be applied to struct or enum definitions.",
105 ))
106 }
107}
108
109/// Like [`apply_function_to_struct_and_enum_fields`] but for darling errors
110fn apply_function_to_struct_and_enum_fields_darling<F>(
111 input: TokenStream,
112 serde_with_crate_path: &Path,
113 function: F,
114) -> Result<TokenStream2, DarlingError>
115where
116 F: Copy,
117 F: Fn(&mut Field) -> Result<(), DarlingError>,
118{
119 /// Handle a single struct or a single enum variant
120 fn apply_on_fields<F>(fields: &mut Fields, function: F) -> Result<(), DarlingError>
121 where
122 F: Fn(&mut Field) -> Result<(), DarlingError>,
123 {
124 match fields {
125 // simple, no fields, do nothing
126 Fields::Unit => Ok(()),
127 Fields::Named(ref mut fields) => {
128 let errors: Vec<DarlingError> = fields
129 .named
130 .iter_mut()
131 .map(|field| function(field).map_err(|err| err.with_span(&field)))
132 // turn the Err variant into the Some, such that we only collect errors
133 .filter_map(std::result::Result::err)
134 .collect();
135 if errors.is_empty() {
136 Ok(())
137 } else {
138 Err(DarlingError::multiple(errors))
139 }
140 }
141 Fields::Unnamed(fields) => {
142 let errors: Vec<DarlingError> = fields
143 .unnamed
144 .iter_mut()
145 .map(|field| function(field).map_err(|err| err.with_span(&field)))
146 // turn the Err variant into the Some, such that we only collect errors
147 .filter_map(std::result::Result::err)
148 .collect();
149 if errors.is_empty() {
150 Ok(())
151 } else {
152 Err(DarlingError::multiple(errors))
153 }
154 }
155 }
156 }
157
158 // Add a dummy derive macro which consumes (makes inert) all field attributes
159 let consume_serde_as_attribute = parse_quote!(
160 #[derive(#serde_with_crate_path::__private_consume_serde_as_attributes)]
161 );
162
163 // For each field in the struct given by `input`, add the `skip_serializing_if` attribute,
164 // if and only if, it is of type `Option`
165 if let Ok(mut input) = syn::parse::<ItemStruct>(input.clone()) {
166 apply_on_fields(&mut input.fields, function)?;
167 input.attrs.push(consume_serde_as_attribute);
168 Ok(quote!(#input))
169 } else if let Ok(mut input) = syn::parse::<ItemEnum>(input) {
170 // Prevent serde_as on enum variants
171 let mut errors: Vec<DarlingError> = input
172 .variants
173 .iter()
174 .flat_map(|variant| {
175 variant.attrs.iter().filter_map(|attr| {
176 if attr.path().is_ident("serde_as") {
177 Some(
178 DarlingError::custom(
179 "serde_as attribute is not allowed on enum variants",
180 )
181 .with_span(&attr),
182 )
183 } else {
184 None
185 }
186 })
187 })
188 .collect();
189 // Process serde_as on all fields
190 errors.extend(
191 input
192 .variants
193 .iter_mut()
194 .map(|variant| apply_on_fields(&mut variant.fields, function))
195 // turn the Err variant into the Some, such that we only collect errors
196 .filter_map(std::result::Result::err),
197 );
198
199 if errors.is_empty() {
200 input.attrs.push(consume_serde_as_attribute);
201 Ok(quote!(#input))
202 } else {
203 Err(DarlingError::multiple(errors))
204 }
205 } else {
206 Err(DarlingError::custom(
207 "The attribute can only be applied to struct or enum definitions.",
208 )
209 .with_span(&Span::call_site()))
210 }
211}
212
213/// Add `skip_serializing_if` annotations to [`Option`] fields.
214///
215/// The attribute can be added to structs and enums.
216/// The `#[skip_serializing_none]` attribute must be placed *before* the `#[derive]` attribute.
217///
218/// # Example
219///
220/// JSON APIs sometimes have many optional values.
221/// Missing values should not be serialized, to keep the serialized format smaller.
222/// Such a data type might look like:
223///
224/// ```rust
225/// # use serde::Serialize;
226/// #
227/// # #[allow(dead_code)]
228/// #[derive(Serialize)]
229/// struct Data {
230/// #[serde(skip_serializing_if = "Option::is_none")]
231/// a: Option<String>,
232/// #[serde(skip_serializing_if = "Option::is_none")]
233/// b: Option<u64>,
234/// #[serde(skip_serializing_if = "Option::is_none")]
235/// c: Option<String>,
236/// #[serde(skip_serializing_if = "Option::is_none")]
237/// d: Option<bool>,
238/// }
239/// ```
240///
241/// The `skip_serializing_if` annotation is repetitive and harms readability.
242/// Instead, the same struct can be written as:
243///
244/// ```rust
245/// # use serde::Serialize;
246/// # use serde_with_macros::skip_serializing_none;
247/// #
248/// # #[allow(dead_code)]
249/// #[skip_serializing_none]
250/// #[derive(Serialize)]
251/// struct Data {
252/// a: Option<String>,
253/// b: Option<u64>,
254/// c: Option<String>,
255/// // Always serialize field d even if None
256/// #[serialize_always]
257/// d: Option<bool>,
258/// }
259/// ```
260///
261/// Existing `skip_serializing_if` annotations will not be altered.
262///
263/// If some values should always be serialized, then `serialize_always` can be used.
264///
265/// # Limitations
266///
267/// The `serialize_always` cannot be used together with a manual `skip_serializing_if` annotations,
268/// as these conflict in their meaning. A compile error will be generated if this occurs.
269///
270/// The `skip_serializing_none` only works if the type is called `Option`,
271/// `std::option::Option`, or `core::option::Option`. Type aliasing an [`Option`] and giving it
272/// another name, will cause this field to be ignored. This cannot be supported, as proc-macros run
273/// before type checking, thus it is not possible to determine if a type alias refers to an
274/// [`Option`].
275///
276/// ```rust
277/// # use serde::Serialize;
278/// # use serde_with_macros::skip_serializing_none;
279/// # #[allow(dead_code)]
280/// type MyOption<T> = Option<T>;
281///
282/// # #[allow(dead_code)]
283/// #[skip_serializing_none]
284/// #[derive(Serialize)]
285/// struct Data {
286/// a: MyOption<String>, // This field will not be skipped
287/// }
288/// ```
289///
290/// Likewise, if you import a type and name it `Option`, the `skip_serializing_if` attributes will
291/// be added and compile errors will occur, if `Option::is_none` is not a valid function.
292/// Here the function `Vec::is_none` does not exist, and therefore the example fails to compile.
293///
294/// ```rust,compile_fail
295/// # use serde::Serialize;
296/// # use serde_with_macros::skip_serializing_none;
297/// use std::vec::Vec as Option;
298///
299/// #[skip_serializing_none]
300/// #[derive(Serialize)]
301/// struct Data {
302/// a: Option<String>,
303/// }
304/// ```
305#[proc_macro_attribute]
306pub fn skip_serializing_none(_args: TokenStream, input: TokenStream) -> TokenStream {
307 let res =
308 apply_function_to_struct_and_enum_fields(input, skip_serializing_none_add_attr_to_field)
309 .unwrap_or_else(|err| err.to_compile_error());
310 TokenStream::from(res)
311}
312
313/// Add the `skip_serializing_if` annotation to each field of the struct
314fn skip_serializing_none_add_attr_to_field(field: &mut Field) -> Result<(), String> {
315 if is_std_option(&field.ty) {
316 let has_skip_serializing_if = field_has_attribute(field, "serde", "skip_serializing_if");
317
318 // Remove the `serialize_always` attribute
319 let mut has_always_attr = false;
320 field.attrs.retain(|attr| {
321 let has_attr = attr.path().is_ident("serialize_always");
322 has_always_attr |= has_attr;
323 !has_attr
324 });
325
326 // Error on conflicting attributes
327 if has_always_attr && has_skip_serializing_if {
328 let mut msg = r#"The attributes `serialize_always` and `serde(skip_serializing_if = "...")` cannot be used on the same field"#.to_string();
329 if let Some(ident) = &field.ident {
330 msg += ": `";
331 msg += &ident.to_string();
332 msg += "`";
333 }
334 msg += ".";
335 return Err(msg);
336 }
337
338 // Do nothing if `skip_serializing_if` or `serialize_always` is already present
339 if has_skip_serializing_if || has_always_attr {
340 return Ok(());
341 }
342
343 // Add the `skip_serializing_if` attribute
344 let attr = parse_quote!(
345 #[serde(skip_serializing_if = "Option::is_none")]
346 );
347 field.attrs.push(attr);
348 } else {
349 // Warn on use of `serialize_always` on non-Option fields
350 let has_attr = field
351 .attrs
352 .iter()
353 .any(|attr| attr.path().is_ident("serialize_always"));
354 if has_attr {
355 return Err("`serialize_always` may only be used on fields of type `Option`.".into());
356 }
357 }
358 Ok(())
359}
360
361/// Return `true`, if the type path refers to `std::option::Option`
362///
363/// Accepts
364///
365/// * `Option`
366/// * `std::option::Option`, with or without leading `::`
367/// * `core::option::Option`, with or without leading `::`
368fn is_std_option(type_: &Type) -> bool {
369 match type_ {
370 Type::Array(_)
371 | Type::BareFn(_)
372 | Type::ImplTrait(_)
373 | Type::Infer(_)
374 | Type::Macro(_)
375 | Type::Never(_)
376 | Type::Ptr(_)
377 | Type::Reference(_)
378 | Type::Slice(_)
379 | Type::TraitObject(_)
380 | Type::Tuple(_)
381 | Type::Verbatim(_) => false,
382
383 Type::Group(syn::TypeGroup { elem, .. })
384 | Type::Paren(syn::TypeParen { elem, .. })
385 | Type::Path(syn::TypePath {
386 qself: Some(syn::QSelf { ty: elem, .. }),
387 ..
388 }) => is_std_option(elem),
389
390 Type::Path(syn::TypePath { qself: None, path }) => {
391 (path.leading_colon.is_none()
392 && path.segments.len() == 1
393 && path.segments[0].ident == "Option")
394 || (path.segments.len() == 3
395 && (path.segments[0].ident == "std" || path.segments[0].ident == "core")
396 && path.segments[1].ident == "option"
397 && path.segments[2].ident == "Option")
398 }
399 _ => false,
400 }
401}
402
403/// Determine if the `field` has an attribute with given `namespace` and `name`
404///
405/// On the example of
406/// `#[serde(skip_serializing_if = "Option::is_none")]`
407///
408/// * `serde` is the outermost path, here namespace
409/// * it contains a `Meta::List`
410/// * which contains in another Meta a `Meta::NameValue`
411/// * with the name being `skip_serializing_if`
412fn field_has_attribute(field: &Field, namespace: &str, name: &str) -> bool {
413 for attr in &field.attrs {
414 if attr.path().is_ident(namespace) {
415 // Ignore non parsable attributes, as these are not important for us
416 if let Meta::List(expr) = &attr.meta {
417 let nested = match Punctuated::<Meta, Token![,]>::parse_terminated
418 .parse2(expr.tokens.clone())
419 {
420 Ok(nested) => nested,
421 Err(_) => continue,
422 };
423 for expr in nested {
424 match expr {
425 Meta::NameValue(expr) => {
426 if let Some(ident) = expr.path.get_ident() {
427 if *ident == name {
428 return true;
429 }
430 }
431 }
432 Meta::Path(expr) => {
433 if let Some(ident) = expr.get_ident() {
434 if *ident == name {
435 return true;
436 }
437 }
438 }
439 _ => (),
440 }
441 }
442 }
443 }
444 }
445 false
446}
447
448/// Convenience macro to use the [`serde_as`] system.
449///
450/// The [`serde_as`] system is designed as a more flexible alternative to serde's `with` annotation.
451/// The `#[serde_as]` attribute must be placed *before* the `#[derive]` attribute.
452/// Each field of a struct or enum can be annotated with `#[serde_as(...)]` to specify which
453/// transformations should be applied. `serde_as` is *not* supported on enum variants.
454/// This is in contrast to `#[serde(with = "...")]`.
455///
456/// # Example
457///
458/// ```rust,ignore
459/// use serde_with::{serde_as, DisplayFromStr, Map};
460///
461/// #[serde_as]
462/// #[derive(Serialize, Deserialize)]
463/// struct Data {
464/// /// Serialize into number
465/// #[serde_as(as = "_")]
466/// a: u32,
467///
468/// /// Serialize into String
469/// #[serde_as(as = "DisplayFromStr")]
470/// b: u32,
471///
472/// /// Serialize into a map from String to String
473/// #[serde_as(as = "Map<DisplayFromStr, _>")]
474/// c: Vec<(u32, String)>,
475/// }
476/// ```
477///
478/// # Alternative path to `serde_with` crate
479///
480/// If `serde_with` is not available at the default path, its path should be specified with the
481/// `crate` argument. See [re-exporting `serde_as`] for more use case information.
482///
483/// ```rust,ignore
484/// #[serde_as(crate = "::some_other_lib::serde_with")]
485/// #[derive(Deserialize)]
486/// struct Data {
487/// #[serde_as(as = "_")]
488/// a: u32,
489/// }
490/// ```
491///
492/// # What this macro does
493///
494/// The `serde_as` macro only serves a convenience function.
495/// All the steps it performs, can easily be done manually, in case the cost of an attribute macro
496/// is deemed too high. The functionality can best be described with an example.
497///
498/// ```rust,ignore
499/// #[serde_as]
500/// #[derive(serde::Serialize)]
501/// struct Foo {
502/// #[serde_as(as = "Vec<_>")]
503/// bar: Vec<u32>,
504///
505/// #[serde_as(as = "Option<DisplayFromStr>")]
506/// baz: Option<u32>,
507/// }
508/// ```
509///
510/// 1. All the placeholder type `_` will be replaced with `::serde_with::Same`.
511/// The placeholder type `_` marks all the places where the type's `Serialize` implementation
512/// should be used. In the example, it means that the `u32` values will serialize with the
513/// `Serialize` implementation of `u32`. The `Same` type implements `SerializeAs` whenever the
514/// underlying type implements `Serialize` and is used to make the two traits compatible.
515///
516/// If you specify a custom path for `serde_with` via the `crate` attribute, the path to the
517/// `Same` type will be altered accordingly.
518///
519/// 2. Wrap the type from the annotation inside a `::serde_with::As`.
520/// In the above example we now have something like `::serde_with::As::<Vec<::serde_with::Same>>`.
521/// The `As` type acts as the opposite of the `Same` type.
522/// It allows using a `SerializeAs` type whenever a `Serialize` is required.
523///
524/// 3. Translate the `*as` attributes into the serde equivalent ones.
525/// `#[serde_as(as = ...)]` will become `#[serde(with = ...)]`.
526/// Similarly, `serialize_as` is translated to `serialize_with`.
527///
528/// The field attributes will be kept on the struct/enum such that other macros can use them
529/// too.
530///
531/// 4. It searches `#[serde_as(as = ...)]` if there is a type named `BorrowCow` under any path.
532/// If `BorrowCow` is found, the attribute `#[serde(borrow)]` is added to the field.
533/// If `#[serde(borrow)]` or `#[serde(borrow = "...")]` is already present, this step will be
534/// skipped.
535///
536/// 5. Restore the ability of accepting missing fields if both the field and the transformation are `Option`.
537///
538/// An `Option` is detected by an exact text match.
539/// Renaming an import or type aliases can cause confusion here.
540/// The following variants are supported.
541/// * `Option`
542/// * `std::option::Option`, with or without leading `::`
543/// * `core::option::Option`, with or without leading `::`
544///
545/// If the field is of type `Option<T>` and the attribute `#[serde_as(as = "Option<S>")]` (also
546/// `deserialize_as`; for any `T`/`S`) then `#[serde(default)]` is applied to the field.
547///
548/// This restores the ability of accepting missing fields, which otherwise often leads to confusing [serde_with#185](https://github.com/jonasbb/serde_with/issues/185).
549/// `#[serde(default)]` is not applied, if it already exists.
550/// It only triggers if both field and transformation are `Option`s.
551/// For example, using `#[serde_as(as = "NoneAsEmptyString")]` on `Option<String>` will not see
552/// any change.
553///
554/// If the automatically applied attribute is undesired, the behavior can be suppressed by adding
555/// `#[serde_as(no_default)]`.
556///
557/// This can be combined like `#[serde_as(as = "Option<S>", no_default)]`.
558///
559/// After all these steps, the code snippet will have transformed into roughly this.
560///
561/// ```rust,ignore
562/// #[derive(serde::Serialize)]
563/// struct Foo {
564/// #[serde_as(as = "Vec<_>")]
565/// #[serde(with = "::serde_with::As::<Vec<::serde_with::Same>>")]
566/// bar: Vec<u32>,
567///
568/// #[serde_as(as = "Option<DisplayFromStr>")]
569/// #[serde(default)]
570/// #[serde(with = "::serde_with::As::<Option<DisplayFromStr>>")]
571/// baz: Option<u32>,
572/// }
573/// ```
574///
575/// # A note on `schemars` integration
576/// When the `schemars_0_8`, `schemars_0_9`, or `schemars_1` features are enabled this macro
577/// will scan for
578/// `#[derive(JsonSchema)]` attributes and, if found, will add
579/// `#[schemars(with = "Schema<T, ...>")]` annotations to any fields with a
580/// `#[serde_as(as = ...)]` annotation. If you wish to override the default
581/// behavior here you can add `#[serde_as(schemars = true)]` or
582/// `#[serde_as(schemars = false)]`.
583///
584/// Note that this macro will check for any of the following derive paths:
585/// * `JsonSchema`
586/// * `schemars::JsonSchema`
587/// * `::schemars::JsonSchema`
588///
589/// It will also work if the relevant derive is behind a `#[cfg_attr]` attribute
590/// and propagate the `#[cfg_attr]` to the various `#[schemars]` field attributes.
591///
592/// [`serde_as`]: https://docs.rs/serde_with/3.14.0/serde_with/guide/index.html
593/// [re-exporting `serde_as`]: https://docs.rs/serde_with/3.14.0/serde_with/guide/serde_as/index.html#re-exporting-serde_as
594#[proc_macro_attribute]
595pub fn serde_as(args: TokenStream, input: TokenStream) -> TokenStream {
596 #[derive(FromMeta)]
597 struct SerdeContainerOptions {
598 #[darling(rename = "crate")]
599 alt_crate_path: Option<Path>,
600 #[darling(rename = "schemars")]
601 enable_schemars_support: Option<bool>,
602 }
603
604 match NestedMeta::parse_meta_list(args.into()) {
605 Ok(list) => {
606 let container_options = match SerdeContainerOptions::from_list(&list) {
607 Ok(v) => v,
608 Err(e) => {
609 return TokenStream::from(e.write_errors());
610 }
611 };
612
613 let serde_with_crate_path = container_options
614 .alt_crate_path
615 .unwrap_or_else(|| syn::parse_quote!(::serde_with));
616
617 let schemars_config = match container_options.enable_schemars_support {
618 _ if cfg!(not(any(
619 feature = "schemars_0_8",
620 feature = "schemars_0_9",
621 feature = "schemars_1"
622 ))) =>
623 {
624 SchemaFieldConfig::False
625 }
626 Some(condition) => condition.into(),
627 None => utils::has_derive_jsonschema(input.clone()).unwrap_or_default(),
628 };
629
630 // Convert any error message into a nice compiler error
631 let res = apply_function_to_struct_and_enum_fields_darling(
632 input,
633 &serde_with_crate_path,
634 |field| serde_as_add_attr_to_field(field, &serde_with_crate_path, &schemars_config),
635 )
636 .unwrap_or_else(darling::Error::write_errors);
637 TokenStream::from(res)
638 }
639 Err(e) => TokenStream::from(DarlingError::from(e).write_errors()),
640 }
641}
642
643/// Inspect the field and convert the `serde_as` attribute into the classical `serde`
644fn serde_as_add_attr_to_field(
645 field: &mut Field,
646 serde_with_crate_path: &Path,
647 schemars_config: &SchemaFieldConfig,
648) -> Result<(), DarlingError> {
649 #[derive(FromField)]
650 #[darling(attributes(serde_as))]
651 struct SerdeAsOptions {
652 /// The original type of the field
653 ty: Type,
654
655 r#as: Option<Type>,
656 deserialize_as: Option<Type>,
657 serialize_as: Option<Type>,
658 no_default: Flag,
659 }
660
661 impl SerdeAsOptions {
662 fn has_any_set(&self) -> bool {
663 self.r#as.is_some() || self.deserialize_as.is_some() || self.serialize_as.is_some()
664 }
665 }
666
667 #[derive(FromField)]
668 #[darling(attributes(serde), allow_unknown_fields)]
669 struct SerdeOptions {
670 with: Option<String>,
671 deserialize_with: Option<String>,
672 serialize_with: Option<String>,
673
674 borrow: Option<Override<String>>,
675 default: Option<Override<String>>,
676 }
677
678 impl SerdeOptions {
679 fn has_any_set(&self) -> bool {
680 self.with.is_some() || self.deserialize_with.is_some() || self.serialize_with.is_some()
681 }
682 }
683
684 /// Emit a `borrow` annotation, if the replacement type requires borrowing.
685 fn emit_borrow_annotation(serde_options: &SerdeOptions, as_type: &Type, field: &mut Field) {
686 let type_borrowcow = &syn::parse_quote!(BorrowCow);
687 // If the field is not borrowed yet, check if we need to borrow it.
688 if serde_options.borrow.is_none() && has_type_embedded(as_type, type_borrowcow) {
689 let attr_borrow = parse_quote!(#[serde(borrow)]);
690 field.attrs.push(attr_borrow);
691 }
692 }
693
694 /// Emit a `default` annotation, if `as_type` and `field` are both `Option`.
695 fn emit_default_annotation(
696 serde_as_options: &SerdeAsOptions,
697 serde_options: &SerdeOptions,
698 as_type: &Type,
699 field: &mut Field,
700 ) {
701 if !serde_as_options.no_default.is_present()
702 && serde_options.default.is_none()
703 && is_std_option(as_type)
704 && is_std_option(&field.ty)
705 {
706 let attr_borrow = parse_quote!(#[serde(default)]);
707 field.attrs.push(attr_borrow);
708 }
709 }
710
711 // syn v2 no longer supports keywords in the path position of an attribute.
712 // That breaks #[serde_as(as = "FooBar")], since `as` is a keyword.
713 // For each attribute, that is named `serde_as`, we replace the `as` keyword with `r#as`.
714 let mut has_serde_as = false;
715 field.attrs.iter_mut().for_each(|attr| {
716 if attr.path().is_ident("serde_as") {
717 // We found a `serde_as` attribute.
718 // Remember that such that we can quick exit otherwise
719 has_serde_as = true;
720
721 if let Meta::List(metalist) = &mut attr.meta {
722 metalist.tokens = std::mem::take(&mut metalist.tokens)
723 .into_iter()
724 .map(|token| {
725 use proc_macro2::{Ident, TokenTree};
726
727 // Replace `as` with `r#as`.
728 match token {
729 TokenTree::Ident(ident) if ident == "as" => {
730 TokenTree::Ident(Ident::new_raw("as", ident.span()))
731 }
732 _ => token,
733 }
734 })
735 .collect();
736 }
737 }
738 });
739 // If there is no `serde_as` attribute, we can exit early.
740 if !has_serde_as {
741 return Ok(());
742 }
743 let serde_as_options = SerdeAsOptions::from_field(field)?;
744 let serde_options = SerdeOptions::from_field(field)?;
745
746 let mut errors = Vec::new();
747 if !serde_as_options.has_any_set() {
748 errors.push(DarlingError::custom("An empty `serde_as` attribute on a field has no effect. You are missing an `as`, `serialize_as`, or `deserialize_as` parameter."));
749 }
750
751 // Check if there are any conflicting attributes
752 if serde_as_options.has_any_set() && serde_options.has_any_set() {
753 errors.push(DarlingError::custom("Cannot combine `serde_as` with serde's `with`, `deserialize_with`, or `serialize_with`."));
754 }
755
756 if serde_as_options.r#as.is_some() && serde_as_options.deserialize_as.is_some() {
757 errors.push(DarlingError::custom("Cannot combine `as` with `deserialize_as`. Use `serialize_as` to specify different serialization code."));
758 } else if serde_as_options.r#as.is_some() && serde_as_options.serialize_as.is_some() {
759 errors.push(DarlingError::custom("Cannot combine `as` with `serialize_as`. Use `deserialize_as` to specify different deserialization code."));
760 }
761
762 if !errors.is_empty() {
763 return Err(DarlingError::multiple(errors));
764 }
765
766 let type_original = &serde_as_options.ty;
767 let type_same = &syn::parse_quote!(#serde_with_crate_path::Same);
768 if let Some(type_) = &serde_as_options.r#as {
769 emit_borrow_annotation(&serde_options, type_, field);
770 emit_default_annotation(&serde_as_options, &serde_options, type_, field);
771
772 let replacement_type = replace_infer_type_with_type(type_.clone(), type_same);
773 let attr_inner_tokens = quote!(#serde_with_crate_path::As::<#replacement_type>).to_string();
774 let attr = parse_quote!(#[serde(with = #attr_inner_tokens)]);
775 field.attrs.push(attr);
776
777 match schemars_config {
778 SchemaFieldConfig::False => {}
779 lhs => {
780 let rhs = utils::schemars_with_attr_if(
781 &field.attrs,
782 &["with", "serialize_with", "deserialize_with", "schema_with"],
783 )?;
784
785 match lhs & !rhs {
786 SchemaFieldConfig::False => {}
787 condition => {
788 let attr_inner_tokens = quote! {
789 #serde_with_crate_path::Schema::<#type_original, #replacement_type>
790 };
791 let attr_inner_tokens = attr_inner_tokens.to_string();
792 let attr = match condition {
793 SchemaFieldConfig::False => unreachable!(),
794 SchemaFieldConfig::True => {
795 parse_quote! { #[schemars(with = #attr_inner_tokens)] }
796 }
797 SchemaFieldConfig::Lazy(SchemaFieldCondition(condition)) => {
798 parse_quote! {
799 #[cfg_attr(
800 #condition,
801 schemars(with = #attr_inner_tokens))
802 ]
803 }
804 }
805 };
806
807 field.attrs.push(attr);
808 }
809 }
810 }
811 }
812 }
813 if let Some(type_) = &serde_as_options.deserialize_as {
814 emit_borrow_annotation(&serde_options, type_, field);
815 emit_default_annotation(&serde_as_options, &serde_options, type_, field);
816
817 let replacement_type = replace_infer_type_with_type(type_.clone(), type_same);
818 let attr_inner_tokens =
819 quote!(#serde_with_crate_path::As::<#replacement_type>::deserialize).to_string();
820 let attr = parse_quote!(#[serde(deserialize_with = #attr_inner_tokens)]);
821 field.attrs.push(attr);
822 }
823 if let Some(type_) = serde_as_options.serialize_as {
824 let replacement_type = replace_infer_type_with_type(type_.clone(), type_same);
825 let attr_inner_tokens =
826 quote!(#serde_with_crate_path::As::<#replacement_type>::serialize).to_string();
827 let attr = parse_quote!(#[serde(serialize_with = #attr_inner_tokens)]);
828 field.attrs.push(attr);
829 }
830
831 Ok(())
832}
833
834/// Recursively replace all occurrences of `_` with `replacement` in a [Type][]
835///
836/// The [`serde_as`][macro@serde_as] macro allows to use the infer type, i.e., `_`, as shortcut for
837/// `serde_with::As`. This function replaces all occurrences of the infer type with another type.
838fn replace_infer_type_with_type(to_replace: Type, replacement: &Type) -> Type {
839 match to_replace {
840 // Base case
841 // Replace the infer type with the actual replacement type
842 Type::Infer(_) => replacement.clone(),
843
844 // Recursive cases
845 // Iterate through all positions where a type could occur and recursively call this function
846 Type::Array(mut inner) => {
847 *inner.elem = replace_infer_type_with_type(*inner.elem, replacement);
848 Type::Array(inner)
849 }
850 Type::Group(mut inner) => {
851 *inner.elem = replace_infer_type_with_type(*inner.elem, replacement);
852 Type::Group(inner)
853 }
854 Type::Never(inner) => Type::Never(inner),
855 Type::Paren(mut inner) => {
856 *inner.elem = replace_infer_type_with_type(*inner.elem, replacement);
857 Type::Paren(inner)
858 }
859 Type::Path(mut inner) => {
860 if let Some(Pair::End(mut t)) | Some(Pair::Punctuated(mut t, _)) =
861 inner.path.segments.pop()
862 {
863 t.arguments = match t.arguments {
864 PathArguments::None => PathArguments::None,
865 PathArguments::AngleBracketed(mut inner) => {
866 // Iterate over the args between the angle brackets
867 inner.args = inner
868 .args
869 .into_iter()
870 .map(|generic_argument| match generic_argument {
871 // replace types within the generics list, but leave other stuff
872 // like lifetimes untouched
873 GenericArgument::Type(type_) => GenericArgument::Type(
874 replace_infer_type_with_type(type_, replacement),
875 ),
876 ga => ga,
877 })
878 .collect();
879 PathArguments::AngleBracketed(inner)
880 }
881 PathArguments::Parenthesized(mut inner) => {
882 inner.inputs = inner
883 .inputs
884 .into_iter()
885 .map(|type_| replace_infer_type_with_type(type_, replacement))
886 .collect();
887 inner.output = match inner.output {
888 ReturnType::Type(arrow, mut type_) => {
889 *type_ = replace_infer_type_with_type(*type_, replacement);
890 ReturnType::Type(arrow, type_)
891 }
892 default => default,
893 };
894 PathArguments::Parenthesized(inner)
895 }
896 };
897 inner.path.segments.push(t);
898 }
899 Type::Path(inner)
900 }
901 Type::Ptr(mut inner) => {
902 *inner.elem = replace_infer_type_with_type(*inner.elem, replacement);
903 Type::Ptr(inner)
904 }
905 Type::Reference(mut inner) => {
906 *inner.elem = replace_infer_type_with_type(*inner.elem, replacement);
907 Type::Reference(inner)
908 }
909 Type::Slice(mut inner) => {
910 *inner.elem = replace_infer_type_with_type(*inner.elem, replacement);
911 Type::Slice(inner)
912 }
913 Type::Tuple(mut inner) => {
914 inner.elems = inner
915 .elems
916 .into_pairs()
917 .map(|pair| match pair {
918 Pair::Punctuated(type_, p) => {
919 Pair::Punctuated(replace_infer_type_with_type(type_, replacement), p)
920 }
921 Pair::End(type_) => Pair::End(replace_infer_type_with_type(type_, replacement)),
922 })
923 .collect();
924 Type::Tuple(inner)
925 }
926
927 // Pass unknown types or non-handleable types (e.g., bare Fn) without performing any
928 // replacements
929 type_ => type_,
930 }
931}
932
933/// Check if a type ending in the `syn::Ident` `embedded_type` is contained in `type_`.
934fn has_type_embedded(type_: &Type, embedded_type: &syn::Ident) -> bool {
935 match type_ {
936 // Base cases
937 Type::Infer(_) => false,
938 Type::Never(_inner) => false,
939
940 // Recursive cases
941 // Iterate through all positions where a type could occur and recursively call this function
942 Type::Array(inner) => has_type_embedded(&inner.elem, embedded_type),
943 Type::Group(inner) => has_type_embedded(&inner.elem, embedded_type),
944 Type::Paren(inner) => has_type_embedded(&inner.elem, embedded_type),
945 Type::Path(inner) => {
946 match inner.path.segments.last() {
947 Some(t) => {
948 if t.ident == *embedded_type {
949 return true;
950 }
951
952 match &t.arguments {
953 PathArguments::None => false,
954 PathArguments::AngleBracketed(inner) => {
955 // Iterate over the args between the angle brackets
956 inner
957 .args
958 .iter()
959 .any(|generic_argument| match generic_argument {
960 // replace types within the generics list, but leave other stuff
961 // like lifetimes untouched
962 GenericArgument::Type(type_) => {
963 has_type_embedded(type_, embedded_type)
964 }
965 _ga => false,
966 })
967 }
968 PathArguments::Parenthesized(inner) => {
969 inner
970 .inputs
971 .iter()
972 .any(|type_| has_type_embedded(type_, embedded_type))
973 || match &inner.output {
974 ReturnType::Type(_arrow, type_) => {
975 has_type_embedded(type_, embedded_type)
976 }
977 _default => false,
978 }
979 }
980 }
981 }
982 None => false,
983 }
984 }
985 Type::Ptr(inner) => has_type_embedded(&inner.elem, embedded_type),
986 Type::Reference(inner) => has_type_embedded(&inner.elem, embedded_type),
987 Type::Slice(inner) => has_type_embedded(&inner.elem, embedded_type),
988 Type::Tuple(inner) => inner.elems.pairs().any(|pair| match pair {
989 Pair::Punctuated(type_, _) | Pair::End(type_) => {
990 has_type_embedded(type_, embedded_type)
991 }
992 }),
993
994 // Pass unknown types or non-handleable types (e.g., bare Fn) without performing any
995 // replacements
996 _type_ => false,
997 }
998}
999
1000/// Deserialize value by using its [`FromStr`] implementation
1001///
1002/// This is an alternative way to implement `Deserialize` for types, which also implement
1003/// [`FromStr`] by deserializing the type from string. Ensure that the struct/enum also implements
1004/// [`FromStr`]. If the implementation is missing, you will get an error message like
1005/// ```text
1006/// error[E0277]: the trait bound `Struct: std::str::FromStr` is not satisfied
1007/// ```
1008/// Additionally, `FromStr::Err` **must** implement [`Display`] as otherwise you will see a rather
1009/// unhelpful error message
1010///
1011/// Serialization with [`Display`] is available with the matching [`SerializeDisplay`] derive.
1012///
1013/// # Attributes
1014///
1015/// Attributes for the derive can be specified via the `#[serde_with(...)]` attribute on the struct
1016/// or enum. Currently, these arguments to the attribute are possible:
1017///
1018/// * **`#[serde_with(crate = "...")]`**: This allows using `DeserializeFromStr` when `serde_with`
1019/// is not available from the crate root. This happens while [renaming dependencies in
1020/// Cargo.toml][cargo-toml-rename] or when re-exporting the macro from a different crate.
1021///
1022/// This argument is analogue to [serde's crate argument][serde-crate] and the [crate argument
1023/// to `serde_as`][serde-as-crate].
1024///
1025/// # Example
1026///
1027/// ```rust,ignore
1028/// use std::str::FromStr;
1029///
1030/// #[derive(DeserializeFromStr)]
1031/// struct A {
1032/// a: u32,
1033/// b: bool,
1034/// }
1035///
1036/// impl FromStr for A {
1037/// type Err = String;
1038///
1039/// /// Parse a value like `123<>true`
1040/// fn from_str(s: &str) -> Result<Self, Self::Err> {
1041/// let mut parts = s.split("<>");
1042/// let number = parts
1043/// .next()
1044/// .ok_or_else(|| "Missing first value".to_string())?
1045/// .parse()
1046/// .map_err(|err: ParseIntError| err.to_string())?;
1047/// let bool = parts
1048/// .next()
1049/// .ok_or_else(|| "Missing second value".to_string())?
1050/// .parse()
1051/// .map_err(|err: ParseBoolError| err.to_string())?;
1052/// Ok(Self { a: number, b: bool })
1053/// }
1054/// }
1055///
1056/// let a: A = serde_json::from_str("\"159<>true\"").unwrap();
1057/// assert_eq!(A { a: 159, b: true }, a);
1058/// ```
1059///
1060/// [`Display`]: std::fmt::Display
1061/// [`FromStr`]: std::str::FromStr
1062/// [cargo-toml-rename]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#renaming-dependencies-in-cargotoml
1063/// [serde-as-crate]: https://docs.rs/serde_with/3.14.0/serde_with/guide/serde_as/index.html#re-exporting-serde_as
1064/// [serde-crate]: https://serde.rs/container-attrs.html#crate
1065#[proc_macro_derive(DeserializeFromStr, attributes(serde_with))]
1066pub fn derive_deserialize_fromstr(item: TokenStream) -> TokenStream {
1067 let input: DeriveInput = parse_macro_input!(item);
1068 let derive_options = match DeriveOptions::from_derive_input(&input) {
1069 Ok(opt) => opt,
1070 Err(err) => {
1071 return err;
1072 }
1073 };
1074 TokenStream::from(deserialize_fromstr(
1075 input,
1076 derive_options.get_serde_with_path(),
1077 ))
1078}
1079
1080fn deserialize_fromstr(mut input: DeriveInput, serde_with_crate_path: Path) -> TokenStream2 {
1081 let ident = input.ident;
1082 let where_clause = &mut input.generics.make_where_clause().predicates;
1083 where_clause.push(parse_quote!(Self: #serde_with_crate_path::__private__::FromStr));
1084 where_clause.push(parse_quote!(
1085 <Self as #serde_with_crate_path::__private__::FromStr>::Err: #serde_with_crate_path::__private__::Display
1086 ));
1087 let (de_impl_generics, ty_generics, where_clause) = split_with_de_lifetime(&input.generics);
1088 quote! {
1089 #[automatically_derived]
1090 impl #de_impl_generics #serde_with_crate_path::serde::Deserialize<'de> for #ident #ty_generics #where_clause {
1091 fn deserialize<__D>(deserializer: __D) -> #serde_with_crate_path::__private__::Result<Self, __D::Error>
1092 where
1093 __D: #serde_with_crate_path::serde::Deserializer<'de>,
1094 {
1095 struct Helper<__S>(#serde_with_crate_path::__private__::PhantomData<__S>);
1096
1097 impl<'de, __S> #serde_with_crate_path::serde::de::Visitor<'de> for Helper<__S>
1098 where
1099 __S: #serde_with_crate_path::__private__::FromStr,
1100 <__S as #serde_with_crate_path::__private__::FromStr>::Err: #serde_with_crate_path::__private__::Display,
1101 {
1102 type Value = __S;
1103
1104 fn expecting(&self, formatter: &mut #serde_with_crate_path::core::fmt::Formatter<'_>) -> #serde_with_crate_path::core::fmt::Result {
1105 #serde_with_crate_path::__private__::Display::fmt("a string", formatter)
1106 }
1107
1108 fn visit_str<__E>(
1109 self,
1110 value: &str
1111 ) -> #serde_with_crate_path::__private__::Result<Self::Value, __E>
1112 where
1113 __E: #serde_with_crate_path::serde::de::Error,
1114 {
1115 value.parse::<Self::Value>().map_err(#serde_with_crate_path::serde::de::Error::custom)
1116 }
1117
1118 fn visit_bytes<__E>(
1119 self,
1120 value: &[u8]
1121 ) -> #serde_with_crate_path::__private__::Result<Self::Value, __E>
1122 where
1123 __E: #serde_with_crate_path::serde::de::Error,
1124 {
1125 let utf8 = #serde_with_crate_path::core::str::from_utf8(value).map_err(#serde_with_crate_path::serde::de::Error::custom)?;
1126 self.visit_str(utf8)
1127 }
1128 }
1129
1130 deserializer.deserialize_str(Helper(#serde_with_crate_path::__private__::PhantomData))
1131 }
1132 }
1133 }
1134}
1135
1136/// Serialize value by using it's [`Display`] implementation
1137///
1138/// This is an alternative way to implement `Serialize` for types, which also implement [`Display`]
1139/// by serializing the type as string. Ensure that the struct/enum also implements [`Display`].
1140/// If the implementation is missing, you will get an error message like
1141/// ```text
1142/// error[E0277]: `Struct` doesn't implement `std::fmt::Display`
1143/// ```
1144///
1145/// Deserialization with [`FromStr`] is available with the matching [`DeserializeFromStr`] derive.
1146///
1147/// # Attributes
1148///
1149/// Attributes for the derive can be specified via the `#[serde_with(...)]` attribute on the struct
1150/// or enum. Currently, these arguments to the attribute are possible:
1151///
1152/// * **`#[serde_with(crate = "...")]`**: This allows using `SerializeDisplay` when `serde_with` is
1153/// not available from the crate root. This happens while [renaming dependencies in
1154/// Cargo.toml][cargo-toml-rename] or when re-exporting the macro from a different crate.
1155///
1156/// This argument is analogue to [serde's crate argument][serde-crate] and the [crate argument
1157/// to `serde_as`][serde-as-crate].
1158///
1159/// # Example
1160///
1161/// ```rust,ignore
1162/// use std::fmt;
1163///
1164/// #[derive(SerializeDisplay)]
1165/// struct A {
1166/// a: u32,
1167/// b: bool,
1168/// }
1169///
1170/// impl fmt::Display for A {
1171/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1172/// write!(f, "{}<>{}", self.a, self.b)
1173/// }
1174/// }
1175///
1176/// let a = A { a: 123, b: false };
1177/// assert_eq!(r#""123<>false""#, serde_json::to_string(&a).unwrap());
1178/// ```
1179///
1180/// [`Display`]: std::fmt::Display
1181/// [`FromStr`]: std::str::FromStr
1182/// [cargo-toml-rename]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#renaming-dependencies-in-cargotoml
1183/// [serde-as-crate]: https://docs.rs/serde_with/3.14.0/serde_with/guide/serde_as/index.html#re-exporting-serde_as
1184/// [serde-crate]: https://serde.rs/container-attrs.html#crate
1185#[proc_macro_derive(SerializeDisplay, attributes(serde_with))]
1186pub fn derive_serialize_display(item: TokenStream) -> TokenStream {
1187 let input: DeriveInput = parse_macro_input!(item);
1188 let derive_options = match DeriveOptions::from_derive_input(&input) {
1189 Ok(opt) => opt,
1190 Err(err) => {
1191 return err;
1192 }
1193 };
1194 TokenStream::from(serialize_display(
1195 input,
1196 false,
1197 derive_options.get_serde_with_path(),
1198 ))
1199}
1200
1201/// Serialize value by using its [`Display`] implementation with the “alternate” (`#`) format flag
1202///
1203/// This derive implements `serde::Serialize` for any type that already implements
1204/// [`std::fmt::Display`], emitting its string form using the alternate formatting specifier
1205/// (`{:#}`) instead of the normal `{}`. In other words, rather than calling
1206/// `format!("{}", self)`, it calls `format!("{:#}", self)`.
1207///
1208/// Ensure that your type implements [`Display`], or you will get a compile‐error such as:
1209/// ```text
1210/// error[E0277]: `MyType` doesn't implement `std::fmt::Display`
1211/// ```
1212///
1213/// Deserialization from strings via [`std::str::FromStr`] is handled by the companion
1214/// [`DeserializeFromStr`] derive.
1215///
1216/// # Attributes
1217///
1218/// You may customize which `serde_with` crate is used (for renamed or re-exported crates)
1219/// via the same attribute namespace:
1220///
1221/// * `#[serde_with(crate = "...")]`
1222/// When your workspace renames or re-exports `serde_with`, use this to point at the correct path.
1223/// For example:
1224/// ```rust,ignore
1225/// #[derive(SerializeDisplayAlt)]
1226/// #[serde_with(crate = "my_forked_serde_with")]
1227/// pub struct Foo(/* … */);
1228/// ```
1229///
1230/// # Example
1231///
1232/// ```rust,ignore
1233/// use std::fmt;
1234/// use serde_with::{SerializeDisplayAlt, DeserializeFromStr};
1235///
1236/// #[derive(Debug, Clone, SerializeDisplayAlt, DeserializeFromStr)]
1237/// #[serde(transparent)]
1238/// pub struct MyType(u32);
1239///
1240/// impl fmt::Display for MyType {
1241/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1242/// if f.alternate() {
1243/// // Alternate formatting: hex with 0x prefix
1244/// write!(f, "0x{:X}", self.0)
1245/// } else {
1246/// // Standard formatting: decimal
1247/// write!(f, "{}", self.0)
1248/// }
1249/// }
1250/// }
1251///
1252/// let v = MyType(15);
1253/// // SerializeDisplayAlt always uses `{:#}`, so this yields `"0xF"`
1254/// assert_eq!(r#""0xF""#, serde_json::to_string(&v).unwrap());
1255/// ```
1256///
1257/// [`Display`]: std::fmt::Display
1258/// [`FromStr`]: std::str::FromStr
1259/// [`DeserializeFromStr`]: crate::DeserializeFromStr
1260#[proc_macro_derive(SerializeDisplayAlt, attributes(serde_with))]
1261pub fn derive_serialize_display_alt(item: TokenStream) -> TokenStream {
1262 let input: DeriveInput = parse_macro_input!(item);
1263 let derive_options = match DeriveOptions::from_derive_input(&input) {
1264 Ok(opt) => opt,
1265 Err(err) => {
1266 return err;
1267 }
1268 };
1269 TokenStream::from(serialize_display(
1270 input,
1271 true,
1272 derive_options.get_serde_with_path(),
1273 ))
1274}
1275
1276fn serialize_display(
1277 mut input: DeriveInput,
1278 alternate: bool,
1279 serde_with_crate_path: Path,
1280) -> TokenStream2 {
1281 let ident = input.ident;
1282 input
1283 .generics
1284 .make_where_clause()
1285 .predicates
1286 .push(parse_quote!(Self: #serde_with_crate_path::__private__::Display));
1287 let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
1288
1289 let collect_str_param = if alternate {
1290 quote! { &format_args!("{self:#}") }
1291 } else {
1292 quote! { &self }
1293 };
1294
1295 quote! {
1296 #[automatically_derived]
1297 impl #impl_generics #serde_with_crate_path::serde::Serialize for #ident #ty_generics #where_clause {
1298 fn serialize<__S>(
1299 &self,
1300 serializer: __S
1301 ) -> #serde_with_crate_path::__private__::Result<__S::Ok, __S::Error>
1302 where
1303 __S: #serde_with_crate_path::serde::Serializer,
1304 {
1305 serializer.collect_str(#collect_str_param)
1306 }
1307 }
1308 }
1309}
1310
1311#[doc(hidden)]
1312/// Private function. Not part of the public API
1313///
1314/// The only task of this derive macro is to consume any `serde_as` attributes and turn them into
1315/// inert attributes. This allows the serde_as macro to keep the field attributes without causing
1316/// compiler errors. The intend is that keeping the field attributes allows downstream crates to
1317/// consume and act on them without causing an ordering dependency to the serde_as macro.
1318///
1319/// Otherwise, downstream proc-macros would need to be placed *in front of* the main `#[serde_as]`
1320/// attribute, since otherwise the field attributes would already be stripped off.
1321///
1322/// More details about the use-cases in the GitHub discussion: <https://github.com/jonasbb/serde_with/discussions/260>.
1323#[proc_macro_derive(
1324 __private_consume_serde_as_attributes,
1325 attributes(serde_as, serde_with)
1326)]
1327pub fn __private_consume_serde_as_attributes(_: TokenStream) -> TokenStream {
1328 TokenStream::new()
1329}
1330
1331/// Apply attributes to all fields with matching types
1332///
1333/// Whenever you experience the need to apply the same attributes to multiple fields, you can use
1334/// this macro. It allows you to specify a list of types and a list of attributes.
1335/// Each field with a "matching" type will then get the attributes applied.
1336/// The `apply` attribute must be placed *before* any consuming attributes, such as `derive` or
1337/// `serde_as`, because Rust expands all attributes in order.
1338///
1339/// For example, if your struct or enum contains many `Option<T>` fields, but you do not want to
1340/// serialize `None` values, you can use this macro to apply the `#[serde(skip_serializing_if =
1341/// "Option::is_none")]` attribute to all fields of type `Option<T>`.
1342///
1343/// ```rust
1344/// # use serde_with_macros as serde_with;
1345/// #[serde_with::apply(
1346/// # crate="serde_with",
1347/// Option => #[serde(skip_serializing_if = "Option::is_none")],
1348/// )]
1349/// #[derive(serde::Serialize)]
1350/// # #[derive(Default)]
1351/// struct Data {
1352/// a: Option<String>,
1353/// b: Option<u64>,
1354/// c: Option<String>,
1355/// d: Option<bool>,
1356/// }
1357/// #
1358/// # assert_eq!("{}", serde_json::to_string(&Data::default()).unwrap());
1359/// ```
1360///
1361/// Each rule starts with a type pattern, specifying which fields to match and a list of attributes
1362/// to apply. Multiple rules can be provided in a single `apply` attribute.
1363///
1364/// ```rust
1365/// # use serde_with_macros as serde_with;
1366/// #[serde_with::apply(
1367/// # crate="serde_with",
1368/// Option => #[serde(default)] #[serde(skip_serializing_if = "Option::is_none")],
1369/// Option<bool> => #[serde(rename = "bool")],
1370/// )]
1371/// # #[derive(serde::Serialize)]
1372/// # #[derive(Default)]
1373/// # struct Data {
1374/// # a: Option<String>,
1375/// # b: Option<u64>,
1376/// # c: Option<String>,
1377/// # d: Option<bool>,
1378/// # }
1379/// #
1380/// # assert_eq!("{}", serde_json::to_string(&Data::default()).unwrap());
1381/// ```
1382///
1383/// ## Type Patterns
1384///
1385/// The type pattern left of the `=>` specifies which fields to match.
1386///
1387/// | Type Pattern | Matching Types | Notes |
1388/// | :---------------------- | ---------------------------------------------------: | :------------------------------------------------------------------------------ |
1389/// | `_` | `Option<bool>`<br>`BTreeMap<&'static str, Vec<u32>>` | `_` matches all fields. |
1390/// | `Option` | `Option<bool>`<br>`Option<String>` | A missing generic is compatible with any generic arguments. |
1391/// | `Option<bool>` | `Option<bool>` | A fully specified type only matches exactly. |
1392/// | `BTreeMap<String, u32>` | `BTreeMap<String, u32>` | A fully specified type only matches exactly. |
1393/// | `BTreeMap<String, _>` | `BTreeMap<String, u32>`<br>`BTreeMap<String, bool>` | Any `String` key `BTreeMap` matches, as the value is using the `_` placeholder. |
1394/// | `[u8; _]` | `[u8; 1]`<br>`[u8; N]` | `_` also works as a placeholder for any array length. |
1395///
1396/// ## Opt-out for Individual Fields
1397///
1398/// The `apply` attribute will find all fields with a compatible type.
1399/// This can be overly eager and a different set of attributes might be required for a specific
1400/// field. You can opt-out of the `apply` attribute by adding the `#[serde_with(skip_apply)]`
1401/// attribute to the field. This will prevent any `apply` to apply to this field.
1402/// If two rules apply to the same field, it is impossible to opt-out of only a single one.
1403/// In this case the attributes must be applied to the field manually.
1404///
1405/// ```rust
1406/// # use serde_json::json;
1407/// # use serde_with_macros as serde_with;
1408/// #[serde_with::apply(
1409/// # crate="serde_with",
1410/// Option => #[serde(skip_serializing_if = "Option::is_none")],
1411/// )]
1412/// #[derive(serde::Serialize)]
1413/// struct Data {
1414/// a: Option<String>,
1415/// #[serde_with(skip_apply)]
1416/// always_serialize_this_field: Option<u64>,
1417/// c: Option<String>,
1418/// d: Option<bool>,
1419/// }
1420///
1421/// let data = Data {
1422/// a: None,
1423/// always_serialize_this_field: None,
1424/// c: None,
1425/// d: None,
1426/// };
1427///
1428/// // serializes into this JSON:
1429/// # assert_eq!(json!(
1430/// {
1431/// "always_serialize_this_field": null
1432/// }
1433/// # ), serde_json::to_value(data).unwrap());
1434/// ```
1435///
1436/// # Alternative path to `serde_with` crate
1437///
1438/// If `serde_with` is not available at the default path, its path should be specified with the
1439/// `crate` argument. See [re-exporting `serde_as`] for more use case information.
1440///
1441/// ```rust,ignore
1442/// #[serde_with::apply(
1443/// crate = "::some_other_lib::serde_with"
1444/// Option => #[serde(skip_serializing_if = "Option::is_none")],
1445/// )]
1446/// #[derive(serde::Serialize)]
1447/// struct Data {
1448/// a: Option<String>,
1449/// b: Option<u64>,
1450/// c: Option<String>,
1451/// d: Option<bool>,
1452/// }
1453/// ```
1454#[proc_macro_attribute]
1455pub fn apply(args: TokenStream, input: TokenStream) -> TokenStream {
1456 apply::apply(args, input)
1457}