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
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ use std::{
path::PathBuf,
};
use structopt::StructOpt;
use subxt_codegen::GeneratedTypeDerives;
use syn::{
punctuated::Punctuated,
Ident,
__private::Span,
Copy link
Contributor

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.

};

/// Utilities for working with substrate metadata for subxt.
#[derive(Debug, StructOpt)]
Expand Down Expand Up @@ -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>,
},
}

Expand Down Expand Up @@ -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")
Expand All @@ -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(())
}
}
Expand All @@ -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();
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() :)

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(())
}