2017-04-19 14:43:46 -07:00
|
|
|
//! This crate provides Serde's two derive macros.
|
|
|
|
//!
|
2018-12-31 23:18:09 -05:00
|
|
|
//! ```edition2018
|
2018-12-31 22:59:55 -05:00
|
|
|
//! # use serde_derive::{Serialize, Deserialize};
|
2017-10-22 12:09:56 -07:00
|
|
|
//! #
|
2017-04-19 14:43:46 -07:00
|
|
|
//! #[derive(Serialize, Deserialize)]
|
2017-10-22 12:09:56 -07:00
|
|
|
//! # struct S;
|
|
|
|
//! #
|
|
|
|
//! # fn main() {}
|
2017-04-19 14:43:46 -07:00
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Please refer to [https://serde.rs/derive.html] for how to set this up.
|
|
|
|
//!
|
|
|
|
//! [https://serde.rs/derive.html]: https://serde.rs/derive.html
|
|
|
|
|
2020-06-21 17:31:02 -07:00
|
|
|
#![doc(html_root_url = "https://docs.rs/serde_derive/1.0.114")]
|
2019-05-31 11:34:18 -07:00
|
|
|
#![allow(unknown_lints, bare_trait_objects)]
|
2020-04-05 21:00:52 -07:00
|
|
|
#![deny(clippy::all, clippy::pedantic)]
|
2018-12-31 22:49:38 -05:00
|
|
|
// Ignored clippy lints
|
2020-04-05 21:00:52 -07:00
|
|
|
#![allow(
|
|
|
|
clippy::cognitive_complexity,
|
|
|
|
clippy::enum_variant_names,
|
|
|
|
clippy::needless_pass_by_value,
|
|
|
|
clippy::too_many_arguments,
|
|
|
|
clippy::trivially_copy_pass_by_ref,
|
|
|
|
clippy::used_underscore_binding,
|
2020-06-10 19:38:52 -07:00
|
|
|
clippy::wildcard_in_or_patterns,
|
|
|
|
// clippy bug: https://github.com/rust-lang/rust-clippy/issues/5704
|
|
|
|
clippy::unnested_or_patterns,
|
2018-05-07 11:02:12 -07:00
|
|
|
)]
|
2018-12-31 22:49:38 -05:00
|
|
|
// Ignored clippy_pedantic lints
|
2020-04-05 21:00:52 -07:00
|
|
|
#![allow(
|
|
|
|
clippy::cast_possible_truncation,
|
|
|
|
clippy::checked_conversions,
|
|
|
|
clippy::doc_markdown,
|
|
|
|
clippy::enum_glob_use,
|
|
|
|
clippy::filter_map,
|
|
|
|
clippy::indexing_slicing,
|
|
|
|
clippy::items_after_statements,
|
|
|
|
clippy::match_same_arms,
|
|
|
|
clippy::module_name_repetitions,
|
|
|
|
clippy::must_use_candidate,
|
|
|
|
clippy::similar_names,
|
|
|
|
clippy::single_match_else,
|
|
|
|
clippy::struct_excessive_bools,
|
|
|
|
clippy::too_many_lines,
|
|
|
|
clippy::unseparated_literal_suffix,
|
|
|
|
clippy::use_self,
|
|
|
|
clippy::wildcard_imports
|
2018-04-12 22:58:24 -07:00
|
|
|
)]
|
2017-01-25 20:07:55 -08:00
|
|
|
// The `quote!` macro requires deep recursion.
|
2018-03-19 00:57:58 +01:00
|
|
|
#![recursion_limit = "512"]
|
2017-01-25 20:07:55 -08:00
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate quote;
|
2018-01-08 21:49:09 -08:00
|
|
|
#[macro_use]
|
2017-12-23 20:13:08 -08:00
|
|
|
extern crate syn;
|
2016-08-28 22:19:17 -07:00
|
|
|
|
2017-01-25 20:07:55 -08:00
|
|
|
extern crate proc_macro;
|
2018-01-08 21:49:09 -08:00
|
|
|
extern crate proc_macro2;
|
|
|
|
|
2018-05-05 23:06:16 -07:00
|
|
|
mod internals;
|
|
|
|
|
2016-10-08 18:29:36 -04:00
|
|
|
use proc_macro::TokenStream;
|
2018-01-08 21:49:09 -08:00
|
|
|
use syn::DeriveInput;
|
2016-08-28 22:19:17 -07:00
|
|
|
|
2017-02-20 13:18:38 -08:00
|
|
|
#[macro_use]
|
2017-01-25 20:07:55 -08:00
|
|
|
mod bound;
|
2017-02-20 14:43:51 -08:00
|
|
|
#[macro_use]
|
|
|
|
mod fragment;
|
|
|
|
|
|
|
|
mod de;
|
2018-12-27 15:20:32 -05:00
|
|
|
mod dummy;
|
2018-05-02 13:29:44 -07:00
|
|
|
mod pretend;
|
2018-04-12 22:58:24 -07:00
|
|
|
mod ser;
|
2018-04-21 10:51:32 -07:00
|
|
|
mod try;
|
2017-01-25 20:07:55 -08:00
|
|
|
|
2016-11-15 21:29:34 -05:00
|
|
|
#[proc_macro_derive(Serialize, attributes(serde))]
|
2016-08-28 22:19:17 -07:00
|
|
|
pub fn derive_serialize(input: TokenStream) -> TokenStream {
|
2018-09-06 21:14:18 -07:00
|
|
|
let input = parse_macro_input!(input as DeriveInput);
|
2018-06-02 22:08:01 -07:00
|
|
|
ser::expand_derive_serialize(&input)
|
2018-11-29 08:01:17 +02:00
|
|
|
.unwrap_or_else(to_compile_errors)
|
2018-06-02 22:08:01 -07:00
|
|
|
.into()
|
2016-08-28 22:19:17 -07:00
|
|
|
}
|
|
|
|
|
2016-11-15 21:29:34 -05:00
|
|
|
#[proc_macro_derive(Deserialize, attributes(serde))]
|
2016-08-28 22:19:17 -07:00
|
|
|
pub fn derive_deserialize(input: TokenStream) -> TokenStream {
|
2018-09-06 21:14:18 -07:00
|
|
|
let input = parse_macro_input!(input as DeriveInput);
|
2018-06-02 22:08:01 -07:00
|
|
|
de::expand_derive_deserialize(&input)
|
2018-11-29 08:01:17 +02:00
|
|
|
.unwrap_or_else(to_compile_errors)
|
2018-06-02 22:08:01 -07:00
|
|
|
.into()
|
|
|
|
}
|
|
|
|
|
2018-11-29 08:01:17 +02:00
|
|
|
fn to_compile_errors(errors: Vec<syn::Error>) -> proc_macro2::TokenStream {
|
|
|
|
let compile_errors = errors.iter().map(syn::Error::to_compile_error);
|
|
|
|
quote!(#(#compile_errors)*)
|
2016-08-28 22:19:17 -07:00
|
|
|
}
|