]> git.proxmox.com Git - rustc.git/blob - vendor/serde_derive/src/lib.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / vendor / serde_derive / src / lib.rs
1 //! This crate provides Serde's two derive macros.
2 //!
3 //! ```edition2018
4 //! # use serde_derive::{Serialize, Deserialize};
5 //! #
6 //! #[derive(Serialize, Deserialize)]
7 //! # struct S;
8 //! #
9 //! # fn main() {}
10 //! ```
11 //!
12 //! Please refer to [https://serde.rs/derive.html] for how to set this up.
13 //!
14 //! [https://serde.rs/derive.html]: https://serde.rs/derive.html
15
16 #![doc(html_root_url = "https://docs.rs/serde_derive/1.0.137")]
17 #![allow(unknown_lints, bare_trait_objects)]
18 // Ignored clippy lints
19 #![allow(
20 // clippy false positive: https://github.com/rust-lang/rust-clippy/issues/7054
21 clippy::branches_sharing_code,
22 clippy::cognitive_complexity,
23 // clippy bug: https://github.com/rust-lang/rust-clippy/issues/7575
24 clippy::collapsible_match,
25 clippy::enum_variant_names,
26 // clippy bug: https://github.com/rust-lang/rust-clippy/issues/6797
27 clippy::manual_map,
28 clippy::match_like_matches_macro,
29 clippy::needless_pass_by_value,
30 clippy::too_many_arguments,
31 clippy::trivially_copy_pass_by_ref,
32 clippy::used_underscore_binding,
33 clippy::wildcard_in_or_patterns,
34 // clippy bug: https://github.com/rust-lang/rust-clippy/issues/5704
35 clippy::unnested_or_patterns,
36 )]
37 // Ignored clippy_pedantic lints
38 #![allow(
39 clippy::cast_possible_truncation,
40 clippy::checked_conversions,
41 clippy::doc_markdown,
42 clippy::enum_glob_use,
43 clippy::indexing_slicing,
44 clippy::items_after_statements,
45 clippy::let_underscore_drop,
46 clippy::manual_assert,
47 clippy::map_err_ignore,
48 clippy::match_same_arms,
49 // clippy bug: https://github.com/rust-lang/rust-clippy/issues/6984
50 clippy::match_wildcard_for_single_variants,
51 clippy::module_name_repetitions,
52 clippy::must_use_candidate,
53 clippy::option_if_let_else,
54 clippy::similar_names,
55 clippy::single_match_else,
56 clippy::struct_excessive_bools,
57 clippy::too_many_lines,
58 clippy::unseparated_literal_suffix,
59 clippy::unused_self,
60 clippy::use_self,
61 clippy::wildcard_imports
62 )]
63 #![cfg_attr(all(test, exhaustive), feature(non_exhaustive_omitted_patterns_lint))]
64
65 #[macro_use]
66 extern crate quote;
67 #[macro_use]
68 extern crate syn;
69
70 extern crate proc_macro;
71 extern crate proc_macro2;
72
73 mod internals;
74
75 use proc_macro::TokenStream;
76 use syn::DeriveInput;
77
78 #[macro_use]
79 mod bound;
80 #[macro_use]
81 mod fragment;
82
83 mod de;
84 mod dummy;
85 mod pretend;
86 mod ser;
87 mod try;
88
89 #[proc_macro_derive(Serialize, attributes(serde))]
90 pub fn derive_serialize(input: TokenStream) -> TokenStream {
91 let mut input = parse_macro_input!(input as DeriveInput);
92 ser::expand_derive_serialize(&mut input)
93 .unwrap_or_else(to_compile_errors)
94 .into()
95 }
96
97 #[proc_macro_derive(Deserialize, attributes(serde))]
98 pub fn derive_deserialize(input: TokenStream) -> TokenStream {
99 let mut input = parse_macro_input!(input as DeriveInput);
100 de::expand_derive_deserialize(&mut input)
101 .unwrap_or_else(to_compile_errors)
102 .into()
103 }
104
105 fn to_compile_errors(errors: Vec<syn::Error>) -> proc_macro2::TokenStream {
106 let compile_errors = errors.iter().map(syn::Error::to_compile_error);
107 quote!(#(#compile_errors)*)
108 }