]>
Commit | Line | Data |
---|---|---|
3b2f2976 XL |
1 | //! This crate provides Serde's two derive macros. |
2 | //! | |
f9f354fc XL |
3 | //! ```edition2018 |
4 | //! # use serde_derive::{Serialize, Deserialize}; | |
abe05a73 | 5 | //! # |
3b2f2976 | 6 | //! #[derive(Serialize, Deserialize)] |
abe05a73 XL |
7 | //! # struct S; |
8 | //! # | |
9 | //! # fn main() {} | |
3b2f2976 XL |
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 | ||
cdc7bbd5 | 16 | #![doc(html_root_url = "https://docs.rs/serde_derive/1.0.125")] |
f9f354fc | 17 | #![allow(unknown_lints, bare_trait_objects)] |
f035d41b | 18 | #![deny(clippy::all, clippy::pedantic)] |
f9f354fc | 19 | // Ignored clippy lints |
f035d41b XL |
20 | #![allow( |
21 | clippy::cognitive_complexity, | |
22 | clippy::enum_variant_names, | |
6a06907d XL |
23 | // clippy bug: https://github.com/rust-lang/rust-clippy/issues/6797 |
24 | clippy::manual_map, | |
3dfed10e | 25 | clippy::match_like_matches_macro, |
f035d41b XL |
26 | clippy::needless_pass_by_value, |
27 | clippy::too_many_arguments, | |
28 | clippy::trivially_copy_pass_by_ref, | |
29 | clippy::used_underscore_binding, | |
30 | clippy::wildcard_in_or_patterns, | |
31 | // clippy bug: https://github.com/rust-lang/rust-clippy/issues/5704 | |
32 | clippy::unnested_or_patterns, | |
8faf50e0 | 33 | )] |
f9f354fc | 34 | // Ignored clippy_pedantic lints |
f035d41b XL |
35 | #![allow( |
36 | clippy::cast_possible_truncation, | |
37 | clippy::checked_conversions, | |
38 | clippy::doc_markdown, | |
39 | clippy::enum_glob_use, | |
40 | clippy::filter_map, | |
41 | clippy::indexing_slicing, | |
42 | clippy::items_after_statements, | |
6a06907d | 43 | clippy::let_underscore_drop, |
fc512014 | 44 | clippy::map_err_ignore, |
f035d41b XL |
45 | clippy::match_same_arms, |
46 | clippy::module_name_repetitions, | |
47 | clippy::must_use_candidate, | |
3dfed10e | 48 | clippy::option_if_let_else, |
f035d41b XL |
49 | clippy::similar_names, |
50 | clippy::single_match_else, | |
51 | clippy::struct_excessive_bools, | |
52 | clippy::too_many_lines, | |
53 | clippy::unseparated_literal_suffix, | |
5869c6ff | 54 | clippy::unused_self, |
f035d41b XL |
55 | clippy::use_self, |
56 | clippy::wildcard_imports | |
83c7162d | 57 | )] |
3b2f2976 | 58 | |
3b2f2976 XL |
59 | #[macro_use] |
60 | extern crate quote; | |
0531ce1d | 61 | #[macro_use] |
ff7c6d11 | 62 | extern crate syn; |
3b2f2976 | 63 | |
3b2f2976 | 64 | extern crate proc_macro; |
0531ce1d XL |
65 | extern crate proc_macro2; |
66 | ||
8faf50e0 XL |
67 | mod internals; |
68 | ||
3b2f2976 | 69 | use proc_macro::TokenStream; |
0531ce1d | 70 | use syn::DeriveInput; |
3b2f2976 XL |
71 | |
72 | #[macro_use] | |
73 | mod bound; | |
74 | #[macro_use] | |
75 | mod fragment; | |
76 | ||
3b2f2976 | 77 | mod de; |
f9f354fc | 78 | mod dummy; |
8faf50e0 | 79 | mod pretend; |
83c7162d | 80 | mod ser; |
8faf50e0 | 81 | mod try; |
3b2f2976 XL |
82 | |
83 | #[proc_macro_derive(Serialize, attributes(serde))] | |
84 | pub fn derive_serialize(input: TokenStream) -> TokenStream { | |
5869c6ff XL |
85 | let mut input = parse_macro_input!(input as DeriveInput); |
86 | ser::expand_derive_serialize(&mut input) | |
0731742a | 87 | .unwrap_or_else(to_compile_errors) |
8faf50e0 | 88 | .into() |
3b2f2976 XL |
89 | } |
90 | ||
91 | #[proc_macro_derive(Deserialize, attributes(serde))] | |
92 | pub fn derive_deserialize(input: TokenStream) -> TokenStream { | |
5869c6ff XL |
93 | let mut input = parse_macro_input!(input as DeriveInput); |
94 | de::expand_derive_deserialize(&mut input) | |
0731742a | 95 | .unwrap_or_else(to_compile_errors) |
8faf50e0 XL |
96 | .into() |
97 | } | |
98 | ||
0731742a XL |
99 | fn to_compile_errors(errors: Vec<syn::Error>) -> proc_macro2::TokenStream { |
100 | let compile_errors = errors.iter().map(syn::Error::to_compile_error); | |
101 | quote!(#(#compile_errors)*) | |
3b2f2976 | 102 | } |