diff --git a/codegen/src/api/mod.rs b/codegen/src/api/mod.rs index 20294cfe9b..54b0a8cee4 100644 --- a/codegen/src/api/mod.rs +++ b/codegen/src/api/mod.rs @@ -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 { @@ -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( type_gen: &TypeGenerator, diff --git a/codegen/src/lib.rs b/codegen/src/lib.rs index b71e1f048f..43fb8ccdc3 100644 --- a/codegen/src/lib.rs +++ b/codegen/src/lib.rs @@ -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; @@ -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();