-
Notifications
You must be signed in to change notification settings - Fork 262
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
Conversation
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(); |
There was a problem hiding this comment.
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
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()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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>
/
35fed03
to
2687140
Compare
cli/src/main.rs
Outdated
use subxt_codegen::GeneratedTypeDerives; | ||
use syn::{ | ||
Ident, | ||
__private::Span, |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
* Custom derive for cli * Better than better * Simplify code * Simplify code * Change follow suggestions
Add custom derive command for cli
Example