Skip to content

Commit 3b822f5

Browse files
committed
ident-format option
1 parent ec3a8ec commit 3b822f5

File tree

3 files changed

+90
-5
lines changed

3 files changed

+90
-5
lines changed

.github/workflows/ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ jobs:
5656
include:
5757
- { rust: stable, vendor: Atmel, options: all }
5858
- { rust: stable, vendor: Atmel, options: "" }
59-
- { rust: stable, vendor: Freescale, options: all }
60-
- { rust: stable, vendor: Freescale, options: "" }
59+
- { rust: stable, vendor: Freescale, options: "--strict --atomics --ident-format register::p:" }
60+
- { rust: stable, vendor: Freescale, options: "--ident-format register::p:" }
6161
- { rust: stable, vendor: Fujitsu, options: "" }
6262
- { rust: stable, vendor: Fujitsu, options: "--atomics" }
6363
- { rust: stable, vendor: GD32, options: all }

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
88
## [Unreleased]
99

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

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

src/main.rs

+87-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#![recursion_limit = "128"]
22

33
use log::{debug, error, info};
4+
use svd2rust::util::{Case, IdentFormat};
45

56
use std::io::Write;
67
use std::process;
78
use std::{fs::File, path::Path};
89

9-
use anyhow::{Context, Result};
10+
use anyhow::{anyhow, Context, Result};
1011
use clap::{Arg, ArgAction, Command};
1112

1213
use svd2rust::{
@@ -18,6 +19,8 @@ use svd2rust::{
1819
fn parse_configs(app: Command) -> Result<Config> {
1920
use irx_config::parsers::{cmd, toml};
2021
use irx_config::ConfigBuilder;
22+
let ident_formats = app.clone().get_matches();
23+
dbg!(&ident_formats);
2124
let irxconfig = ConfigBuilder::default()
2225
.append_parser(cmd::ParserBuilder::new(app).exit_on_error(true).build()?)
2326
.append_parser(
@@ -29,7 +32,81 @@ fn parse_configs(app: Command) -> Result<Config> {
2932
)
3033
.load()?;
3134

32-
irxconfig.get().map_err(Into::into)
35+
let mut config: Config = irxconfig.get()?;
36+
if let Some(ident_formats) = ident_formats.get_many::<String>("ident_format") {
37+
for f in ident_formats {
38+
let mut f = f.split(":");
39+
dbg!(f.clone().count());
40+
if let (Some(n), Some(p), Some(c), Some(s)) = (f.next(), f.next(), f.next(), f.next()) {
41+
let fmts = &mut config.ident_formats;
42+
let case = match c {
43+
"" => None,
44+
"p" | "pascal" | "type" => Some(Case::Pascal),
45+
"s" | "snake" | "lower" => Some(Case::Snake),
46+
"c" | "constant" | "upper" => Some(Case::Constant),
47+
_ => return Err(anyhow!("Unknown case")),
48+
};
49+
let id_f = IdentFormat {
50+
case,
51+
prefix: p.into(),
52+
suffix: s.into(),
53+
};
54+
dbg!(&id_f);
55+
match n {
56+
"field_reader" => {
57+
fmts.field_reader = id_f;
58+
}
59+
"enum_name" => {
60+
fmts.enum_name = id_f;
61+
}
62+
"enum_write_name" => {
63+
fmts.enum_write_name = id_f;
64+
}
65+
"enum_value" => {
66+
fmts.enum_value = id_f;
67+
}
68+
"interrupt" => {
69+
fmts.interrupt = id_f;
70+
}
71+
"cluster" => {
72+
fmts.cluster = id_f;
73+
}
74+
"cluster_accessor" => {
75+
fmts.cluster_accessor = id_f;
76+
}
77+
//"cluster_mod" => {
78+
// fmts.cluster_mod = id_f;
79+
//},
80+
"register" => {
81+
fmts.register = id_f;
82+
}
83+
"register_spec" => {
84+
fmts.register_spec = id_f;
85+
}
86+
"register_accessor" => {
87+
fmts.register_accessor = id_f;
88+
}
89+
//"register_mod" => {
90+
// fmts.register_mod = id_f;
91+
//},
92+
"peripheral" => {
93+
fmts.peripheral = id_f;
94+
}
95+
"peripheral_sigleton" => {
96+
fmts.peripheral_sigleton = id_f;
97+
}
98+
//"peripheral_mod" => {
99+
// fmts.peripheral_mod = id_f;
100+
//},
101+
"peripheral_feature" => {
102+
fmts.peripheral_feature = id_f;
103+
}
104+
_ => {}
105+
}
106+
}
107+
}
108+
}
109+
Ok(config)
33110
}
34111

35112
fn run() -> Result<()> {
@@ -119,6 +196,14 @@ fn run() -> Result<()> {
119196
.action(ArgAction::SetTrue)
120197
.help("Use independent cfg feature flags for each peripheral"),
121198
)
199+
.arg(
200+
Arg::new("ident_format")
201+
.long("ident-format")
202+
.short('f')
203+
.alias("ident_format")
204+
.action(ArgAction::Append)
205+
.help("Specify prefix, case and suffix for identifier type"),
206+
)
122207
.arg(
123208
Arg::new("max_cluster_size")
124209
.long("max-cluster-size")

0 commit comments

Comments
 (0)