From 60e3a693b29789045614e2ed73126695bc8b0794 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Mon, 13 Jul 2020 10:54:16 +0200 Subject: [PATCH] Check that `serde` is present in `sc-chain-spec-derive` (#6636) We should check that `serde` is present when using `sc-chains-spec-derive`, because we require this for the generated struct. This also switches the generated `derive` instruction to import `Serialize`/`Deserialize` by path to not require the user to import these derive macros. --- client/chain-spec/derive/src/impls.rs | 21 +++++++++++++++++++-- client/chain-spec/src/lib.rs | 10 ++++------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/client/chain-spec/derive/src/impls.rs b/client/chain-spec/derive/src/impls.rs index 1b22f16581a79..ded961a6da815 100644 --- a/client/chain-spec/derive/src/impls.rs +++ b/client/chain-spec/derive/src/impls.rs @@ -64,7 +64,6 @@ pub fn extension_derive(ast: &DeriveInput) -> proc_macro::TokenStream { }) } - /// Implements required traits and creates `Fork` structs for `ChainSpec` custom parameter group. pub fn group_derive(ast: &DeriveInput) -> proc_macro::TokenStream { derive(ast, |crate_name, name, generics: &syn::Generics, field_names, field_types, _fields| { @@ -75,9 +74,27 @@ pub fn group_derive(ast: &DeriveInput) -> proc_macro::TokenStream { let to_fork = generate_base_to_fork(&fork_name, &field_names); let combine_with = generate_combine_with(&field_names); let to_base = generate_fork_to_base(name, &field_names); + let serde_crate_name = match proc_macro_crate::crate_name("serde") { + Ok(name) => Ident::new(&name.replace("-", "_"), Span::call_site()), + Err(e) => { + let err = Error::new( + Span::call_site(), + &format!("Could not find `serde` crate: {}", e), + ).to_compile_error(); + + return quote!( #err ).into(); + } + }; quote! { - #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ChainSpecExtension)] + #[derive( + Debug, + Clone, + PartialEq, + #serde_crate_name::Serialize, + #serde_crate_name::Deserialize, + ChainSpecExtension, + )] pub struct #fork_name #ty_generics #where_clause { #fork_fields } diff --git a/client/chain-spec/src/lib.rs b/client/chain-spec/src/lib.rs index 66bce2b1363c2..8901a9a682224 100644 --- a/client/chain-spec/src/lib.rs +++ b/client/chain-spec/src/lib.rs @@ -29,10 +29,9 @@ //! //! ```rust //! use std::collections::HashMap; -//! use serde::{Serialize, Deserialize}; //! use sc_chain_spec::{GenericChainSpec, ChainSpecExtension}; //! -//! #[derive(Clone, Debug, Serialize, Deserialize, ChainSpecExtension)] +//! #[derive(Clone, Debug, serde::Serialize, serde::Deserialize, ChainSpecExtension)] //! pub struct MyExtension { //! pub known_blocks: HashMap, //! } @@ -48,21 +47,20 @@ //! block number. //! //! ```rust -//! use serde::{Serialize, Deserialize}; //! use sc_chain_spec::{Forks, ChainSpecGroup, ChainSpecExtension, GenericChainSpec}; //! -//! #[derive(Clone, Debug, Serialize, Deserialize, ChainSpecGroup)] +//! #[derive(Clone, Debug, serde::Serialize, serde::Deserialize, ChainSpecGroup)] //! pub struct ClientParams { //! max_block_size: usize, //! max_extrinsic_size: usize, //! } //! -//! #[derive(Clone, Debug, Serialize, Deserialize, ChainSpecGroup)] +//! #[derive(Clone, Debug, serde::Serialize, serde::Deserialize, ChainSpecGroup)] //! pub struct PoolParams { //! max_transaction_size: usize, //! } //! -//! #[derive(Clone, Debug, Serialize, Deserialize, ChainSpecGroup, ChainSpecExtension)] +//! #[derive(Clone, Debug, serde::Serialize, serde::Deserialize, ChainSpecGroup, ChainSpecExtension)] //! pub struct Extension { //! pub client: ClientParams, //! pub pool: PoolParams,