Tidy subxt-codegen crate interface (#1225)

* first pass tidying codegen crate interface

* fix a codegen test

* macro: keep error spans

* clippy

* fix doc example

* removecommented-out code
This commit is contained in:
James Wilson
2023-10-27 16:35:18 +01:00
committed by GitHub
parent 98170c8fdb
commit 2b21f8dc8c
29 changed files with 503 additions and 554 deletions
+2 -2
View File
@@ -4,7 +4,7 @@
use crate::error::CodegenError;
use super::{CratePath, Derives, Field, TypeDefParameters, TypeGenerator, TypeParameter, TypePath};
use super::{Derives, Field, TypeDefParameters, TypeGenerator, TypeParameter, TypePath};
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use scale_info::{form::PortableForm, Type, TypeDef, TypeDefPrimitive};
@@ -36,7 +36,7 @@ impl CompositeDef {
field_visibility: Option<syn::Visibility>,
type_gen: &TypeGenerator,
docs: &[String],
crate_path: &CratePath,
crate_path: &syn::Path,
) -> Result<Self, CodegenError> {
let mut derives = type_gen.type_derives(ty)?;
let fields: Vec<_> = fields_def.field_types().collect();
+3 -4
View File
@@ -2,7 +2,6 @@
// 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, Path};
use std::collections::{HashMap, HashSet};
@@ -35,7 +34,7 @@ impl DerivesRegistry {
///
/// The `crate_path` denotes the `subxt` crate access path in the
/// generated code.
pub fn with_default_derives(crate_path: &CratePath) -> Self {
pub fn with_default_derives(crate_path: &syn::Path) -> Self {
Self {
default_derives: Derives::with_defaults(crate_path),
specific_type_derives: Default::default(),
@@ -117,7 +116,7 @@ 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 with_defaults(crate_path: &CratePath) -> Self {
pub fn with_defaults(crate_path: &syn::Path) -> Self {
let mut derives = HashSet::new();
let mut attributes = HashSet::new();
@@ -148,7 +147,7 @@ impl Derives {
}
/// Add `#crate_path::ext::codec::CompactAs` to the derives.
pub fn insert_codec_compact_as(&mut self, crate_path: &CratePath) {
pub fn insert_codec_compact_as(&mut self, crate_path: &syn::Path) {
self.insert_derive(parse_quote!(#crate_path::ext::codec::CompactAs));
}
+2 -54
View File
@@ -41,7 +41,7 @@ pub struct TypeGenerator<'a> {
/// Set of derives with which to annotate generated types.
derives: DerivesRegistry,
/// The `subxt` crate access path in the generated code.
crate_path: CratePath,
crate_path: syn::Path,
/// True if codegen should generate the documentation for the API.
should_gen_docs: bool,
}
@@ -53,7 +53,7 @@ impl<'a> TypeGenerator<'a> {
root_mod: &'static str,
type_substitutes: TypeSubstitutes,
derives: DerivesRegistry,
crate_path: CratePath,
crate_path: syn::Path,
should_gen_docs: bool,
) -> Self {
let root_mod_ident = Ident::new(root_mod, Span::call_site());
@@ -334,55 +334,3 @@ impl Module {
&self.root_mod
}
}
/// A newtype wrapper which stores the path to the Subxt crate.
#[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::parse_str(crate_path).unwrap_or_else(|err| {
panic!("failed converting {crate_path:?} to `syn::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(),
}
}
}
+2 -2
View File
@@ -2,7 +2,7 @@
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
use crate::{error::TypeSubstitutionError, CratePath};
use crate::error::TypeSubstitutionError;
use std::{borrow::Borrow, collections::HashMap};
use syn::{parse_quote, spanned::Spanned as _};
@@ -55,7 +55,7 @@ impl TypeSubstitutes {
///
/// The `crate_path` denotes the `subxt` crate access path in the
/// generated code.
pub fn with_default_substitutes(crate_path: &CratePath) -> Self {
pub fn with_default_substitutes(crate_path: &syn::Path) -> Self {
// Some hardcoded default type substitutes, can be overridden by user
let defaults = [
(
+22 -22
View File
@@ -34,7 +34,7 @@ fn generate_struct_with_primitives() {
registry.register_type(&meta_type::<S>());
let portable_types: PortableRegistry = registry.into();
let crate_path = "::subxt_path".into();
let crate_path = syn::parse_str("::subxt_path").unwrap();
let type_gen = TypeGenerator::new(
&portable_types,
"root",
@@ -86,7 +86,7 @@ fn generate_struct_with_a_struct_field() {
registry.register_type(&meta_type::<Parent>());
let portable_types: PortableRegistry = registry.into();
let crate_path = "::subxt_path".into();
let crate_path = syn::parse_str("::subxt_path").unwrap();
let type_gen = TypeGenerator::new(
&portable_types,
"root",
@@ -140,7 +140,7 @@ fn generate_tuple_struct() {
registry.register_type(&meta_type::<Parent>());
let portable_types: PortableRegistry = registry.into();
let crate_path = "::subxt_path".into();
let crate_path = syn::parse_str("::subxt_path").unwrap();
let type_gen = TypeGenerator::new(
&portable_types,
"root",
@@ -231,7 +231,7 @@ fn derive_compact_as_for_uint_wrapper_structs() {
registry.register_type(&meta_type::<TSu128>());
let portable_types: PortableRegistry = registry.into();
let crate_path = "::subxt_path".into();
let crate_path = syn::parse_str("::subxt_path").unwrap();
let type_gen = TypeGenerator::new(
&portable_types,
"root",
@@ -328,7 +328,7 @@ fn generate_enum() {
registry.register_type(&meta_type::<E>());
let portable_types: PortableRegistry = registry.into();
let crate_path = "::subxt_path".into();
let crate_path = syn::parse_str("::subxt_path").unwrap();
let type_gen = TypeGenerator::new(
&portable_types,
"root",
@@ -392,7 +392,7 @@ fn compact_fields() {
registry.register_type(&meta_type::<E>());
let portable_types: PortableRegistry = registry.into();
let crate_path = "::subxt_path".into();
let crate_path = syn::parse_str("::subxt_path").unwrap();
let type_gen = TypeGenerator::new(
&portable_types,
"root",
@@ -460,7 +460,7 @@ fn compact_generic_parameter() {
registry.register_type(&meta_type::<S>());
let portable_types: PortableRegistry = registry.into();
let crate_path = "::subxt_path".into();
let crate_path = syn::parse_str("::subxt_path").unwrap();
let type_gen = TypeGenerator::new(
&portable_types,
"root",
@@ -507,7 +507,7 @@ fn generate_array_field() {
registry.register_type(&meta_type::<S>());
let portable_types: PortableRegistry = registry.into();
let crate_path = "::subxt_path".into();
let crate_path = syn::parse_str("::subxt_path").unwrap();
let type_gen = TypeGenerator::new(
&portable_types,
"root",
@@ -550,7 +550,7 @@ fn option_fields() {
registry.register_type(&meta_type::<S>());
let portable_types: PortableRegistry = registry.into();
let crate_path = "::subxt_path".into();
let crate_path = syn::parse_str("::subxt_path").unwrap();
let type_gen = TypeGenerator::new(
&portable_types,
"root",
@@ -596,7 +596,7 @@ fn box_fields_struct() {
registry.register_type(&meta_type::<S>());
let portable_types: PortableRegistry = registry.into();
let crate_path = "::subxt_path".into();
let crate_path = syn::parse_str("::subxt_path").unwrap();
let type_gen = TypeGenerator::new(
&portable_types,
"root",
@@ -642,7 +642,7 @@ fn box_fields_enum() {
registry.register_type(&meta_type::<E>());
let portable_types: PortableRegistry = registry.into();
let crate_path = "::subxt_path".into();
let crate_path = syn::parse_str("::subxt_path").unwrap();
let type_gen = TypeGenerator::new(
&portable_types,
"root",
@@ -688,7 +688,7 @@ fn range_fields() {
registry.register_type(&meta_type::<S>());
let portable_types: PortableRegistry = registry.into();
let crate_path = "::subxt_path".into();
let crate_path = syn::parse_str("::subxt_path").unwrap();
let type_gen = TypeGenerator::new(
&portable_types,
"root",
@@ -738,7 +738,7 @@ fn generics() {
registry.register_type(&meta_type::<Bar>());
let portable_types: PortableRegistry = registry.into();
let crate_path = "::subxt_path".into();
let crate_path = syn::parse_str("::subxt_path").unwrap();
let type_gen = TypeGenerator::new(
&portable_types,
"root",
@@ -795,7 +795,7 @@ fn generics_nested() {
registry.register_type(&meta_type::<Bar<bool>>());
let portable_types: PortableRegistry = registry.into();
let crate_path = "::subxt_path".into();
let crate_path = syn::parse_str("::subxt_path").unwrap();
let type_gen = TypeGenerator::new(
&portable_types,
"root",
@@ -852,7 +852,7 @@ fn generate_bitvec() {
registry.register_type(&meta_type::<S>());
let portable_types: PortableRegistry = registry.into();
let crate_path = "::subxt_path".into();
let crate_path = syn::parse_str("::subxt_path").unwrap();
let type_gen = TypeGenerator::new(
&portable_types,
"root",
@@ -911,7 +911,7 @@ fn generics_with_alias_adds_phantom_data_marker() {
registry.register_type(&meta_type::<UnnamedFields<bool, bool>>());
let portable_types: PortableRegistry = registry.into();
let crate_path = "::subxt_path".into();
let crate_path = syn::parse_str("::subxt_path").unwrap();
let type_gen = TypeGenerator::new(
&portable_types,
"root",
@@ -982,7 +982,7 @@ fn modules() {
registry.register_type(&meta_type::<m::c::Foo>());
let portable_types: PortableRegistry = registry.into();
let crate_path = "::subxt_path".into();
let crate_path = syn::parse_str("::subxt_path").unwrap();
let type_gen = TypeGenerator::new(
&portable_types,
"root",
@@ -1051,7 +1051,7 @@ fn dont_force_struct_names_camel_case() {
registry.register_type(&meta_type::<AB>());
let portable_types: PortableRegistry = registry.into();
let crate_path = "::subxt_path".into();
let crate_path = syn::parse_str("::subxt_path").unwrap();
let type_gen = TypeGenerator::new(
&portable_types,
"root",
@@ -1094,7 +1094,7 @@ fn apply_user_defined_derives_for_all_types() {
registry.register_type(&meta_type::<A>());
let portable_types: PortableRegistry = registry.into();
let crate_path = "::subxt_path".into();
let crate_path = syn::parse_str("::subxt_path").unwrap();
// configure derives
let mut derives = DerivesRegistry::with_default_derives(&crate_path);
derives.extend_for_all(
@@ -1156,7 +1156,7 @@ fn apply_user_defined_derives_for_specific_types() {
registry.register_type(&meta_type::<A>());
let portable_types: PortableRegistry = registry.into();
let crate_path = "::subxt_path".into();
let crate_path = syn::parse_str("::subxt_path").unwrap();
// configure derives
let mut derives = DerivesRegistry::with_default_derives(&crate_path);
// for all types
@@ -1233,7 +1233,7 @@ fn opt_out_from_default_derives() {
registry.register_type(&meta_type::<A>());
let portable_types: PortableRegistry = registry.into();
let crate_path = "::subxt_path".into();
let crate_path = syn::parse_str("::subxt_path").unwrap();
// configure derives
let mut derives = DerivesRegistry::new();
derives.extend_for_all(
@@ -1293,7 +1293,7 @@ fn opt_out_from_default_substitutes() {
registry.register_type(&meta_type::<S>());
let portable_types: PortableRegistry = registry.into();
let crate_path = "::subxt_path".into();
let crate_path = syn::parse_str("::subxt_path").unwrap();
let type_gen = TypeGenerator::new(
&portable_types,
"root",
+2 -3
View File
@@ -5,8 +5,7 @@
use crate::error::CodegenError;
use super::{
CompositeDef, CompositeDefFields, CratePath, Derives, TypeDefParameters, TypeGenerator,
TypeParameter,
CompositeDef, CompositeDefFields, Derives, TypeDefParameters, TypeGenerator, TypeParameter,
};
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
@@ -34,7 +33,7 @@ impl TypeDefGen {
pub fn from_type(
ty: &Type<PortableForm>,
type_gen: &TypeGenerator,
crate_path: &CratePath,
crate_path: &syn::Path,
should_gen_docs: bool,
) -> Result<Self, CodegenError> {
let derives = type_gen.type_derives(ty)?;
+2 -4
View File
@@ -2,8 +2,6 @@
// 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};
use quote::format_ident;
use scale_info::{form::PortableForm, Path, TypeDefPrimitive};
@@ -121,12 +119,12 @@ pub enum TypePathType {
Compact {
inner: Box<TypePath>,
is_field: bool,
crate_path: CratePath,
crate_path: syn::Path,
},
BitVec {
bit_order_type: Box<TypePath>,
bit_store_type: Box<TypePath>,
crate_path: CratePath,
crate_path: syn::Path,
},
}