Skip to content

Commit

Permalink
Merge pull request #16 from korbin/multiple-transforms
Browse files Browse the repository at this point in the history
Allow multiple transforms for generate/extract cli commands
  • Loading branch information
Dirbaio committed Nov 12, 2023
2 parents 6069091 + 4d4e429 commit 168ce27
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct ExtractPeripheral {
peripheral: String,
/// Transforms file path
#[clap(long)]
transform: Option<String>,
transform: Vec<String>,
}

/// Extract all peripherals from SVD to YAML
Expand Down Expand Up @@ -78,7 +78,7 @@ struct Generate {
svd: String,
/// Transforms file path
#[clap(long)]
transform: Option<String>,
transform: Vec<String>,
}

/// Reformat a YAML
Expand Down Expand Up @@ -156,9 +156,13 @@ fn load_config(path: &str) -> Result<Config> {
}

fn extract_peripheral(args: ExtractPeripheral) -> Result<()> {
let config = match args.transform {
Some(s) => load_config(&s)?,
None => Config::default(),
let config = if args.transform.is_empty() {
Config::default()
} else {
args.transform
.into_iter()
.map(|s| load_config(&s))
.collect::<Result<Config>>()?
};

let svd = load_svd(&args.svd)?;
Expand Down Expand Up @@ -247,9 +251,13 @@ fn extract_all(args: ExtractAll) -> Result<()> {
}

fn gen(args: Generate) -> Result<()> {
let config = match args.transform {
Some(s) => load_config(&s)?,
None => Config::default(),
let config = if args.transform.is_empty() {
Config::default()
} else {
args.transform
.into_iter()
.map(|s| load_config(&s))
.collect::<Result<Config>>()?
};

let svd = load_svd(&args.svd)?;
Expand Down Expand Up @@ -400,6 +408,13 @@ struct Config {
transforms: Vec<chiptool::transform::Transform>,
}

impl FromIterator<Config> for Config {
fn from_iter<I: IntoIterator<Item = Config>>(iter: I) -> Self {
let transforms: Vec<_> = iter.into_iter().flat_map(|c| c.transforms).collect();
Self { transforms }
}
}

impl Default for Config {
fn default() -> Self {
Self { transforms: vec![] }
Expand Down

0 comments on commit 168ce27

Please sign in to comment.