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.97")]
17 #![allow(unknown_lints, bare_trait_objects)]
18 #![cfg_attr(feature = "cargo-clippy", allow(renamed_and_removed_lints))]
19 #![cfg_attr(feature = "cargo-clippy", deny(clippy, clippy_pedantic))]
20 // Ignored clippy lints
21 #![cfg_attr(
22 feature = "cargo-clippy",
23 allow(
24 cognitive_complexity,
25 enum_variant_names,
26 needless_pass_by_value,
27 redundant_field_names,
28 too_many_arguments,
29 trivially_copy_pass_by_ref,
30 used_underscore_binding,
31 )
32 )]
33 // Ignored clippy_pedantic lints
34 #![cfg_attr(
35 feature = "cargo-clippy",
36 allow(
37 cast_possible_truncation,
38 checked_conversions,
39 doc_markdown,
40 enum_glob_use,
41 filter_map,
42 indexing_slicing,
43 items_after_statements,
44 match_same_arms,
45 module_name_repetitions,
46 similar_names,
47 single_match_else,
48 unseparated_literal_suffix,
49 use_self,
50 )
51 )]
52 // The `quote!` macro requires deep recursion.
53 #![recursion_limit = "512"]
54
55 #[macro_use]
56 extern crate quote;
57 #[macro_use]
58 extern crate syn;
59
60 extern crate proc_macro;
61 extern crate proc_macro2;
62
63 mod internals;
64
65 use proc_macro::TokenStream;
66 use syn::DeriveInput;
67
68 #[macro_use]
69 mod bound;
70 #[macro_use]
71 mod fragment;
72
73 mod de;
74 mod dummy;
75 mod pretend;
76 mod ser;
77 mod try;
78
79 #[proc_macro_derive(Serialize, attributes(serde))]
derive_serialize(input: TokenStream) -> TokenStream80 pub fn derive_serialize(input: TokenStream) -> TokenStream {
81 let input = parse_macro_input!(input as DeriveInput);
82 ser::expand_derive_serialize(&input)
83 .unwrap_or_else(to_compile_errors)
84 .into()
85 }
86
87 #[proc_macro_derive(Deserialize, attributes(serde))]
derive_deserialize(input: TokenStream) -> TokenStream88 pub fn derive_deserialize(input: TokenStream) -> TokenStream {
89 let input = parse_macro_input!(input as DeriveInput);
90 de::expand_derive_deserialize(&input)
91 .unwrap_or_else(to_compile_errors)
92 .into()
93 }
94
to_compile_errors(errors: Vec<syn::Error>) -> proc_macro2::TokenStream95 fn to_compile_errors(errors: Vec<syn::Error>) -> proc_macro2::TokenStream {
96 let compile_errors = errors.iter().map(syn::Error::to_compile_error);
97 quote!(#(#compile_errors)*)
98 }
99