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.13.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` or `schemars_0_9` 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.13.0/serde_with/guide/index.html
593/// [re-exporting `serde_as`]: https://docs.rs/serde_with/3.13.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(feature = "schemars_0_8", feature = "schemars_0_9"))) => {
619                    SchemaFieldConfig::False
620                }
621                Some(condition) => condition.into(),
622                None => utils::has_derive_jsonschema(input.clone()).unwrap_or_default(),
623            };
624
625            // Convert any error message into a nice compiler error
626            let res = apply_function_to_struct_and_enum_fields_darling(
627                input,
628                &serde_with_crate_path,
629                |field| serde_as_add_attr_to_field(field, &serde_with_crate_path, &schemars_config),
630            )
631            .unwrap_or_else(darling::Error::write_errors);
632            TokenStream::from(res)
633        }
634        Err(e) => TokenStream::from(DarlingError::from(e).write_errors()),
635    }
636}
637
638/// Inspect the field and convert the `serde_as` attribute into the classical `serde`
639fn serde_as_add_attr_to_field(
640    field: &mut Field,
641    serde_with_crate_path: &Path,
642    schemars_config: &SchemaFieldConfig,
643) -> Result<(), DarlingError> {
644    #[derive(FromField)]
645    #[darling(attributes(serde_as))]
646    struct SerdeAsOptions {
647        /// The original type of the field
648        ty: Type,
649
650        r#as: Option<Type>,
651        deserialize_as: Option<Type>,
652        serialize_as: Option<Type>,
653        no_default: Flag,
654    }
655
656    impl SerdeAsOptions {
657        fn has_any_set(&self) -> bool {
658            self.r#as.is_some() || self.deserialize_as.is_some() || self.serialize_as.is_some()
659        }
660    }
661
662    #[derive(FromField)]
663    #[darling(attributes(serde), allow_unknown_fields)]
664    struct SerdeOptions {
665        with: Option<String>,
666        deserialize_with: Option<String>,
667        serialize_with: Option<String>,
668
669        borrow: Option<Override<String>>,
670        default: Option<Override<String>>,
671    }
672
673    impl SerdeOptions {
674        fn has_any_set(&self) -> bool {
675            self.with.is_some() || self.deserialize_with.is_some() || self.serialize_with.is_some()
676        }
677    }
678
679    /// Emit a `borrow` annotation, if the replacement type requires borrowing.
680    fn emit_borrow_annotation(serde_options: &SerdeOptions, as_type: &Type, field: &mut Field) {
681        let type_borrowcow = &syn::parse_quote!(BorrowCow);
682        // If the field is not borrowed yet, check if we need to borrow it.
683        if serde_options.borrow.is_none() && has_type_embedded(as_type, type_borrowcow) {
684            let attr_borrow = parse_quote!(#[serde(borrow)]);
685            field.attrs.push(attr_borrow);
686        }
687    }
688
689    /// Emit a `default` annotation, if `as_type` and `field` are both `Option`.
690    fn emit_default_annotation(
691        serde_as_options: &SerdeAsOptions,
692        serde_options: &SerdeOptions,
693        as_type: &Type,
694        field: &mut Field,
695    ) {
696        if !serde_as_options.no_default.is_present()
697            && serde_options.default.is_none()
698            && is_std_option(as_type)
699            && is_std_option(&field.ty)
700        {
701            let attr_borrow = parse_quote!(#[serde(default)]);
702            field.attrs.push(attr_borrow);
703        }
704    }
705
706    // syn v2 no longer supports keywords in the path position of an attribute.
707    // That breaks #[serde_as(as = "FooBar")], since `as` is a keyword.
708    // For each attribute, that is named `serde_as`, we replace the `as` keyword with `r#as`.
709    let mut has_serde_as = false;
710    field.attrs.iter_mut().for_each(|attr| {
711        if attr.path().is_ident("serde_as") {
712            // We found a `serde_as` attribute.
713            // Remember that such that we can quick exit otherwise
714            has_serde_as = true;
715
716            if let Meta::List(metalist) = &mut attr.meta {
717                metalist.tokens = std::mem::take(&mut metalist.tokens)
718                    .into_iter()
719                    .map(|token| {
720                        use proc_macro2::{Ident, TokenTree};
721
722                        // Replace `as` with `r#as`.
723                        match token {
724                            TokenTree::Ident(ident) if ident == "as" => {
725                                TokenTree::Ident(Ident::new_raw("as", ident.span()))
726                            }
727                            _ => token,
728                        }
729                    })
730                    .collect();
731            }
732        }
733    });
734    // If there is no `serde_as` attribute, we can exit early.
735    if !has_serde_as {
736        return Ok(());
737    }
738    let serde_as_options = SerdeAsOptions::from_field(field)?;
739    let serde_options = SerdeOptions::from_field(field)?;
740
741    let mut errors = Vec::new();
742    if !serde_as_options.has_any_set() {
743        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."));
744    }
745
746    // Check if there are any conflicting attributes
747    if serde_as_options.has_any_set() && serde_options.has_any_set() {
748        errors.push(DarlingError::custom("Cannot combine `serde_as` with serde's `with`, `deserialize_with`, or `serialize_with`."));
749    }
750
751    if serde_as_options.r#as.is_some() && serde_as_options.deserialize_as.is_some() {
752        errors.push(DarlingError::custom("Cannot combine `as` with `deserialize_as`. Use `serialize_as` to specify different serialization code."));
753    } else if serde_as_options.r#as.is_some() && serde_as_options.serialize_as.is_some() {
754        errors.push(DarlingError::custom("Cannot combine `as` with `serialize_as`. Use `deserialize_as` to specify different deserialization code."));
755    }
756
757    if !errors.is_empty() {
758        return Err(DarlingError::multiple(errors));
759    }
760
761    let type_original = &serde_as_options.ty;
762    let type_same = &syn::parse_quote!(#serde_with_crate_path::Same);
763    if let Some(type_) = &serde_as_options.r#as {
764        emit_borrow_annotation(&serde_options, type_, field);
765        emit_default_annotation(&serde_as_options, &serde_options, type_, field);
766
767        let replacement_type = replace_infer_type_with_type(type_.clone(), type_same);
768        let attr_inner_tokens = quote!(#serde_with_crate_path::As::<#replacement_type>).to_string();
769        let attr = parse_quote!(#[serde(with = #attr_inner_tokens)]);
770        field.attrs.push(attr);
771
772        match schemars_config {
773            SchemaFieldConfig::False => {}
774            lhs => {
775                let rhs = utils::schemars_with_attr_if(
776                    &field.attrs,
777                    &["with", "serialize_with", "deserialize_with", "schema_with"],
778                )?;
779
780                match lhs & !rhs {
781                    SchemaFieldConfig::False => {}
782                    condition => {
783                        let attr_inner_tokens = quote! {
784                            #serde_with_crate_path::Schema::<#type_original, #replacement_type>
785                        };
786                        let attr_inner_tokens = attr_inner_tokens.to_string();
787                        let attr = match condition {
788                            SchemaFieldConfig::False => unreachable!(),
789                            SchemaFieldConfig::True => {
790                                parse_quote! { #[schemars(with = #attr_inner_tokens)] }
791                            }
792                            SchemaFieldConfig::Lazy(SchemaFieldCondition(condition)) => {
793                                parse_quote! {
794                                    #[cfg_attr(
795                                        #condition,
796                                        schemars(with = #attr_inner_tokens))
797                                    ]
798                                }
799                            }
800                        };
801
802                        field.attrs.push(attr);
803                    }
804                }
805            }
806        }
807    }
808    if let Some(type_) = &serde_as_options.deserialize_as {
809        emit_borrow_annotation(&serde_options, type_, field);
810        emit_default_annotation(&serde_as_options, &serde_options, type_, field);
811
812        let replacement_type = replace_infer_type_with_type(type_.clone(), type_same);
813        let attr_inner_tokens =
814            quote!(#serde_with_crate_path::As::<#replacement_type>::deserialize).to_string();
815        let attr = parse_quote!(#[serde(deserialize_with = #attr_inner_tokens)]);
816        field.attrs.push(attr);
817    }
818    if let Some(type_) = serde_as_options.serialize_as {
819        let replacement_type = replace_infer_type_with_type(type_.clone(), type_same);
820        let attr_inner_tokens =
821            quote!(#serde_with_crate_path::As::<#replacement_type>::serialize).to_string();
822        let attr = parse_quote!(#[serde(serialize_with = #attr_inner_tokens)]);
823        field.attrs.push(attr);
824    }
825
826    Ok(())
827}
828
829/// Recursively replace all occurrences of `_` with `replacement` in a [Type][]
830///
831/// The [`serde_as`][macro@serde_as] macro allows to use the infer type, i.e., `_`, as shortcut for
832/// `serde_with::As`. This function replaces all occurrences of the infer type with another type.
833fn replace_infer_type_with_type(to_replace: Type, replacement: &Type) -> Type {
834    match to_replace {
835        // Base case
836        // Replace the infer type with the actual replacement type
837        Type::Infer(_) => replacement.clone(),
838
839        // Recursive cases
840        // Iterate through all positions where a type could occur and recursively call this function
841        Type::Array(mut inner) => {
842            *inner.elem = replace_infer_type_with_type(*inner.elem, replacement);
843            Type::Array(inner)
844        }
845        Type::Group(mut inner) => {
846            *inner.elem = replace_infer_type_with_type(*inner.elem, replacement);
847            Type::Group(inner)
848        }
849        Type::Never(inner) => Type::Never(inner),
850        Type::Paren(mut inner) => {
851            *inner.elem = replace_infer_type_with_type(*inner.elem, replacement);
852            Type::Paren(inner)
853        }
854        Type::Path(mut inner) => {
855            if let Some(Pair::End(mut t)) | Some(Pair::Punctuated(mut t, _)) =
856                inner.path.segments.pop()
857            {
858                t.arguments = match t.arguments {
859                    PathArguments::None => PathArguments::None,
860                    PathArguments::AngleBracketed(mut inner) => {
861                        // Iterate over the args between the angle brackets
862                        inner.args = inner
863                            .args
864                            .into_iter()
865                            .map(|generic_argument| match generic_argument {
866                                // replace types within the generics list, but leave other stuff
867                                // like lifetimes untouched
868                                GenericArgument::Type(type_) => GenericArgument::Type(
869                                    replace_infer_type_with_type(type_, replacement),
870                                ),
871                                ga => ga,
872                            })
873                            .collect();
874                        PathArguments::AngleBracketed(inner)
875                    }
876                    PathArguments::Parenthesized(mut inner) => {
877                        inner.inputs = inner
878                            .inputs
879                            .into_iter()
880                            .map(|type_| replace_infer_type_with_type(type_, replacement))
881                            .collect();
882                        inner.output = match inner.output {
883                            ReturnType::Type(arrow, mut type_) => {
884                                *type_ = replace_infer_type_with_type(*type_, replacement);
885                                ReturnType::Type(arrow, type_)
886                            }
887                            default => default,
888                        };
889                        PathArguments::Parenthesized(inner)
890                    }
891                };
892                inner.path.segments.push(t);
893            }
894            Type::Path(inner)
895        }
896        Type::Ptr(mut inner) => {
897            *inner.elem = replace_infer_type_with_type(*inner.elem, replacement);
898            Type::Ptr(inner)
899        }
900        Type::Reference(mut inner) => {
901            *inner.elem = replace_infer_type_with_type(*inner.elem, replacement);
902            Type::Reference(inner)
903        }
904        Type::Slice(mut inner) => {
905            *inner.elem = replace_infer_type_with_type(*inner.elem, replacement);
906            Type::Slice(inner)
907        }
908        Type::Tuple(mut inner) => {
909            inner.elems = inner
910                .elems
911                .into_pairs()
912                .map(|pair| match pair {
913                    Pair::Punctuated(type_, p) => {
914                        Pair::Punctuated(replace_infer_type_with_type(type_, replacement), p)
915                    }
916                    Pair::End(type_) => Pair::End(replace_infer_type_with_type(type_, replacement)),
917                })
918                .collect();
919            Type::Tuple(inner)
920        }
921
922        // Pass unknown types or non-handleable types (e.g., bare Fn) without performing any
923        // replacements
924        type_ => type_,
925    }
926}
927
928/// Check if a type ending in the `syn::Ident` `embedded_type` is contained in `type_`.
929fn has_type_embedded(type_: &Type, embedded_type: &syn::Ident) -> bool {
930    match type_ {
931        // Base cases
932        Type::Infer(_) => false,
933        Type::Never(_inner) => false,
934
935        // Recursive cases
936        // Iterate through all positions where a type could occur and recursively call this function
937        Type::Array(inner) => has_type_embedded(&inner.elem, embedded_type),
938        Type::Group(inner) => has_type_embedded(&inner.elem, embedded_type),
939        Type::Paren(inner) => has_type_embedded(&inner.elem, embedded_type),
940        Type::Path(inner) => {
941            match inner.path.segments.last() {
942                Some(t) => {
943                    if t.ident == *embedded_type {
944                        return true;
945                    }
946
947                    match &t.arguments {
948                        PathArguments::None => false,
949                        PathArguments::AngleBracketed(inner) => {
950                            // Iterate over the args between the angle brackets
951                            inner
952                                .args
953                                .iter()
954                                .any(|generic_argument| match generic_argument {
955                                    // replace types within the generics list, but leave other stuff
956                                    // like lifetimes untouched
957                                    GenericArgument::Type(type_) => {
958                                        has_type_embedded(type_, embedded_type)
959                                    }
960                                    _ga => false,
961                                })
962                        }
963                        PathArguments::Parenthesized(inner) => {
964                            inner
965                                .inputs
966                                .iter()
967                                .any(|type_| has_type_embedded(type_, embedded_type))
968                                || match &inner.output {
969                                    ReturnType::Type(_arrow, type_) => {
970                                        has_type_embedded(type_, embedded_type)
971                                    }
972                                    _default => false,
973                                }
974                        }
975                    }
976                }
977                None => false,
978            }
979        }
980        Type::Ptr(inner) => has_type_embedded(&inner.elem, embedded_type),
981        Type::Reference(inner) => has_type_embedded(&inner.elem, embedded_type),
982        Type::Slice(inner) => has_type_embedded(&inner.elem, embedded_type),
983        Type::Tuple(inner) => inner.elems.pairs().any(|pair| match pair {
984            Pair::Punctuated(type_, _) | Pair::End(type_) => {
985                has_type_embedded(type_, embedded_type)
986            }
987        }),
988
989        // Pass unknown types or non-handleable types (e.g., bare Fn) without performing any
990        // replacements
991        _type_ => false,
992    }
993}
994
995/// Deserialize value by using its [`FromStr`] implementation
996///
997/// This is an alternative way to implement `Deserialize` for types, which also implement
998/// [`FromStr`] by deserializing the type from string. Ensure that the struct/enum also implements
999/// [`FromStr`]. If the implementation is missing, you will get an error message like
1000/// ```text
1001/// error[E0277]: the trait bound `Struct: std::str::FromStr` is not satisfied
1002/// ```
1003/// Additionally, `FromStr::Err` **must** implement [`Display`] as otherwise you will see a rather
1004/// unhelpful error message
1005///
1006/// Serialization with [`Display`] is available with the matching [`SerializeDisplay`] derive.
1007///
1008/// # Attributes
1009///
1010/// Attributes for the derive can be specified via the `#[serde_with(...)]` attribute on the struct
1011/// or enum. Currently, these arguments to the attribute are possible:
1012///
1013/// * **`#[serde_with(crate = "...")]`**: This allows using `DeserializeFromStr` when `serde_with`
1014///   is not available from the crate root. This happens while [renaming dependencies in
1015///   Cargo.toml][cargo-toml-rename] or when re-exporting the macro from a different crate.
1016///
1017///   This argument is analogue to [serde's crate argument][serde-crate] and the [crate argument
1018///   to `serde_as`][serde-as-crate].
1019///
1020/// # Example
1021///
1022/// ```rust,ignore
1023/// use std::str::FromStr;
1024///
1025/// #[derive(DeserializeFromStr)]
1026/// struct A {
1027///     a: u32,
1028///     b: bool,
1029/// }
1030///
1031/// impl FromStr for A {
1032///     type Err = String;
1033///
1034///     /// Parse a value like `123<>true`
1035///     fn from_str(s: &str) -> Result<Self, Self::Err> {
1036///         let mut parts = s.split("<>");
1037///         let number = parts
1038///             .next()
1039///             .ok_or_else(|| "Missing first value".to_string())?
1040///             .parse()
1041///             .map_err(|err: ParseIntError| err.to_string())?;
1042///         let bool = parts
1043///             .next()
1044///             .ok_or_else(|| "Missing second value".to_string())?
1045///             .parse()
1046///             .map_err(|err: ParseBoolError| err.to_string())?;
1047///         Ok(Self { a: number, b: bool })
1048///     }
1049/// }
1050///
1051/// let a: A = serde_json::from_str("\"159<>true\"").unwrap();
1052/// assert_eq!(A { a: 159, b: true }, a);
1053/// ```
1054///
1055/// [`Display`]: std::fmt::Display
1056/// [`FromStr`]: std::str::FromStr
1057/// [cargo-toml-rename]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#renaming-dependencies-in-cargotoml
1058/// [serde-as-crate]: https://docs.rs/serde_with/3.13.0/serde_with/guide/serde_as/index.html#re-exporting-serde_as
1059/// [serde-crate]: https://serde.rs/container-attrs.html#crate
1060#[proc_macro_derive(DeserializeFromStr, attributes(serde_with))]
1061pub fn derive_deserialize_fromstr(item: TokenStream) -> TokenStream {
1062    let input: DeriveInput = parse_macro_input!(item);
1063    let derive_options = match DeriveOptions::from_derive_input(&input) {
1064        Ok(opt) => opt,
1065        Err(err) => {
1066            return err;
1067        }
1068    };
1069    TokenStream::from(deserialize_fromstr(
1070        input,
1071        derive_options.get_serde_with_path(),
1072    ))
1073}
1074
1075fn deserialize_fromstr(mut input: DeriveInput, serde_with_crate_path: Path) -> TokenStream2 {
1076    let ident = input.ident;
1077    let where_clause = &mut input.generics.make_where_clause().predicates;
1078    where_clause.push(parse_quote!(Self: #serde_with_crate_path::__private__::FromStr));
1079    where_clause.push(parse_quote!(
1080        <Self as #serde_with_crate_path::__private__::FromStr>::Err: #serde_with_crate_path::__private__::Display
1081    ));
1082    let (de_impl_generics, ty_generics, where_clause) = split_with_de_lifetime(&input.generics);
1083    quote! {
1084        #[automatically_derived]
1085        impl #de_impl_generics #serde_with_crate_path::serde::Deserialize<'de> for #ident #ty_generics #where_clause {
1086            fn deserialize<__D>(deserializer: __D) -> #serde_with_crate_path::__private__::Result<Self, __D::Error>
1087            where
1088                __D: #serde_with_crate_path::serde::Deserializer<'de>,
1089            {
1090                struct Helper<__S>(#serde_with_crate_path::__private__::PhantomData<__S>);
1091
1092                impl<'de, __S> #serde_with_crate_path::serde::de::Visitor<'de> for Helper<__S>
1093                where
1094                    __S: #serde_with_crate_path::__private__::FromStr,
1095                    <__S as #serde_with_crate_path::__private__::FromStr>::Err: #serde_with_crate_path::__private__::Display,
1096                {
1097                    type Value = __S;
1098
1099                    fn expecting(&self, formatter: &mut #serde_with_crate_path::core::fmt::Formatter<'_>) -> #serde_with_crate_path::core::fmt::Result {
1100                        #serde_with_crate_path::__private__::Display::fmt("a string", formatter)
1101                    }
1102
1103                    fn visit_str<__E>(
1104                        self,
1105                        value: &str
1106                    ) -> #serde_with_crate_path::__private__::Result<Self::Value, __E>
1107                    where
1108                        __E: #serde_with_crate_path::serde::de::Error,
1109                    {
1110                        value.parse::<Self::Value>().map_err(#serde_with_crate_path::serde::de::Error::custom)
1111                    }
1112
1113                    fn visit_bytes<__E>(
1114                        self,
1115                        value: &[u8]
1116                    ) -> #serde_with_crate_path::__private__::Result<Self::Value, __E>
1117                    where
1118                        __E: #serde_with_crate_path::serde::de::Error,
1119                    {
1120                        let utf8 = #serde_with_crate_path::core::str::from_utf8(value).map_err(#serde_with_crate_path::serde::de::Error::custom)?;
1121                        self.visit_str(utf8)
1122                    }
1123                }
1124
1125                deserializer.deserialize_str(Helper(#serde_with_crate_path::__private__::PhantomData))
1126            }
1127        }
1128    }
1129}
1130
1131/// Serialize value by using it's [`Display`] implementation
1132///
1133/// This is an alternative way to implement `Serialize` for types, which also implement [`Display`]
1134/// by serializing the type as string. Ensure that the struct/enum also implements [`Display`].
1135/// If the implementation is missing, you will get an error message like
1136/// ```text
1137/// error[E0277]: `Struct` doesn't implement `std::fmt::Display`
1138/// ```
1139///
1140/// Deserialization with [`FromStr`] is available with the matching [`DeserializeFromStr`] derive.
1141///
1142/// # Attributes
1143///
1144/// Attributes for the derive can be specified via the `#[serde_with(...)]` attribute on the struct
1145/// or enum. Currently, these arguments to the attribute are possible:
1146///
1147/// * **`#[serde_with(crate = "...")]`**: This allows using `SerializeDisplay` when `serde_with` is
1148///   not available from the crate root. This happens while [renaming dependencies in
1149///   Cargo.toml][cargo-toml-rename] or when re-exporting the macro from a different crate.
1150///
1151///   This argument is analogue to [serde's crate argument][serde-crate] and the [crate argument
1152///   to `serde_as`][serde-as-crate].
1153///
1154/// # Example
1155///
1156/// ```rust,ignore
1157/// use std::fmt;
1158///
1159/// #[derive(SerializeDisplay)]
1160/// struct A {
1161///     a: u32,
1162///     b: bool,
1163/// }
1164///
1165/// impl fmt::Display for A {
1166///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1167///         write!(f, "{}<>{}", self.a, self.b)
1168///     }
1169/// }
1170///
1171/// let a = A { a: 123, b: false };
1172/// assert_eq!(r#""123<>false""#, serde_json::to_string(&a).unwrap());
1173/// ```
1174///
1175/// [`Display`]: std::fmt::Display
1176/// [`FromStr`]: std::str::FromStr
1177/// [cargo-toml-rename]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#renaming-dependencies-in-cargotoml
1178/// [serde-as-crate]: https://docs.rs/serde_with/3.13.0/serde_with/guide/serde_as/index.html#re-exporting-serde_as
1179/// [serde-crate]: https://serde.rs/container-attrs.html#crate
1180#[proc_macro_derive(SerializeDisplay, attributes(serde_with))]
1181pub fn derive_serialize_display(item: TokenStream) -> TokenStream {
1182    let input: DeriveInput = parse_macro_input!(item);
1183    let derive_options = match DeriveOptions::from_derive_input(&input) {
1184        Ok(opt) => opt,
1185        Err(err) => {
1186            return err;
1187        }
1188    };
1189    TokenStream::from(serialize_display(
1190        input,
1191        false,
1192        derive_options.get_serde_with_path(),
1193    ))
1194}
1195
1196/// Serialize value by using its [`Display`] implementation with the “alternate” (`#`) format flag
1197///
1198/// This derive implements `serde::Serialize` for any type that already implements
1199/// [`std::fmt::Display`], emitting its string form using the alternate formatting specifier
1200/// (`{:#}`) instead of the normal `{}`.  In other words, rather than calling
1201/// `format!("{}", self)`, it calls `format!("{:#}", self)`.
1202///
1203/// Ensure that your type implements [`Display`], or you will get a compile‐error such as:
1204/// ```text
1205/// error[E0277]: `MyType` doesn't implement `std::fmt::Display`
1206/// ```
1207///
1208/// Deserialization from strings via [`std::str::FromStr`] is handled by the companion
1209/// [`DeserializeFromStr`] derive.
1210///
1211/// # Attributes
1212///
1213/// You may customize which `serde_with` crate is used (for renamed or re-exported crates)
1214/// via the same attribute namespace:
1215///
1216/// * `#[serde_with(crate = "...")]`
1217///   When your workspace renames or re-exports `serde_with`, use this to point at the correct path.
1218///   For example:
1219///   ```rust,ignore
1220///   #[derive(SerializeDisplayAlt)]
1221///   #[serde_with(crate = "my_forked_serde_with")]
1222///   pub struct Foo(/* … */);
1223///   ```
1224///
1225/// # Example
1226///
1227/// ```rust,ignore
1228/// use std::fmt;
1229/// use serde_with::{SerializeDisplayAlt, DeserializeFromStr};
1230///
1231/// #[derive(Debug, Clone, SerializeDisplayAlt, DeserializeFromStr)]
1232/// #[serde(transparent)]
1233/// pub struct MyType(u32);
1234///
1235/// impl fmt::Display for MyType {
1236///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1237///         if f.alternate() {
1238///             // Alternate formatting: hex with 0x prefix
1239///             write!(f, "0x{:X}", self.0)
1240///         } else {
1241///             // Standard formatting: decimal
1242///             write!(f, "{}", self.0)
1243///         }
1244///     }
1245/// }
1246///
1247/// let v = MyType(15);
1248/// // SerializeDisplayAlt always uses `{:#}`, so this yields `"0xF"`
1249/// assert_eq!(r#""0xF""#, serde_json::to_string(&v).unwrap());
1250/// ```
1251///
1252/// [`Display`]: std::fmt::Display
1253/// [`FromStr`]: std::str::FromStr
1254/// [`DeserializeFromStr`]: crate::DeserializeFromStr
1255#[proc_macro_derive(SerializeDisplayAlt, attributes(serde_with))]
1256pub fn derive_serialize_display_alt(item: TokenStream) -> TokenStream {
1257    let input: DeriveInput = parse_macro_input!(item);
1258    let derive_options = match DeriveOptions::from_derive_input(&input) {
1259        Ok(opt) => opt,
1260        Err(err) => {
1261            return err;
1262        }
1263    };
1264    TokenStream::from(serialize_display(
1265        input,
1266        true,
1267        derive_options.get_serde_with_path(),
1268    ))
1269}
1270
1271fn serialize_display(
1272    mut input: DeriveInput,
1273    alternate: bool,
1274    serde_with_crate_path: Path,
1275) -> TokenStream2 {
1276    let ident = input.ident;
1277    input
1278        .generics
1279        .make_where_clause()
1280        .predicates
1281        .push(parse_quote!(Self: #serde_with_crate_path::__private__::Display));
1282    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
1283
1284    let collect_str_param = if alternate {
1285        quote! { &format_args!("{self:#}") }
1286    } else {
1287        quote! { &self }
1288    };
1289
1290    quote! {
1291        #[automatically_derived]
1292        impl #impl_generics #serde_with_crate_path::serde::Serialize for #ident #ty_generics #where_clause {
1293            fn serialize<__S>(
1294                &self,
1295                serializer: __S
1296            ) -> #serde_with_crate_path::__private__::Result<__S::Ok, __S::Error>
1297            where
1298                __S: #serde_with_crate_path::serde::Serializer,
1299            {
1300                serializer.collect_str(#collect_str_param)
1301            }
1302        }
1303    }
1304}
1305
1306#[doc(hidden)]
1307/// Private function. Not part of the public API
1308///
1309/// The only task of this derive macro is to consume any `serde_as` attributes and turn them into
1310/// inert attributes. This allows the serde_as macro to keep the field attributes without causing
1311/// compiler errors. The intend is that keeping the field attributes allows downstream crates to
1312/// consume and act on them without causing an ordering dependency to the serde_as macro.
1313///
1314/// Otherwise, downstream proc-macros would need to be placed *in front of* the main `#[serde_as]`
1315/// attribute, since otherwise the field attributes would already be stripped off.
1316///
1317/// More details about the use-cases in the GitHub discussion: <https://github.com/jonasbb/serde_with/discussions/260>.
1318#[proc_macro_derive(
1319    __private_consume_serde_as_attributes,
1320    attributes(serde_as, serde_with)
1321)]
1322pub fn __private_consume_serde_as_attributes(_: TokenStream) -> TokenStream {
1323    TokenStream::new()
1324}
1325
1326/// Apply attributes to all fields with matching types
1327///
1328/// Whenever you experience the need to apply the same attributes to multiple fields, you can use
1329/// this macro. It allows you to specify a list of types and a list of attributes.
1330/// Each field with a "matching" type will then get the attributes applied.
1331/// The `apply` attribute must be placed *before* any consuming attributes, such as `derive` or
1332/// `serde_as`, because Rust expands all attributes in order.
1333///
1334/// For example, if your struct or enum contains many `Option<T>` fields, but you do not want to
1335/// serialize `None` values, you can use this macro to apply the `#[serde(skip_serializing_if =
1336/// "Option::is_none")]` attribute to all fields of type `Option<T>`.
1337///
1338/// ```rust
1339/// # use serde_with_macros as serde_with;
1340/// #[serde_with::apply(
1341/// #   crate="serde_with",
1342///     Option => #[serde(skip_serializing_if = "Option::is_none")],
1343/// )]
1344/// #[derive(serde::Serialize)]
1345/// # #[derive(Default)]
1346/// struct Data {
1347///     a: Option<String>,
1348///     b: Option<u64>,
1349///     c: Option<String>,
1350///     d: Option<bool>,
1351/// }
1352/// #
1353/// # assert_eq!("{}", serde_json::to_string(&Data::default()).unwrap());
1354/// ```
1355///
1356/// Each rule starts with a type pattern, specifying which fields to match and a list of attributes
1357/// to apply. Multiple rules can be provided in a single `apply` attribute.
1358///
1359/// ```rust
1360/// # use serde_with_macros as serde_with;
1361/// #[serde_with::apply(
1362/// #   crate="serde_with",
1363///     Option => #[serde(default)] #[serde(skip_serializing_if = "Option::is_none")],
1364///     Option<bool> => #[serde(rename = "bool")],
1365/// )]
1366/// # #[derive(serde::Serialize)]
1367/// # #[derive(Default)]
1368/// # struct Data {
1369/// #     a: Option<String>,
1370/// #     b: Option<u64>,
1371/// #     c: Option<String>,
1372/// #     d: Option<bool>,
1373/// # }
1374/// #
1375/// # assert_eq!("{}", serde_json::to_string(&Data::default()).unwrap());
1376/// ```
1377///
1378/// ## Type Patterns
1379///
1380/// The type pattern left of the `=>` specifies which fields to match.
1381///
1382/// | Type Pattern            |                                       Matching Types | Notes                                                                           |
1383/// | :---------------------- | ---------------------------------------------------: | :------------------------------------------------------------------------------ |
1384/// | `_`                     | `Option<bool>`<br>`BTreeMap<&'static str, Vec<u32>>` | `_` matches all fields.                                                         |
1385/// | `Option`                |                   `Option<bool>`<br>`Option<String>` | A missing generic is compatible with any generic arguments.                     |
1386/// | `Option<bool>`          |                                       `Option<bool>` | A fully specified type only matches exactly.                                    |
1387/// | `BTreeMap<String, u32>` |                              `BTreeMap<String, u32>` | A fully specified type only matches exactly.                                    |
1388/// | `BTreeMap<String, _>`   |  `BTreeMap<String, u32>`<br>`BTreeMap<String, bool>` | Any `String` key `BTreeMap` matches, as the value is using the `_` placeholder. |
1389/// | `[u8; _]`               |                               `[u8; 1]`<br>`[u8; N]` | `_` also works as a placeholder for any array length.                           |
1390///
1391/// ## Opt-out for Individual Fields
1392///
1393/// The `apply` attribute will find all fields with a compatible type.
1394/// This can be overly eager and a different set of attributes might be required for a specific
1395/// field. You can opt-out of the `apply` attribute by adding the `#[serde_with(skip_apply)]`
1396/// attribute to the field. This will prevent any `apply` to apply to this field.
1397/// If two rules apply to the same field, it is impossible to opt-out of only a single one.
1398/// In this case the attributes must be applied to the field manually.
1399///
1400/// ```rust
1401/// # use serde_json::json;
1402/// # use serde_with_macros as serde_with;
1403/// #[serde_with::apply(
1404/// #   crate="serde_with",
1405///     Option => #[serde(skip_serializing_if = "Option::is_none")],
1406/// )]
1407/// #[derive(serde::Serialize)]
1408/// struct Data {
1409///     a: Option<String>,
1410///     #[serde_with(skip_apply)]
1411///     always_serialize_this_field: Option<u64>,
1412///     c: Option<String>,
1413///     d: Option<bool>,
1414/// }
1415///
1416/// let data = Data {
1417///     a: None,
1418///     always_serialize_this_field: None,
1419///     c: None,
1420///     d: None,
1421/// };
1422///
1423/// // serializes into this JSON:
1424/// # assert_eq!(json!(
1425/// {
1426///     "always_serialize_this_field": null
1427/// }
1428/// # ), serde_json::to_value(data).unwrap());
1429/// ```
1430///
1431/// # Alternative path to `serde_with` crate
1432///
1433/// If `serde_with` is not available at the default path, its path should be specified with the
1434/// `crate` argument. See [re-exporting `serde_as`] for more use case information.
1435///
1436/// ```rust,ignore
1437/// #[serde_with::apply(
1438///     crate = "::some_other_lib::serde_with"
1439///     Option => #[serde(skip_serializing_if = "Option::is_none")],
1440/// )]
1441/// #[derive(serde::Serialize)]
1442/// struct Data {
1443///     a: Option<String>,
1444///     b: Option<u64>,
1445///     c: Option<String>,
1446///     d: Option<bool>,
1447/// }
1448/// ```
1449#[proc_macro_attribute]
1450pub fn apply(args: TokenStream, input: TokenStream) -> TokenStream {
1451    apply::apply(args, input)
1452}