mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-23 06:08:01 +00:00
Opt out from default derives and attributes (#925)
* No default derives * Clippy * Clippy * Review * Review * Typo
This commit is contained in:
committed by
GitHub
parent
97bd198951
commit
d3e20e993b
@@ -19,11 +19,19 @@ pub struct Opts {
|
||||
/// Additional derives
|
||||
#[clap(long = "derive")]
|
||||
derives: Vec<String>,
|
||||
/// Additional attributes
|
||||
#[clap(long = "attribute")]
|
||||
attributes: Vec<String>,
|
||||
/// Additional derives for a given type.
|
||||
///
|
||||
/// Example `--derive-for-type my_module::my_type=serde::Serialize`.
|
||||
#[clap(long = "derive-for-type", value_parser = derive_for_type_parser)]
|
||||
derives_for_type: Vec<(String, String)>,
|
||||
/// Additional attributes for a given type.
|
||||
///
|
||||
/// Example `--attributes-for-type my_module::my_type=#[allow(clippy::all)]`.
|
||||
#[clap(long = "attributes-for-type", value_parser = attributes_for_type_parser)]
|
||||
attributes_for_type: Vec<(String, String)>,
|
||||
/// Substitute a type for another.
|
||||
///
|
||||
/// Example `--substitute-type sp_runtime::MultiAddress<A,B>=subxt::utils::Static<::sp_runtime::MultiAddress<A,B>>`
|
||||
@@ -39,8 +47,15 @@ pub struct Opts {
|
||||
#[clap(long, action)]
|
||||
no_docs: bool,
|
||||
/// Whether to limit code generation to only runtime types.
|
||||
///
|
||||
/// Defaults to `false` (all types are generated).
|
||||
#[clap(long)]
|
||||
runtime_types_only: bool,
|
||||
/// Do not provide default trait derivations for the generated types.
|
||||
///
|
||||
/// Defaults to `false` (default trait derivations are provided).
|
||||
#[clap(long)]
|
||||
no_default_derives: bool,
|
||||
}
|
||||
|
||||
fn derive_for_type_parser(src: &str) -> Result<(String, String), String> {
|
||||
@@ -51,6 +66,14 @@ fn derive_for_type_parser(src: &str) -> Result<(String, String), String> {
|
||||
Ok((ty.to_string(), derive.to_string()))
|
||||
}
|
||||
|
||||
fn attributes_for_type_parser(src: &str) -> Result<(String, String), String> {
|
||||
let (ty, attribute) = src
|
||||
.split_once('=')
|
||||
.ok_or_else(|| String::from("Invalid pattern for `attribute-type`. It should be `type=attribute`, like `my_type=serde::#[allow(clippy::all)]`"))?;
|
||||
|
||||
Ok((ty.to_string(), attribute.to_string()))
|
||||
}
|
||||
|
||||
fn substitute_type_parser(src: &str) -> Result<(String, String), String> {
|
||||
let (from, to) = src
|
||||
.split_once('=')
|
||||
@@ -65,41 +88,71 @@ pub async fn run(opts: Opts) -> color_eyre::Result<()> {
|
||||
codegen(
|
||||
&bytes,
|
||||
opts.derives,
|
||||
opts.attributes,
|
||||
opts.derives_for_type,
|
||||
opts.attributes_for_type,
|
||||
opts.substitute_types,
|
||||
opts.crate_path,
|
||||
opts.no_docs,
|
||||
opts.runtime_types_only,
|
||||
opts.no_default_derives,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct OuterAttribute(syn::Attribute);
|
||||
|
||||
impl syn::parse::Parse for OuterAttribute {
|
||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
||||
Ok(Self(input.call(syn::Attribute::parse_outer)?[0].clone()))
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn codegen(
|
||||
metadata_bytes: &[u8],
|
||||
raw_derives: Vec<String>,
|
||||
raw_attributes: Vec<String>,
|
||||
derives_for_type: Vec<(String, String)>,
|
||||
attributes_for_type: Vec<(String, String)>,
|
||||
substitute_types: Vec<(String, String)>,
|
||||
crate_path: Option<String>,
|
||||
no_docs: bool,
|
||||
runtime_types_only: bool,
|
||||
no_default_derives: bool,
|
||||
) -> color_eyre::Result<()> {
|
||||
let item_mod = syn::parse_quote!(
|
||||
pub mod api {}
|
||||
);
|
||||
|
||||
let p = raw_derives
|
||||
let universal_derives = raw_derives
|
||||
.iter()
|
||||
.map(|raw| syn::parse_str(raw))
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let universal_attributes = raw_attributes
|
||||
.iter()
|
||||
.map(|raw| syn::parse_str(raw))
|
||||
.map(|attr: syn::Result<OuterAttribute>| attr.map(|attr| attr.0))
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
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 mut derives = if no_default_derives {
|
||||
DerivesRegistry::new()
|
||||
} else {
|
||||
DerivesRegistry::with_default_derives(&crate_path)
|
||||
};
|
||||
derives.extend_for_all(universal_derives, universal_attributes);
|
||||
|
||||
for (ty, derive) in derives_for_type {
|
||||
let ty = syn::parse_str(&ty)?;
|
||||
let derive = syn::parse_str(&derive)?;
|
||||
derives.extend_for_type(ty, std::iter::once(derive), &crate_path);
|
||||
derives.extend_for_type(ty, std::iter::once(derive), vec![]);
|
||||
}
|
||||
for (ty, attr) in attributes_for_type {
|
||||
let ty = syn::parse_str(&ty)?;
|
||||
let attribute: OuterAttribute = syn::parse_str(&attr)?;
|
||||
derives.extend_for_type(ty, vec![], std::iter::once(attribute.0));
|
||||
}
|
||||
|
||||
let mut type_substitutes = TypeSubstitutes::new(&crate_path);
|
||||
|
||||
Reference in New Issue
Block a user