Skip to content

Commit

Permalink
ident-format option
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Feb 3, 2024
1 parent ec3a8ec commit 3b822f5
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ jobs:
include:
- { rust: stable, vendor: Atmel, options: all }
- { rust: stable, vendor: Atmel, options: "" }
- { rust: stable, vendor: Freescale, options: all }
- { rust: stable, vendor: Freescale, options: "" }
- { rust: stable, vendor: Freescale, options: "--strict --atomics --ident-format register::p:" }
- { rust: stable, vendor: Freescale, options: "--ident-format register::p:" }
- { rust: stable, vendor: Fujitsu, options: "" }
- { rust: stable, vendor: Fujitsu, options: "--atomics" }
- { rust: stable, vendor: GD32, options: all }
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
## [Unreleased]

- Add `base-address-shift` config flag
- Use `PascalCase` for type idents, fix case changing bugs
- Use `PascalCase` for type idents, fix case changing bugs, add `--ident-format` (`-f`) option flag

## [v0.31.5] - 2024-01-04

Expand Down
89 changes: 87 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#![recursion_limit = "128"]

use log::{debug, error, info};
use svd2rust::util::{Case, IdentFormat};

use std::io::Write;
use std::process;
use std::{fs::File, path::Path};

use anyhow::{Context, Result};
use anyhow::{anyhow, Context, Result};
use clap::{Arg, ArgAction, Command};

use svd2rust::{
Expand All @@ -18,6 +19,8 @@ use svd2rust::{
fn parse_configs(app: Command) -> Result<Config> {
use irx_config::parsers::{cmd, toml};
use irx_config::ConfigBuilder;
let ident_formats = app.clone().get_matches();
dbg!(&ident_formats);
let irxconfig = ConfigBuilder::default()
.append_parser(cmd::ParserBuilder::new(app).exit_on_error(true).build()?)
.append_parser(
Expand All @@ -29,7 +32,81 @@ fn parse_configs(app: Command) -> Result<Config> {
)
.load()?;

irxconfig.get().map_err(Into::into)
let mut config: Config = irxconfig.get()?;
if let Some(ident_formats) = ident_formats.get_many::<String>("ident_format") {
for f in ident_formats {
let mut f = f.split(":");
dbg!(f.clone().count());
if let (Some(n), Some(p), Some(c), Some(s)) = (f.next(), f.next(), f.next(), f.next()) {
let fmts = &mut config.ident_formats;
let case = match c {
"" => None,
"p" | "pascal" | "type" => Some(Case::Pascal),
"s" | "snake" | "lower" => Some(Case::Snake),
"c" | "constant" | "upper" => Some(Case::Constant),
_ => return Err(anyhow!("Unknown case")),
};
let id_f = IdentFormat {
case,
prefix: p.into(),
suffix: s.into(),
};
dbg!(&id_f);
match n {
"field_reader" => {
fmts.field_reader = id_f;
}
"enum_name" => {
fmts.enum_name = id_f;
}
"enum_write_name" => {
fmts.enum_write_name = id_f;
}
"enum_value" => {
fmts.enum_value = id_f;
}
"interrupt" => {
fmts.interrupt = id_f;
}
"cluster" => {
fmts.cluster = id_f;
}
"cluster_accessor" => {
fmts.cluster_accessor = id_f;
}
//"cluster_mod" => {
// fmts.cluster_mod = id_f;
//},
"register" => {
fmts.register = id_f;
}
"register_spec" => {
fmts.register_spec = id_f;
}
"register_accessor" => {
fmts.register_accessor = id_f;
}
//"register_mod" => {
// fmts.register_mod = id_f;
//},
"peripheral" => {
fmts.peripheral = id_f;
}
"peripheral_sigleton" => {
fmts.peripheral_sigleton = id_f;
}
//"peripheral_mod" => {
// fmts.peripheral_mod = id_f;
//},
"peripheral_feature" => {
fmts.peripheral_feature = id_f;
}
_ => {}
}
}
}
}
Ok(config)
}

fn run() -> Result<()> {
Expand Down Expand Up @@ -119,6 +196,14 @@ fn run() -> Result<()> {
.action(ArgAction::SetTrue)
.help("Use independent cfg feature flags for each peripheral"),
)
.arg(
Arg::new("ident_format")
.long("ident-format")
.short('f')
.alias("ident_format")
.action(ArgAction::Append)
.help("Specify prefix, case and suffix for identifier type"),
)
.arg(
Arg::new("max_cluster_size")
.long("max-cluster-size")
Expand Down

0 comments on commit 3b822f5

Please sign in to comment.