Skip to content
Prev Previous commit
Next Next commit
Remove Default impls in favor of explicit constructors
  • Loading branch information
cmichi committed Sep 26, 2022
commit a920a2af408a55f455e9d6a80b8f5ec9246d85fe
3 changes: 2 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ fn codegen<I: Input>(
.map(|raw| syn::parse_str(raw))
.collect::<Result<Vec<_>, _>>()?;

let mut derives = DerivesRegistry::default();
let crate_path = crate_path.map(Into::into).unwrap_or_default();
let mut derives = DerivesRegistry::new(&crate_path);
derives.extend_for_all(p.into_iter());

let runtime_api = generator.generate_runtime(item_mod, derives, crate_path.into());
Expand Down
2 changes: 1 addition & 1 deletion codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
//! pub mod api {}
//! );
//! // Default module derivatives.
//! let mut derives = DerivesRegistry::default();
//! let mut derives = DerivesRegistry::new(&CratePath::default());
//! // Generate the Runtime API.
//! let generator = subxt_codegen::RuntimeGenerator::new(metadata);
//! let runtime_api = generator.generate_runtime(item_mod, derives, CratePath::default());
Expand Down
48 changes: 23 additions & 25 deletions codegen/src/types/derives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,24 @@ use std::collections::{
HashSet,
};

#[derive(Debug, Default, Clone)]
#[derive(Debug, Clone)]
pub struct DerivesRegistry {
default_derives: Derives,
specific_type_derives: HashMap<syn::TypePath, Derives>,
}

impl DerivesRegistry {
/// Creates a new `DeviceRegistry` with the supplied `crate_path`.
///
/// The `crate_path` denotes the `subxt` crate access path in the
/// generated code.
pub fn new(crate_path: &CratePath) -> Self {
Self {
default_derives: Derives::new(crate_path),
specific_type_derives: Default::default(),
}
}

/// Insert derives to be applied to all generated types.
pub fn extend_for_all(&mut self, derives: impl IntoIterator<Item = syn::Path>) {
self.default_derives.derives.extend(derives)
Expand All @@ -36,7 +47,7 @@ impl DerivesRegistry {
let type_derives = self
.specific_type_derives
.entry(ty)
.or_insert_with(|| Derives::default_with_crate_path(crate_path));
.or_insert_with(|| Derives::new(crate_path));
type_derives.derives.extend(derives)
}

Expand All @@ -56,13 +67,6 @@ impl DerivesRegistry {
}
Derives { derives: defaults }
}

pub fn default_with_crate_path(crate_path: &CratePath) -> Self {
Self {
default_derives: Derives::default_with_crate_path(crate_path),
specific_type_derives: Default::default(),
}
}
}

#[derive(Debug, Clone)]
Expand All @@ -78,6 +82,16 @@ impl FromIterator<syn::Path> for Derives {
}

impl Derives {
/// Creates a new instance of `Derives` with the `crate_path` prepended
/// to the set of default derives that reside in `subxt`.
pub fn new(crate_path: &CratePath) -> Self {
let mut derives = HashSet::new();
derives.insert(syn::parse_quote!(#crate_path::ext::codec::Encode));
derives.insert(syn::parse_quote!(#crate_path::ext::codec::Decode));
derives.insert(syn::parse_quote!(Debug));
Self { derives }
}

/// Add `#crate_path::ext::codec::CompactAs` to the derives.
pub fn insert_codec_compact_as(&mut self, crate_path: &CratePath) {
self.insert(parse_quote!(#crate_path::ext::codec::CompactAs));
Expand All @@ -92,22 +106,6 @@ impl Derives {
pub fn insert(&mut self, derive: syn::Path) {
self.derives.insert(derive);
}

/// Creates a default instance of `Derives` with the `crate_path` prepended
/// to the set of default derives that reside in `subxt`.
pub fn default_with_crate_path(crate_path: &CratePath) -> Self {
let mut derives = HashSet::new();
derives.insert(syn::parse_quote!(#crate_path::ext::codec::Encode));
derives.insert(syn::parse_quote!(#crate_path::ext::codec::Decode));
derives.insert(syn::parse_quote!(Debug));
Self { derives }
}
}

impl Default for Derives {
fn default() -> Self {
Derives::default_with_crate_path(&CratePath::default())
}
}

impl quote::ToTokens for Derives {
Expand Down
40 changes: 20 additions & 20 deletions codegen/src/types/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn generate_struct_with_primitives() {
&portable_types,
"root",
Default::default(),
DerivesRegistry::default_with_crate_path(&"::subxt_path".into()),
DerivesRegistry::new(&"::subxt_path".into()),
"::subxt_path".into(),
);
let types = type_gen.generate_types_mod();
Expand Down Expand Up @@ -90,7 +90,7 @@ fn generate_struct_with_a_struct_field() {
&portable_types,
"root",
Default::default(),
DerivesRegistry::default_with_crate_path(&"::subxt_path".into()),
DerivesRegistry::new(&"::subxt_path".into()),
"::subxt_path".into(),
);
let types = type_gen.generate_types_mod();
Expand Down Expand Up @@ -136,7 +136,7 @@ fn generate_tuple_struct() {
&portable_types,
"root",
Default::default(),
DerivesRegistry::default_with_crate_path(&"::subxt_path".into()),
DerivesRegistry::new(&"::subxt_path".into()),
"::subxt_path".into(),
);
let types = type_gen.generate_types_mod();
Expand Down Expand Up @@ -219,7 +219,7 @@ fn derive_compact_as_for_uint_wrapper_structs() {
&portable_types,
"root",
Default::default(),
DerivesRegistry::default_with_crate_path(&"::subxt_path".into()),
DerivesRegistry::new(&"::subxt_path".into()),
"::subxt_path".into(),
);
let types = type_gen.generate_types_mod();
Expand Down Expand Up @@ -284,7 +284,7 @@ fn generate_enum() {
&portable_types,
"root",
Default::default(),
DerivesRegistry::default_with_crate_path(&"::subxt_path".into()),
DerivesRegistry::new(&"::subxt_path".into()),
"::subxt_path".into(),
);
let types = type_gen.generate_types_mod();
Expand Down Expand Up @@ -343,7 +343,7 @@ fn compact_fields() {
&portable_types,
"root",
Default::default(),
DerivesRegistry::default_with_crate_path(&"::subxt_path".into()),
DerivesRegistry::new(&"::subxt_path".into()),
"::subxt_path".into(),
);
let types = type_gen.generate_types_mod();
Expand Down Expand Up @@ -400,7 +400,7 @@ fn compact_generic_parameter() {
&portable_types,
"root",
Default::default(),
DerivesRegistry::default_with_crate_path(&"::subxt_path".into()),
DerivesRegistry::new(&"::subxt_path".into()),
"::subxt_path".into(),
);
let types = type_gen.generate_types_mod();
Expand Down Expand Up @@ -442,7 +442,7 @@ fn generate_array_field() {
&portable_types,
"root",
Default::default(),
DerivesRegistry::default_with_crate_path(&"::subxt_path".into()),
DerivesRegistry::new(&"::subxt_path".into()),
"::subxt_path".into(),
);
let types = type_gen.generate_types_mod();
Expand Down Expand Up @@ -480,7 +480,7 @@ fn option_fields() {
&portable_types,
"root",
Default::default(),
DerivesRegistry::default_with_crate_path(&"::subxt_path".into()),
DerivesRegistry::new(&"::subxt_path".into()),
"::subxt_path".into(),
);
let types = type_gen.generate_types_mod();
Expand Down Expand Up @@ -521,7 +521,7 @@ fn box_fields_struct() {
&portable_types,
"root",
Default::default(),
DerivesRegistry::default_with_crate_path(&"::subxt_path".into()),
DerivesRegistry::new(&"::subxt_path".into()),
"::subxt_path".into(),
);
let types = type_gen.generate_types_mod();
Expand Down Expand Up @@ -562,7 +562,7 @@ fn box_fields_enum() {
&portable_types,
"root",
Default::default(),
DerivesRegistry::default_with_crate_path(&"::subxt_path".into()),
DerivesRegistry::new(&"::subxt_path".into()),
"::subxt_path".into(),
);
let types = type_gen.generate_types_mod();
Expand Down Expand Up @@ -603,7 +603,7 @@ fn range_fields() {
&portable_types,
"root",
Default::default(),
DerivesRegistry::default_with_crate_path(&"::subxt_path".into()),
DerivesRegistry::new(&"::subxt_path".into()),
"::subxt_path".into(),
);
let types = type_gen.generate_types_mod();
Expand Down Expand Up @@ -648,7 +648,7 @@ fn generics() {
&portable_types,
"root",
Default::default(),
DerivesRegistry::default_with_crate_path(&"::subxt_path".into()),
DerivesRegistry::new(&"::subxt_path".into()),
"::subxt_path".into(),
);
let types = type_gen.generate_types_mod();
Expand Down Expand Up @@ -697,7 +697,7 @@ fn generics_nested() {
&portable_types,
"root",
Default::default(),
DerivesRegistry::default_with_crate_path(&"::subxt_path".into()),
DerivesRegistry::new(&"::subxt_path".into()),
"::subxt_path".into(),
);
let types = type_gen.generate_types_mod();
Expand Down Expand Up @@ -749,7 +749,7 @@ fn generate_bitvec() {
&portable_types,
"root",
Default::default(),
DerivesRegistry::default_with_crate_path(&"::subxt_path".into()),
DerivesRegistry::new(&"::subxt_path".into()),
"::subxt_path".into(),
);
let types = type_gen.generate_types_mod();
Expand Down Expand Up @@ -803,7 +803,7 @@ fn generics_with_alias_adds_phantom_data_marker() {
&portable_types,
"root",
Default::default(),
DerivesRegistry::default_with_crate_path(&"::subxt_path".into()),
DerivesRegistry::new(&"::subxt_path".into()),
"::subxt_path".into(),
);
let types = type_gen.generate_types_mod();
Expand Down Expand Up @@ -864,7 +864,7 @@ fn modules() {
&portable_types,
"root",
Default::default(),
DerivesRegistry::default_with_crate_path(&"::subxt_path".into()),
DerivesRegistry::new(&"::subxt_path".into()),
"::subxt_path".into(),
);
let types = type_gen.generate_types_mod();
Expand Down Expand Up @@ -922,7 +922,7 @@ fn dont_force_struct_names_camel_case() {
&portable_types,
"root",
Default::default(),
DerivesRegistry::default_with_crate_path(&"::subxt_path".into()),
DerivesRegistry::new(&"::subxt_path".into()),
"::subxt_path".into(),
);
let types = type_gen.generate_types_mod();
Expand Down Expand Up @@ -957,7 +957,7 @@ fn apply_user_defined_derives_for_all_types() {
let portable_types: PortableRegistry = registry.into();

// configure derives
let mut derives = DerivesRegistry::default_with_crate_path(&"::subxt_path".into());
let mut derives = DerivesRegistry::new(&"::subxt_path".into());
derives.extend_for_all(vec![parse_quote!(Clone), parse_quote!(Eq)]);

let type_gen = TypeGenerator::new(
Expand Down Expand Up @@ -1006,7 +1006,7 @@ fn apply_user_defined_derives_for_specific_types() {
let portable_types: PortableRegistry = registry.into();

// configure derives
let mut derives = DerivesRegistry::default_with_crate_path(&"::subxt_path".into());
let mut derives = DerivesRegistry::new(&"::subxt_path".into());
// for all types
derives.extend_for_all(vec![parse_quote!(Eq)]);
// for specific types
Expand Down
2 changes: 1 addition & 1 deletion macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub fn subxt(args: TokenStream, input: TokenStream) -> TokenStream {
Some(crate_path) => crate_path.into(),
None => subxt_codegen::CratePath::default(),
};
let mut derives_registry = DerivesRegistry::default_with_crate_path(&crate_path);
let mut derives_registry = DerivesRegistry::new(&crate_path);
if let Some(derive_for_all) = args.derive_for_all_types {
derives_registry.extend_for_all(derive_for_all.iter().cloned());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn generate_runtime_interface(crate_path: CratePath) -> String {
let item_mod = syn::parse_quote!(
pub mod api {}
);
let derives = DerivesRegistry::default();
let derives = DerivesRegistry::new(&crate_path);
generator
.generate_runtime(item_mod, derives, crate_path)
.to_string()
Expand Down