Skip to content

Commit

Permalink
expose default typegen settings
Browse files Browse the repository at this point in the history
  • Loading branch information
tadeohepperle committed Nov 29, 2023
1 parent fd718bf commit 985a46c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
22 changes: 2 additions & 20 deletions codegen/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ mod storage;

use scale_typegen::typegen::ir::type_ir::CompositeIR;
use scale_typegen::typegen::type_params::TypeParameters;
use scale_typegen::{TypeGenerator, TypeGeneratorSettings};
use scale_typegen::TypeGenerator;
use subxt_metadata::Metadata;

use crate::error::CodegenError;
use crate::subxt_type_gen_settings;
use crate::{api::custom_values::generate_custom_values, ir};

use heck::ToSnakeCase as _;
use proc_macro2::TokenStream as TokenStream2;
use quote::{format_ident, quote};
use syn::parse_quote;

/// Create the API for interacting with a Substrate runtime.
pub struct RuntimeGenerator {
Expand Down Expand Up @@ -321,24 +321,6 @@ impl RuntimeGenerator {
}
}

fn subxt_type_gen_settings(
derives: scale_typegen::DerivesRegistry,
substitutes: scale_typegen::TypeSubstitutes,
crate_path: &syn::Path,
should_gen_docs: bool,
) -> TypeGeneratorSettings {
TypeGeneratorSettings {
types_mod_ident: parse_quote!(runtime_types),
should_gen_docs,
derives,
substitutes,
decoded_bits_type_path: Some(parse_quote!(#crate_path::utils::bits::DecodedBits)),
compact_as_type_path: Some(parse_quote!(#crate_path::ext::codec::CompactAs)),
compact_type_path: Some(parse_quote!(#crate_path::ext::codec::Compact)),
insert_codec_attributes: true,
}
}

/// Return a vector of tuples of variant names and corresponding struct definitions.
pub fn generate_structs_from_variants<F>(
type_gen: &TypeGenerator,
Expand Down
30 changes: 29 additions & 1 deletion codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ use getrandom as _;
use api::RuntimeGenerator;
use proc_macro2::TokenStream as TokenStream2;
use scale_typegen::{
typegen::settings::substitutes::absolute_path, DerivesRegistry, TypeSubstitutes, TypegenError,
typegen::settings::substitutes::absolute_path, DerivesRegistry, TypeGeneratorSettings,
TypeSubstitutes, TypegenError,
};
use std::collections::HashMap;
use syn::parse_quote;
Expand Down Expand Up @@ -290,6 +291,33 @@ impl CodegenBuilder {
}
}

/// The default [`scale_typegen::TypeGeneratorSettings`], subxt is using for generating code.
/// Useful for emulating subxt's code generation settings from e.g. subxt-explorer.
pub fn default_subxt_type_gen_settings() -> TypeGeneratorSettings {
let crate_path: syn::Path = parse_quote!(::subxt);
let derives = default_derives(&crate_path);
let substitutes = default_substitutes(&crate_path);
subxt_type_gen_settings(derives, substitutes, &crate_path, true)
}

fn subxt_type_gen_settings(
derives: scale_typegen::DerivesRegistry,
substitutes: scale_typegen::TypeSubstitutes,
crate_path: &syn::Path,
should_gen_docs: bool,
) -> TypeGeneratorSettings {
TypeGeneratorSettings {
types_mod_ident: parse_quote!(runtime_types),
should_gen_docs,
derives,
substitutes,
decoded_bits_type_path: Some(parse_quote!(#crate_path::utils::bits::DecodedBits)),
compact_as_type_path: Some(parse_quote!(#crate_path::ext::codec::CompactAs)),
compact_type_path: Some(parse_quote!(#crate_path::ext::codec::Compact)),
insert_codec_attributes: true,
}
}

fn default_derives(crate_path: &syn::Path) -> DerivesRegistry {
let encode_crate_path = quote::quote! { #crate_path::ext::scale_encode }.to_string();
let decode_crate_path = quote::quote! { #crate_path::ext::scale_decode }.to_string();
Expand Down

0 comments on commit 985a46c

Please sign in to comment.