]> git.proxmox.com Git - rustc.git/blob - vendor/serde_derive/src/lib.rs
New upstream version 1.32.0~beta.2+dfsg1
[rustc.git] / vendor / serde_derive / src / lib.rs
1 // Copyright 2017 Serde Developers
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8
9 //! This crate provides Serde's two derive macros.
10 //!
11 //! ```rust
12 //! # #[macro_use]
13 //! # extern crate serde_derive;
14 //! #
15 //! #[derive(Serialize, Deserialize)]
16 //! # struct S;
17 //! #
18 //! # fn main() {}
19 //! ```
20 //!
21 //! Please refer to [https://serde.rs/derive.html] for how to set this up.
22 //!
23 //! [https://serde.rs/derive.html]: https://serde.rs/derive.html
24
25 #![doc(html_root_url = "https://docs.rs/serde_derive/1.0.75")]
26 #![cfg_attr(feature = "cargo-clippy", deny(clippy, clippy_pedantic))]
27 // Whitelisted clippy lints
28 #![cfg_attr(
29 feature = "cargo-clippy",
30 allow(
31 enum_variant_names,
32 redundant_field_names,
33 too_many_arguments,
34 used_underscore_binding,
35 cyclomatic_complexity,
36 needless_pass_by_value
37 )
38 )]
39 // Whitelisted clippy_pedantic lints
40 #![cfg_attr(
41 feature = "cargo-clippy",
42 allow(
43 items_after_statements,
44 doc_markdown,
45 stutter,
46 similar_names,
47 use_self,
48 single_match_else,
49 enum_glob_use,
50 match_same_arms,
51 filter_map,
52 cast_possible_truncation,
53 indexing_slicing,
54 )
55 )]
56 // The `quote!` macro requires deep recursion.
57 #![recursion_limit = "512"]
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 pretend;
79 mod ser;
80 mod try;
81
82 #[proc_macro_derive(Serialize, attributes(serde))]
83 pub fn derive_serialize(input: TokenStream) -> TokenStream {
84 let input: DeriveInput = syn::parse(input).unwrap();
85 ser::expand_derive_serialize(&input)
86 .unwrap_or_else(compile_error)
87 .into()
88 }
89
90 #[proc_macro_derive(Deserialize, attributes(serde))]
91 pub fn derive_deserialize(input: TokenStream) -> TokenStream {
92 let input: DeriveInput = syn::parse(input).unwrap();
93 de::expand_derive_deserialize(&input)
94 .unwrap_or_else(compile_error)
95 .into()
96 }
97
98 fn compile_error(message: String) -> proc_macro2::TokenStream {
99 quote! {
100 compile_error!(#message);
101 }
102 }