diff --git a/src/main.rs b/src/main.rs index afe447b..18e68d8 100755 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,8 @@ use anyhow::{bail, Context, Result}; use chiptool::{generate, svd2ir}; use clap::Parser; use log::*; +use proc_macro2::TokenStream; +use quote::format_ident; use regex::Regex; use std::collections::HashSet; use std::fs; @@ -80,6 +82,11 @@ struct Generate { /// Transforms file path #[clap(long)] transform: Vec, + /// Path of the `common` module. + /// + /// Defaults to `crate::common`. + #[clap(long)] + pub common_module: Option, } /// Reformat a YAML @@ -122,6 +129,11 @@ struct GenBlock { /// Output YAML path #[clap(short, long)] output: String, + /// Path of the `common` module. + /// + /// Defaults to `crate::common`. + #[clap(long)] + pub common_module: Option, } fn main() -> Result<()> { @@ -277,7 +289,12 @@ fn gen(args: Generate) -> Result<()> { } let generate_opts = generate::Options { - common_module: generate::CommonModule::Builtin, + common_module: args + .common_module + .map_or(generate::CommonModule::Builtin, |path| { + let path = parse_path(path); + generate::CommonModule::External(quote::quote!(#path)) + }), }; let items = generate::render(&ir, &generate_opts).unwrap(); fs::write("lib.rs", items.to_string())?; @@ -398,7 +415,12 @@ fn gen_block(args: GenBlock) -> Result<()> { chiptool::transform::sort::Sort {}.run(&mut ir).unwrap(); let generate_opts = generate::Options { - common_module: generate::CommonModule::Builtin, + common_module: args + .common_module + .map_or(generate::CommonModule::Builtin, |path| { + let path = parse_path(path); + generate::CommonModule::External(quote::quote!(#path)) + }), }; let items = generate::render(&ir, &generate_opts).unwrap(); fs::write(&args.output, items.to_string())?; @@ -416,3 +438,8 @@ impl FromIterator for Config { Self { transforms } } } + +fn parse_path(path: String) -> TokenStream { + let segments = path.split("::").map(|s| format_ident!("{}", s)); + quote::quote!(#(#segments)::*) +}