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

Allow specifying path for the common module from the outside #33

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
31 changes: 29 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,6 +82,11 @@ struct Generate {
/// Transforms file path
#[clap(long)]
transform: Vec<String>,
/// Path of the `common` module.
///
/// Defaults to `crate::common`.
#[clap(long)]
pub common_module: Option<String>,
}

/// Reformat a YAML
Expand Down Expand Up @@ -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<String>,
}

fn main() -> Result<()> {
Expand Down Expand Up @@ -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())?;
Expand Down Expand Up @@ -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())?;
Expand All @@ -416,3 +438,8 @@ impl FromIterator<Config> for Config {
Self { transforms }
}
}

fn parse_path(path: String) -> TokenStream {
let segments = path.split("::").map(|s| format_ident!("{}", s));
quote::quote!(#(#segments)::*)
}