-
Notifications
You must be signed in to change notification settings - Fork 245
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
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,12 @@ use std::{ | |
path::PathBuf, | ||
}; | ||
use structopt::StructOpt; | ||
use subxt_codegen::GeneratedTypeDerives; | ||
use syn::{ | ||
punctuated::Punctuated, | ||
Ident, | ||
__private::Span, | ||
}; | ||
|
||
/// Utilities for working with substrate metadata for subxt. | ||
#[derive(Debug, StructOpt)] | ||
|
@@ -70,6 +76,9 @@ enum Command { | |
/// the path to the encoded metadata file. | ||
#[structopt(short, long, parse(from_os_str))] | ||
file: Option<PathBuf>, | ||
/// Additional derives | ||
#[structopt(long = "derive")] | ||
derives: Vec<String>, | ||
}, | ||
} | ||
|
||
|
@@ -102,7 +111,7 @@ fn main() -> color_eyre::Result<()> { | |
} | ||
} | ||
} | ||
Command::Codegen { url, file } => { | ||
Command::Codegen { url, file, derives } => { | ||
if let Some(file) = file.as_ref() { | ||
if url.is_some() { | ||
eyre::bail!("specify one of `--url` or `--file` but not both") | ||
|
@@ -111,15 +120,15 @@ fn main() -> color_eyre::Result<()> { | |
let mut file = fs::File::open(file)?; | ||
let mut bytes = Vec::new(); | ||
file.read_to_end(&mut bytes)?; | ||
codegen(&mut &bytes[..])?; | ||
codegen(&mut &bytes[..], derives)?; | ||
return Ok(()) | ||
} | ||
|
||
let url = url.unwrap_or_else(|| { | ||
url::Url::parse("http://localhost:9933").expect("default url is valid") | ||
}); | ||
let (_, bytes) = fetch_metadata(&url)?; | ||
codegen(&mut &bytes[..])?; | ||
codegen(&mut &bytes[..], derives)?; | ||
Ok(()) | ||
} | ||
} | ||
|
@@ -145,13 +154,24 @@ fn fetch_metadata(url: &url::Url) -> color_eyre::Result<(String, Vec<u8>)> { | |
Ok((hex_data, bytes)) | ||
} | ||
|
||
fn codegen<I: Input>(encoded: &mut I) -> color_eyre::Result<()> { | ||
fn codegen<I: Input>( | ||
encoded: &mut I, | ||
raw_derives: Vec<String>, | ||
) -> color_eyre::Result<()> { | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. This could just be |
||
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 runtime_api = generator.generate_runtime(item_mod, derives); | ||
println!("{}", runtime_api); | ||
Ok(()) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 useproc_macro2::Span
or some other way of constructing thesyn::Path
. If you still want to construct anIdent
you should be able to usequote::format_ident
, or you can usesyn::parse_str
or similar to go straight to aPath
.