Skip to content

Commit

Permalink
Check that serde is present in sc-chain-spec-derive (paritytech#6636
Browse files Browse the repository at this point in the history
)

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.
  • Loading branch information
bkchr authored Jul 13, 2020
1 parent b0eefeb commit 60e3a69
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
21 changes: 19 additions & 2 deletions client/chain-spec/derive/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand All @@ -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
}
Expand Down
10 changes: 4 additions & 6 deletions client/chain-spec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64, String>,
//! }
Expand All @@ -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,
Expand Down

0 comments on commit 60e3a69

Please sign in to comment.