mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 12:17:58 +00:00
Allow specifying the subxt crate path for generated code (#664)
* Allow specifying the `subxt` crate path for generated code * Make `clippy` happy * Add documentation * Improve optics * Remove custom crate path test * Implement comments * Update comment * Make `crate_path` property instead of argument * Remove unnecessary derives * Remove `Default` impls in favor of explicit constructors * Remove unnecessary `into` * Update codegen/src/types/mod.rs Co-authored-by: Andrew Jones <ascjones@gmail.com> Co-authored-by: Andrew Jones <ascjones@gmail.com>
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
// see LICENSE for license details.
|
||||
|
||||
use super::{
|
||||
CratePath,
|
||||
Derives,
|
||||
Field,
|
||||
TypeDefParameters,
|
||||
@@ -41,6 +42,7 @@ pub struct CompositeDef {
|
||||
|
||||
impl CompositeDef {
|
||||
/// Construct a definition which will generate code for a standalone `struct`.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn struct_def(
|
||||
ty: &Type<PortableForm>,
|
||||
ident: &str,
|
||||
@@ -49,6 +51,7 @@ impl CompositeDef {
|
||||
field_visibility: Option<syn::Visibility>,
|
||||
type_gen: &TypeGenerator,
|
||||
docs: &[String],
|
||||
crate_path: &CratePath,
|
||||
) -> Self {
|
||||
let mut derives = type_gen.type_derives(ty);
|
||||
let fields: Vec<_> = fields_def.field_types().collect();
|
||||
@@ -73,7 +76,7 @@ impl CompositeDef {
|
||||
| TypeDefPrimitive::U128
|
||||
)
|
||||
) {
|
||||
derives.insert_codec_compact_as()
|
||||
derives.insert_codec_compact_as(crate_path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
|
||||
// see LICENSE for license details.
|
||||
|
||||
use crate::CratePath;
|
||||
use syn::{
|
||||
parse_quote,
|
||||
punctuated::Punctuated,
|
||||
@@ -13,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)
|
||||
@@ -30,11 +42,12 @@ impl DerivesRegistry {
|
||||
&mut self,
|
||||
ty: syn::TypePath,
|
||||
derives: impl IntoIterator<Item = syn::Path>,
|
||||
crate_path: &CratePath,
|
||||
) {
|
||||
let type_derives = self
|
||||
.specific_type_derives
|
||||
.entry(ty)
|
||||
.or_insert_with(Derives::default);
|
||||
.or_insert_with(|| Derives::new(crate_path));
|
||||
type_derives.derives.extend(derives)
|
||||
}
|
||||
|
||||
@@ -69,9 +82,19 @@ impl FromIterator<syn::Path> for Derives {
|
||||
}
|
||||
|
||||
impl Derives {
|
||||
/// Add `::subxt::ext::codec::CompactAs` to the derives.
|
||||
pub fn insert_codec_compact_as(&mut self) {
|
||||
self.insert(parse_quote!(::subxt::ext::codec::CompactAs));
|
||||
/// 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));
|
||||
}
|
||||
|
||||
pub fn append(&mut self, derives: impl Iterator<Item = syn::Path>) {
|
||||
@@ -85,16 +108,6 @@ impl Derives {
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Derives {
|
||||
fn default() -> Self {
|
||||
let mut derives = HashSet::new();
|
||||
derives.insert(syn::parse_quote!(::subxt::ext::codec::Encode));
|
||||
derives.insert(syn::parse_quote!(::subxt::ext::codec::Decode));
|
||||
derives.insert(syn::parse_quote!(Debug));
|
||||
Self { derives }
|
||||
}
|
||||
}
|
||||
|
||||
impl quote::ToTokens for Derives {
|
||||
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
|
||||
if !self.derives.is_empty() {
|
||||
|
||||
@@ -10,6 +10,7 @@ mod type_def;
|
||||
mod type_def_params;
|
||||
mod type_path;
|
||||
|
||||
use darling::FromMeta;
|
||||
use proc_macro2::{
|
||||
Ident,
|
||||
Span,
|
||||
@@ -63,6 +64,8 @@ pub struct TypeGenerator<'a> {
|
||||
type_substitutes: HashMap<String, syn::TypePath>,
|
||||
/// Set of derives with which to annotate generated types.
|
||||
derives: DerivesRegistry,
|
||||
/// The `subxt` crate access path in the generated code.
|
||||
crate_path: CratePath,
|
||||
}
|
||||
|
||||
impl<'a> TypeGenerator<'a> {
|
||||
@@ -72,6 +75,7 @@ impl<'a> TypeGenerator<'a> {
|
||||
root_mod: &'static str,
|
||||
type_substitutes: HashMap<String, syn::TypePath>,
|
||||
derives: DerivesRegistry,
|
||||
crate_path: CratePath,
|
||||
) -> Self {
|
||||
let root_mod_ident = Ident::new(root_mod, Span::call_site());
|
||||
Self {
|
||||
@@ -79,6 +83,7 @@ impl<'a> TypeGenerator<'a> {
|
||||
type_registry,
|
||||
type_substitutes,
|
||||
derives,
|
||||
crate_path,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,9 +131,10 @@ impl<'a> TypeGenerator<'a> {
|
||||
.or_insert_with(|| Module::new(mod_ident, root_mod_ident.clone()));
|
||||
|
||||
if path.len() == 1 {
|
||||
child_mod
|
||||
.types
|
||||
.insert(ty.path().clone(), TypeDefGen::from_type(ty, self));
|
||||
child_mod.types.insert(
|
||||
ty.path().clone(),
|
||||
TypeDefGen::from_type(ty, self, &self.crate_path),
|
||||
);
|
||||
} else {
|
||||
self.insert_type(ty, id, path[1..].to_vec(), root_mod_ident, child_mod)
|
||||
}
|
||||
@@ -279,6 +285,7 @@ impl<'a> TypeGenerator<'a> {
|
||||
parent_type_params,
|
||||
)),
|
||||
is_field,
|
||||
crate_path: self.crate_path.clone(),
|
||||
}
|
||||
}
|
||||
TypeDef::BitSequence(bitseq) => {
|
||||
@@ -293,6 +300,7 @@ impl<'a> TypeGenerator<'a> {
|
||||
false,
|
||||
parent_type_params,
|
||||
)),
|
||||
crate_path: self.crate_path.clone(),
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -358,3 +366,57 @@ impl Module {
|
||||
&self.name
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CratePath(syn::Path);
|
||||
|
||||
impl CratePath {
|
||||
/// Create a new `CratePath` from a `syn::Path`.
|
||||
pub fn new(path: syn::Path) -> Self {
|
||||
Self(path)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for CratePath {
|
||||
fn default() -> Self {
|
||||
Self(syn::parse_quote!(::subxt))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<syn::Path> for CratePath {
|
||||
fn from(path: syn::Path) -> Self {
|
||||
CratePath::new(path)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for CratePath {
|
||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||
self.0.to_tokens(tokens)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for CratePath {
|
||||
fn from(crate_path: &str) -> Self {
|
||||
Self(syn::Path::from_string(crate_path).unwrap_or_else(|err| {
|
||||
panic!(
|
||||
"failed converting {:?} to `syn::Path`: {:?}",
|
||||
crate_path, err
|
||||
);
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for CratePath {
|
||||
fn from(crate_path: String) -> Self {
|
||||
CratePath::from(crate_path.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Option<String>> for CratePath {
|
||||
fn from(maybe_crate_path: Option<String>) -> Self {
|
||||
match maybe_crate_path {
|
||||
None => CratePath::default(),
|
||||
Some(crate_path) => crate_path.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+102
-72
@@ -43,7 +43,8 @@ fn generate_struct_with_primitives() {
|
||||
&portable_types,
|
||||
"root",
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
DerivesRegistry::new(&"::subxt_path".into()),
|
||||
"::subxt_path".into(),
|
||||
);
|
||||
let types = type_gen.generate_types_mod();
|
||||
let tests_mod = get_mod(&types, MOD_PATH).unwrap();
|
||||
@@ -54,7 +55,7 @@ fn generate_struct_with_primitives() {
|
||||
pub mod tests {
|
||||
use super::root;
|
||||
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct S {
|
||||
pub a: ::core::primitive::bool,
|
||||
pub b: ::core::primitive::u32,
|
||||
@@ -89,7 +90,8 @@ fn generate_struct_with_a_struct_field() {
|
||||
&portable_types,
|
||||
"root",
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
DerivesRegistry::new(&"::subxt_path".into()),
|
||||
"::subxt_path".into(),
|
||||
);
|
||||
let types = type_gen.generate_types_mod();
|
||||
let tests_mod = get_mod(&types, MOD_PATH).unwrap();
|
||||
@@ -100,12 +102,12 @@ fn generate_struct_with_a_struct_field() {
|
||||
pub mod tests {
|
||||
use super::root;
|
||||
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct Child {
|
||||
pub a: ::core::primitive::i32,
|
||||
}
|
||||
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct Parent {
|
||||
pub a: ::core::primitive::bool,
|
||||
pub b: root::subxt_codegen::types::tests::Child,
|
||||
@@ -134,7 +136,8 @@ fn generate_tuple_struct() {
|
||||
&portable_types,
|
||||
"root",
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
DerivesRegistry::new(&"::subxt_path".into()),
|
||||
"::subxt_path".into(),
|
||||
);
|
||||
let types = type_gen.generate_types_mod();
|
||||
let tests_mod = get_mod(&types, MOD_PATH).unwrap();
|
||||
@@ -145,10 +148,10 @@ fn generate_tuple_struct() {
|
||||
pub mod tests {
|
||||
use super::root;
|
||||
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct Child(pub ::core::primitive::i32,);
|
||||
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct Parent(pub ::core::primitive::bool, pub root::subxt_codegen::types::tests::Child,);
|
||||
}
|
||||
}
|
||||
@@ -216,7 +219,8 @@ fn derive_compact_as_for_uint_wrapper_structs() {
|
||||
&portable_types,
|
||||
"root",
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
DerivesRegistry::new(&"::subxt_path".into()),
|
||||
"::subxt_path".into(),
|
||||
);
|
||||
let types = type_gen.generate_types_mod();
|
||||
let tests_mod = get_mod(&types, MOD_PATH).unwrap();
|
||||
@@ -227,34 +231,34 @@ fn derive_compact_as_for_uint_wrapper_structs() {
|
||||
pub mod tests {
|
||||
use super::root;
|
||||
|
||||
#[derive(::subxt::ext::codec::CompactAs, ::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::CompactAs, ::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct Su128 { pub a: ::core::primitive::u128, }
|
||||
|
||||
#[derive(::subxt::ext::codec::CompactAs, ::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::CompactAs, ::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct Su16 { pub a: ::core::primitive::u16, }
|
||||
|
||||
#[derive(::subxt::ext::codec::CompactAs, ::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::CompactAs, ::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct Su32 { pub a: ::core::primitive::u32, }
|
||||
|
||||
#[derive(::subxt::ext::codec::CompactAs, ::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::CompactAs, ::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct Su64 { pub a: ::core::primitive::u64, }
|
||||
|
||||
#[derive(::subxt::ext::codec::CompactAs, ::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::CompactAs, ::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct Su8 { pub a: ::core::primitive::u8, }
|
||||
|
||||
#[derive(::subxt::ext::codec::CompactAs, ::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::CompactAs, ::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct TSu128(pub ::core::primitive::u128,);
|
||||
|
||||
#[derive(::subxt::ext::codec::CompactAs, ::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::CompactAs, ::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct TSu16(pub ::core::primitive::u16,);
|
||||
|
||||
#[derive(::subxt::ext::codec::CompactAs, ::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::CompactAs, ::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct TSu32(pub ::core::primitive::u32,);
|
||||
|
||||
#[derive(::subxt::ext::codec::CompactAs, ::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::CompactAs, ::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct TSu64(pub ::core::primitive::u64,);
|
||||
|
||||
#[derive(::subxt::ext::codec::CompactAs, ::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::CompactAs, ::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct TSu8(pub ::core::primitive::u8,);
|
||||
}
|
||||
}
|
||||
@@ -280,7 +284,8 @@ fn generate_enum() {
|
||||
&portable_types,
|
||||
"root",
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
DerivesRegistry::new(&"::subxt_path".into()),
|
||||
"::subxt_path".into(),
|
||||
);
|
||||
let types = type_gen.generate_types_mod();
|
||||
let tests_mod = get_mod(&types, MOD_PATH).unwrap();
|
||||
@@ -290,7 +295,7 @@ fn generate_enum() {
|
||||
quote! {
|
||||
pub mod tests {
|
||||
use super::root;
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub enum E {
|
||||
# [codec (index = 0)]
|
||||
A,
|
||||
@@ -338,7 +343,8 @@ fn compact_fields() {
|
||||
&portable_types,
|
||||
"root",
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
DerivesRegistry::new(&"::subxt_path".into()),
|
||||
"::subxt_path".into(),
|
||||
);
|
||||
let types = type_gen.generate_types_mod();
|
||||
let tests_mod = get_mod(&types, MOD_PATH).unwrap();
|
||||
@@ -348,7 +354,7 @@ fn compact_fields() {
|
||||
quote! {
|
||||
pub mod tests {
|
||||
use super::root;
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub enum E {
|
||||
# [codec (index = 0)]
|
||||
A {
|
||||
@@ -359,12 +365,12 @@ fn compact_fields() {
|
||||
B( #[codec(compact)] ::core::primitive::u32,),
|
||||
}
|
||||
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct S {
|
||||
#[codec(compact)] pub a: ::core::primitive::u32,
|
||||
}
|
||||
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct TupleStruct(#[codec(compact)] pub ::core::primitive::u32,);
|
||||
}
|
||||
}
|
||||
@@ -394,7 +400,8 @@ fn compact_generic_parameter() {
|
||||
&portable_types,
|
||||
"root",
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
DerivesRegistry::new(&"::subxt_path".into()),
|
||||
"::subxt_path".into(),
|
||||
);
|
||||
let types = type_gen.generate_types_mod();
|
||||
let tests_mod = get_mod(&types, MOD_PATH).unwrap();
|
||||
@@ -405,13 +412,13 @@ fn compact_generic_parameter() {
|
||||
pub mod tests {
|
||||
use super::root;
|
||||
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct S {
|
||||
pub a: ::core::option::Option<::subxt::ext::codec::Compact<::core::primitive::u128> >,
|
||||
pub nested: ::core::option::Option<::core::result::Result<::subxt::ext::codec::Compact<::core::primitive::u128>, ::core::primitive::u8 > >,
|
||||
pub vector: ::std::vec::Vec<::subxt::ext::codec::Compact<::core::primitive::u16> >,
|
||||
pub array: [::subxt::ext::codec::Compact<::core::primitive::u8>; 32usize],
|
||||
pub tuple: (::subxt::ext::codec::Compact<::core::primitive::u8>, ::subxt::ext::codec::Compact<::core::primitive::u16>,),
|
||||
pub a: ::core::option::Option<::subxt_path::ext::codec::Compact<::core::primitive::u128> >,
|
||||
pub nested: ::core::option::Option<::core::result::Result<::subxt_path::ext::codec::Compact<::core::primitive::u128>, ::core::primitive::u8 > >,
|
||||
pub vector: ::std::vec::Vec<::subxt_path::ext::codec::Compact<::core::primitive::u16> >,
|
||||
pub array: [::subxt_path::ext::codec::Compact<::core::primitive::u8>; 32usize],
|
||||
pub tuple: (::subxt_path::ext::codec::Compact<::core::primitive::u8>, ::subxt_path::ext::codec::Compact<::core::primitive::u16>,),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -435,7 +442,8 @@ fn generate_array_field() {
|
||||
&portable_types,
|
||||
"root",
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
DerivesRegistry::new(&"::subxt_path".into()),
|
||||
"::subxt_path".into(),
|
||||
);
|
||||
let types = type_gen.generate_types_mod();
|
||||
let tests_mod = get_mod(&types, MOD_PATH).unwrap();
|
||||
@@ -445,7 +453,7 @@ fn generate_array_field() {
|
||||
quote! {
|
||||
pub mod tests {
|
||||
use super::root;
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct S {
|
||||
pub a: [::core::primitive::u8; 32usize],
|
||||
}
|
||||
@@ -472,7 +480,8 @@ fn option_fields() {
|
||||
&portable_types,
|
||||
"root",
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
DerivesRegistry::new(&"::subxt_path".into()),
|
||||
"::subxt_path".into(),
|
||||
);
|
||||
let types = type_gen.generate_types_mod();
|
||||
let tests_mod = get_mod(&types, MOD_PATH).unwrap();
|
||||
@@ -482,7 +491,7 @@ fn option_fields() {
|
||||
quote! {
|
||||
pub mod tests {
|
||||
use super::root;
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct S {
|
||||
pub a: ::core::option::Option<::core::primitive::bool>,
|
||||
pub b: ::core::option::Option<::core::primitive::u32>,
|
||||
@@ -512,7 +521,8 @@ fn box_fields_struct() {
|
||||
&portable_types,
|
||||
"root",
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
DerivesRegistry::new(&"::subxt_path".into()),
|
||||
"::subxt_path".into(),
|
||||
);
|
||||
let types = type_gen.generate_types_mod();
|
||||
let tests_mod = get_mod(&types, MOD_PATH).unwrap();
|
||||
@@ -522,7 +532,7 @@ fn box_fields_struct() {
|
||||
quote! {
|
||||
pub mod tests {
|
||||
use super::root;
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct S {
|
||||
pub a: ::std::boxed::Box<::core::primitive::bool>,
|
||||
pub b: ::std::boxed::Box<::core::primitive::u32>,
|
||||
@@ -552,7 +562,8 @@ fn box_fields_enum() {
|
||||
&portable_types,
|
||||
"root",
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
DerivesRegistry::new(&"::subxt_path".into()),
|
||||
"::subxt_path".into(),
|
||||
);
|
||||
let types = type_gen.generate_types_mod();
|
||||
let tests_mod = get_mod(&types, MOD_PATH).unwrap();
|
||||
@@ -562,7 +573,7 @@ fn box_fields_enum() {
|
||||
quote! {
|
||||
pub mod tests {
|
||||
use super::root;
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub enum E {
|
||||
# [codec (index = 0)]
|
||||
A(::std::boxed::Box<::core::primitive::bool>,),
|
||||
@@ -592,7 +603,8 @@ fn range_fields() {
|
||||
&portable_types,
|
||||
"root",
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
DerivesRegistry::new(&"::subxt_path".into()),
|
||||
"::subxt_path".into(),
|
||||
);
|
||||
let types = type_gen.generate_types_mod();
|
||||
let tests_mod = get_mod(&types, MOD_PATH).unwrap();
|
||||
@@ -602,7 +614,7 @@ fn range_fields() {
|
||||
quote! {
|
||||
pub mod tests {
|
||||
use super::root;
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct S {
|
||||
pub a: ::core::ops::Range<::core::primitive::u32>,
|
||||
pub b: ::core::ops::RangeInclusive<::core::primitive::u32>,
|
||||
@@ -636,7 +648,8 @@ fn generics() {
|
||||
&portable_types,
|
||||
"root",
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
DerivesRegistry::new(&"::subxt_path".into()),
|
||||
"::subxt_path".into(),
|
||||
);
|
||||
let types = type_gen.generate_types_mod();
|
||||
let tests_mod = get_mod(&types, MOD_PATH).unwrap();
|
||||
@@ -646,12 +659,12 @@ fn generics() {
|
||||
quote! {
|
||||
pub mod tests {
|
||||
use super::root;
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct Bar {
|
||||
pub b: root::subxt_codegen::types::tests::Foo<::core::primitive::u32>,
|
||||
pub c: root::subxt_codegen::types::tests::Foo<::core::primitive::u8>,
|
||||
}
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct Foo<_0> {
|
||||
pub a: _0,
|
||||
}
|
||||
@@ -684,7 +697,8 @@ fn generics_nested() {
|
||||
&portable_types,
|
||||
"root",
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
DerivesRegistry::new(&"::subxt_path".into()),
|
||||
"::subxt_path".into(),
|
||||
);
|
||||
let types = type_gen.generate_types_mod();
|
||||
let tests_mod = get_mod(&types, MOD_PATH).unwrap();
|
||||
@@ -694,12 +708,12 @@ fn generics_nested() {
|
||||
quote! {
|
||||
pub mod tests {
|
||||
use super::root;
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct Bar<_0> {
|
||||
pub b: root::subxt_codegen::types::tests::Foo<_0, ::core::primitive::u32>,
|
||||
}
|
||||
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct Foo<_0, _1> {
|
||||
pub a: _0,
|
||||
pub b: ::core::option::Option<(_0, _1,)>,
|
||||
@@ -735,7 +749,8 @@ fn generate_bitvec() {
|
||||
&portable_types,
|
||||
"root",
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
DerivesRegistry::new(&"::subxt_path".into()),
|
||||
"::subxt_path".into(),
|
||||
);
|
||||
let types = type_gen.generate_types_mod();
|
||||
let tests_mod = get_mod(&types, MOD_PATH).unwrap();
|
||||
@@ -745,10 +760,10 @@ fn generate_bitvec() {
|
||||
quote! {
|
||||
pub mod tests {
|
||||
use super::root;
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct S {
|
||||
pub lsb: ::subxt::ext::bitvec::vec::BitVec<::core::primitive::u8, root::bitvec::order::Lsb0>,
|
||||
pub msb: ::subxt::ext::bitvec::vec::BitVec<::core::primitive::u16, root::bitvec::order::Msb0>,
|
||||
pub lsb: ::subxt_path::ext::bitvec::vec::BitVec<::core::primitive::u8, root::bitvec::order::Lsb0>,
|
||||
pub msb: ::subxt_path::ext::bitvec::vec::BitVec<::core::primitive::u16, root::bitvec::order::Msb0>,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -788,7 +803,8 @@ fn generics_with_alias_adds_phantom_data_marker() {
|
||||
&portable_types,
|
||||
"root",
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
DerivesRegistry::new(&"::subxt_path".into()),
|
||||
"::subxt_path".into(),
|
||||
);
|
||||
let types = type_gen.generate_types_mod();
|
||||
let tests_mod = get_mod(&types, MOD_PATH).unwrap();
|
||||
@@ -798,12 +814,12 @@ fn generics_with_alias_adds_phantom_data_marker() {
|
||||
quote! {
|
||||
pub mod tests {
|
||||
use super::root;
|
||||
#[derive(::subxt::ext::codec::CompactAs, ::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::CompactAs, ::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct NamedFields<_0> {
|
||||
pub b: ::core::primitive::u32,
|
||||
#[codec(skip)] pub __subxt_unused_type_params: ::core::marker::PhantomData<_0>
|
||||
}
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct UnnamedFields<_0, _1> (
|
||||
pub (::core::primitive::u32, ::core::primitive::u32,),
|
||||
#[codec(skip)] pub ::core::marker::PhantomData<(_0, _1)>
|
||||
@@ -848,7 +864,8 @@ fn modules() {
|
||||
&portable_types,
|
||||
"root",
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
DerivesRegistry::new(&"::subxt_path".into()),
|
||||
"::subxt_path".into(),
|
||||
);
|
||||
let types = type_gen.generate_types_mod();
|
||||
let tests_mod = get_mod(&types, MOD_PATH).unwrap();
|
||||
@@ -866,20 +883,20 @@ fn modules() {
|
||||
pub mod b {
|
||||
use super::root;
|
||||
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct Bar {
|
||||
pub a: root::subxt_codegen::types::tests::m::a::Foo,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct Foo;
|
||||
}
|
||||
|
||||
pub mod c {
|
||||
use super::root;
|
||||
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct Foo {
|
||||
pub a: root::subxt_codegen::types::tests::m::a::b::Bar,
|
||||
}
|
||||
@@ -905,7 +922,8 @@ fn dont_force_struct_names_camel_case() {
|
||||
&portable_types,
|
||||
"root",
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
DerivesRegistry::new(&"::subxt_path".into()),
|
||||
"::subxt_path".into(),
|
||||
);
|
||||
let types = type_gen.generate_types_mod();
|
||||
let tests_mod = get_mod(&types, MOD_PATH).unwrap();
|
||||
@@ -916,7 +934,7 @@ fn dont_force_struct_names_camel_case() {
|
||||
pub mod tests {
|
||||
use super::root;
|
||||
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
|
||||
pub struct AB;
|
||||
}
|
||||
}
|
||||
@@ -939,11 +957,16 @@ fn apply_user_defined_derives_for_all_types() {
|
||||
let portable_types: PortableRegistry = registry.into();
|
||||
|
||||
// configure derives
|
||||
let mut derives = DerivesRegistry::default();
|
||||
let mut derives = DerivesRegistry::new(&"::subxt_path".into());
|
||||
derives.extend_for_all(vec![parse_quote!(Clone), parse_quote!(Eq)]);
|
||||
|
||||
let type_gen =
|
||||
TypeGenerator::new(&portable_types, "root", Default::default(), derives);
|
||||
let type_gen = TypeGenerator::new(
|
||||
&portable_types,
|
||||
"root",
|
||||
Default::default(),
|
||||
derives,
|
||||
"::subxt_path".into(),
|
||||
);
|
||||
let types = type_gen.generate_types_mod();
|
||||
let tests_mod = get_mod(&types, MOD_PATH).unwrap();
|
||||
|
||||
@@ -953,10 +976,10 @@ fn apply_user_defined_derives_for_all_types() {
|
||||
pub mod tests {
|
||||
use super::root;
|
||||
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Clone, Debug, Eq)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Clone, Debug, Eq)]
|
||||
pub struct A(pub root :: subxt_codegen :: types :: tests :: B,);
|
||||
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Clone, Debug, Eq)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Clone, Debug, Eq)]
|
||||
pub struct B;
|
||||
}
|
||||
}
|
||||
@@ -983,13 +1006,14 @@ fn apply_user_defined_derives_for_specific_types() {
|
||||
let portable_types: PortableRegistry = registry.into();
|
||||
|
||||
// configure derives
|
||||
let mut derives = DerivesRegistry::default();
|
||||
let mut derives = DerivesRegistry::new(&"::subxt_path".into());
|
||||
// for all types
|
||||
derives.extend_for_all(vec![parse_quote!(Eq)]);
|
||||
// for specific types
|
||||
derives.extend_for_type(
|
||||
parse_quote!(subxt_codegen::types::tests::B),
|
||||
vec![parse_quote!(Hash)],
|
||||
&"::subxt_path".into(),
|
||||
);
|
||||
// duplicates (in this case `Eq`) will be combined (i.e. a set union)
|
||||
derives.extend_for_type(
|
||||
@@ -999,10 +1023,16 @@ fn apply_user_defined_derives_for_specific_types() {
|
||||
parse_quote!(Ord),
|
||||
parse_quote!(PartialOrd),
|
||||
],
|
||||
&"::subxt_path".into(),
|
||||
);
|
||||
|
||||
let type_gen =
|
||||
TypeGenerator::new(&portable_types, "root", Default::default(), derives);
|
||||
let type_gen = TypeGenerator::new(
|
||||
&portable_types,
|
||||
"root",
|
||||
Default::default(),
|
||||
derives,
|
||||
"::subxt_path".into(),
|
||||
);
|
||||
let types = type_gen.generate_types_mod();
|
||||
let tests_mod = get_mod(&types, MOD_PATH).unwrap();
|
||||
|
||||
@@ -1012,13 +1042,13 @@ fn apply_user_defined_derives_for_specific_types() {
|
||||
pub mod tests {
|
||||
use super::root;
|
||||
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug, Eq)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug, Eq)]
|
||||
pub struct A(pub root :: subxt_codegen :: types :: tests :: B,);
|
||||
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug, Eq, Hash)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug, Eq, Hash)]
|
||||
pub struct B(pub root :: subxt_codegen :: types :: tests :: C,);
|
||||
|
||||
#[derive(::subxt::ext::codec::Decode, ::subxt::ext::codec::Encode, Debug, Eq, Ord, PartialOrd)]
|
||||
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug, Eq, Ord, PartialOrd)]
|
||||
pub struct C;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
use super::{
|
||||
CompositeDef,
|
||||
CompositeDefFields,
|
||||
CratePath,
|
||||
Derives,
|
||||
TypeDefParameters,
|
||||
TypeGenerator,
|
||||
@@ -40,7 +41,11 @@ pub struct TypeDefGen {
|
||||
|
||||
impl TypeDefGen {
|
||||
/// Construct a type definition for codegen from the given [`scale_info::Type`].
|
||||
pub fn from_type(ty: Type<PortableForm>, type_gen: &TypeGenerator) -> Self {
|
||||
pub fn from_type(
|
||||
ty: Type<PortableForm>,
|
||||
type_gen: &TypeGenerator,
|
||||
crate_path: &CratePath,
|
||||
) -> Self {
|
||||
let derives = type_gen.type_derives(&ty);
|
||||
|
||||
let type_params = ty
|
||||
@@ -82,6 +87,7 @@ impl TypeDefGen {
|
||||
Some(parse_quote!(pub)),
|
||||
type_gen,
|
||||
ty.docs(),
|
||||
crate_path,
|
||||
);
|
||||
TypeDefGenKind::Struct(composite_def)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
|
||||
// see LICENSE for license details.
|
||||
|
||||
use crate::CratePath;
|
||||
|
||||
use proc_macro2::{
|
||||
Ident,
|
||||
TokenStream,
|
||||
@@ -96,10 +98,12 @@ pub enum TypePathType {
|
||||
Compact {
|
||||
inner: Box<TypePath>,
|
||||
is_field: bool,
|
||||
crate_path: CratePath,
|
||||
},
|
||||
BitVec {
|
||||
bit_order_type: Box<TypePath>,
|
||||
bit_store_type: Box<TypePath>,
|
||||
crate_path: CratePath,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -170,6 +174,7 @@ impl TypePathType {
|
||||
TypePathType::BitVec {
|
||||
bit_order_type,
|
||||
bit_store_type,
|
||||
crate_path: _,
|
||||
} => {
|
||||
bit_order_type.parent_type_params(acc);
|
||||
bit_store_type.parent_type_params(acc);
|
||||
@@ -222,21 +227,26 @@ impl TypePathType {
|
||||
TypeDefPrimitive::I256 => unimplemented!("not a rust primitive"),
|
||||
})
|
||||
}
|
||||
TypePathType::Compact { inner, is_field } => {
|
||||
TypePathType::Compact {
|
||||
inner,
|
||||
is_field,
|
||||
crate_path,
|
||||
} => {
|
||||
let path = if *is_field {
|
||||
// compact fields can use the inner compact type directly and be annotated with
|
||||
// the `compact` attribute e.g. `#[codec(compact)] my_compact_field: u128`
|
||||
parse_quote! ( #inner )
|
||||
} else {
|
||||
parse_quote! ( ::subxt::ext::codec::Compact<#inner> )
|
||||
parse_quote! ( #crate_path::ext::codec::Compact<#inner> )
|
||||
};
|
||||
syn::Type::Path(path)
|
||||
}
|
||||
TypePathType::BitVec {
|
||||
bit_order_type,
|
||||
bit_store_type,
|
||||
crate_path,
|
||||
} => {
|
||||
let type_path = parse_quote! { ::subxt::ext::bitvec::vec::BitVec<#bit_store_type, #bit_order_type> };
|
||||
let type_path = parse_quote! { #crate_path::ext::bitvec::vec::BitVec<#bit_store_type, #bit_order_type> };
|
||||
syn::Type::Path(type_path)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user