]>
Commit | Line | Data |
---|---|---|
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.125")] | |
17 | #![allow(unknown_lints, bare_trait_objects)] | |
18 | #![deny(clippy::all, clippy::pedantic)] | |
19 | // Ignored clippy lints | |
20 | #![allow( | |
21 | clippy::cognitive_complexity, | |
22 | clippy::enum_variant_names, | |
23 | // clippy bug: https://github.com/rust-lang/rust-clippy/issues/6797 | |
24 | clippy::manual_map, | |
25 | clippy::match_like_matches_macro, | |
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, | |
33 | )] | |
34 | // Ignored clippy_pedantic lints | |
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, | |
43 | clippy::let_underscore_drop, | |
44 | clippy::map_err_ignore, | |
45 | clippy::match_same_arms, | |
46 | clippy::module_name_repetitions, | |
47 | clippy::must_use_candidate, | |
48 | clippy::option_if_let_else, | |
49 | clippy::similar_names, | |
50 | clippy::single_match_else, | |
51 | clippy::struct_excessive_bools, | |
52 | clippy::too_many_lines, | |
53 | clippy::unseparated_literal_suffix, | |
54 | clippy::unused_self, | |
55 | clippy::use_self, | |
56 | clippy::wildcard_imports | |
57 | )] | |
58 | ||
59 | #[macro_use] | |
60 | extern crate quote; | |
61 | #[macro_use] | |
62 | extern crate syn; | |
63 | ||
64 | extern crate proc_macro; | |
65 | extern crate proc_macro2; | |
66 | ||
67 | mod internals; | |
68 | ||
69 | use proc_macro::TokenStream; | |
70 | use syn::DeriveInput; | |
71 | ||
72 | #[macro_use] | |
73 | mod bound; | |
74 | #[macro_use] | |
75 | mod fragment; | |
76 | ||
77 | mod de; | |
78 | mod dummy; | |
79 | mod pretend; | |
80 | mod ser; | |
81 | mod try; | |
82 | ||
83 | #[proc_macro_derive(Serialize, attributes(serde))] | |
84 | pub fn derive_serialize(input: TokenStream) -> TokenStream { | |
85 | let mut input = parse_macro_input!(input as DeriveInput); | |
86 | ser::expand_derive_serialize(&mut input) | |
87 | .unwrap_or_else(to_compile_errors) | |
88 | .into() | |
89 | } | |
90 | ||
91 | #[proc_macro_derive(Deserialize, attributes(serde))] | |
92 | pub fn derive_deserialize(input: TokenStream) -> TokenStream { | |
93 | let mut input = parse_macro_input!(input as DeriveInput); | |
94 | de::expand_derive_deserialize(&mut input) | |
95 | .unwrap_or_else(to_compile_errors) | |
96 | .into() | |
97 | } | |
98 | ||
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)*) | |
102 | } |