Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom derive for cli #407

Merged
merged 5 commits into from Jan 27, 2022
Merged

Conversation

fewensa
Copy link
Contributor

@fewensa fewensa commented Jan 25, 2022

Add custom derive command for cli

Example

subxt codegen --url https://pangolin-rpc.darwinia.network --derive Clone --derive Other

cli/src/main.rs Outdated
let metadata = <RuntimeMetadataPrefixed as Decode>::decode(encoded)?;
let generator = subxt_codegen::RuntimeGenerator::new(metadata);
let item_mod = syn::parse_quote!(
pub mod api {}
);
let runtime_api = generator.generate_runtime(item_mod, Default::default());

let mut p: Punctuated<syn::Path, syn::Token![,]> = Punctuated::new();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could just be Vec<syn::Path> (as far as I can see, that's all it's being used for) and then below, p.into_iter() to avoid the cloned() :)

cli/src/main.rs Outdated
Comment on lines 166 to 171
let mut p = Vec::<syn::Path>::new();
for raw in raw_derives {
p.push(Ident::new(&raw, Span::call_site()).into());
}
let mut derives = GeneratedTypeDerives::default();
derives.append(p.iter().cloned());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let mut p = Vec::<syn::Path>::new();
for raw in raw_derives {
p.push(Ident::new(&raw, Span::call_site()).into());
}
let mut derives = GeneratedTypeDerives::default();
derives.append(p.iter().cloned());
let p = raw_derives
.iter()
.map(|s| syn::parse_str(s))
.collect::<Result<Vec<_>, _>>()?;
let mut derives = GeneratedTypeDerives::default();
derives.append(p.into_iter());

Haven't checked that compiled.

Also maybe we could also just let the constructor of GeneratedTypeDerives accept a plain Vec<syn::Path> to make it easier, or even implement FromIterator<syn::Path>/

cli/src/main.rs Outdated
use subxt_codegen::GeneratedTypeDerives;
use syn::{
Ident,
__private::Span,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer not to import this since it is marked as __private. So you can either use proc_macro2::Span or some other way of constructing the syn::Path. If you still want to construct an Ident you should be able to use quote::format_ident, or you can use syn::parse_str or similar to go straight to a Path.

Copy link
Member

@ascjones ascjones left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jsdw jsdw merged commit 3dded0c into paritytech:master Jan 27, 2022
0623forbidden pushed a commit to DEIPworld/substrate-subxt that referenced this pull request Feb 15, 2022
* Custom derive for cli

* Better than better

* Simplify code

* Simplify code

* Change follow suggestions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants