Skip to content

Commit 1357824

Browse files
committed
IdentFormatsTheme
1 parent 30eb398 commit 1357824

File tree

4 files changed

+154
-75
lines changed

4 files changed

+154
-75
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ jobs:
8989
- { rust: nightly, vendor: MSP430, options: "--atomics" }
9090
- { rust: nightly, vendor: MSP430, options: "" }
9191
# Workaround for _1token0
92-
- { rust: nightly, vendor: Espressif, options: "--atomics --ident-format register::c:" }
92+
- { rust: nightly, vendor: Espressif, options: "--atomics --ident-formats-theme legacy" }
9393
- { rust: nightly, vendor: Espressif, options: "--ident-format register:::Reg" }
9494

9595
steps:

src/config.rs

Lines changed: 117 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub struct Config {
3232
pub reexport_core_peripherals: bool,
3333
pub reexport_interrupt: bool,
3434
pub ident_formats: IdentFormats,
35+
pub ident_formats_theme: IdentFormatsTheme,
3536
pub base_address_shift: u64,
3637
}
3738

@@ -172,62 +173,113 @@ impl IdentFormat {
172173
}
173174
}
174175

175-
#[derive(Clone, Debug, PartialEq, Eq)]
176+
#[derive(Clone, Debug, Default, PartialEq, Eq)]
176177
#[cfg_attr(feature = "serde", derive(serde::Deserialize), serde(default))]
177178
pub struct IdentFormats(HashMap<String, IdentFormat>);
178179

179-
impl Default for IdentFormats {
180-
fn default() -> Self {
181-
let mut map = HashMap::new();
182-
183-
map.insert("field_accessor".into(), IdentFormat::default().snake_case());
184-
map.insert(
185-
"field_reader".into(),
186-
IdentFormat::default().pascal_case().suffix("R"),
187-
);
188-
map.insert(
189-
"field_writer".into(),
190-
IdentFormat::default().pascal_case().suffix("W"),
191-
);
192-
map.insert("enum_name".into(), IdentFormat::default().pascal_case());
193-
map.insert(
194-
"enum_write_name".into(),
195-
IdentFormat::default().pascal_case().suffix("WO"),
196-
);
197-
map.insert("enum_value".into(), IdentFormat::default().pascal_case());
198-
map.insert(
199-
"enum_value_accessor".into(),
200-
IdentFormat::default().snake_case(),
201-
);
202-
map.insert("interrupt".into(), IdentFormat::default());
203-
map.insert("cluster".into(), IdentFormat::default().pascal_case());
204-
map.insert(
205-
"cluster_accessor".into(),
206-
IdentFormat::default().snake_case(),
207-
);
208-
map.insert("cluster_mod".into(), IdentFormat::default().snake_case());
209-
map.insert("register".into(), IdentFormat::default().pascal_case());
210-
map.insert(
211-
"register_spec".into(),
212-
IdentFormat::default().pascal_case().suffix("Spec"),
213-
);
214-
map.insert(
215-
"register_accessor".into(),
216-
IdentFormat::default().snake_case(),
217-
);
218-
map.insert("register_mod".into(), IdentFormat::default().snake_case());
219-
map.insert("peripheral".into(), IdentFormat::default().pascal_case());
220-
map.insert(
221-
"peripheral_singleton".into(),
222-
IdentFormat::default().snake_case(),
223-
);
224-
map.insert("peripheral_mod".into(), IdentFormat::default().snake_case());
225-
map.insert(
226-
"peripheral_feature".into(),
227-
IdentFormat::default().snake_case(),
228-
);
229-
230-
Self(map)
180+
impl IdentFormats {
181+
pub fn new_theme() -> Self {
182+
Self(HashMap::from([
183+
("field_accessor".into(), IdentFormat::default().snake_case()),
184+
(
185+
"field_reader".into(),
186+
IdentFormat::default().pascal_case().suffix("R"),
187+
),
188+
(
189+
"field_writer".into(),
190+
IdentFormat::default().pascal_case().suffix("W"),
191+
),
192+
("enum_name".into(), IdentFormat::default().pascal_case()),
193+
(
194+
"enum_write_name".into(),
195+
IdentFormat::default().pascal_case().suffix("WO"),
196+
),
197+
("enum_value".into(), IdentFormat::default().pascal_case()),
198+
(
199+
"enum_value_accessor".into(),
200+
IdentFormat::default().snake_case(),
201+
),
202+
("interrupt".into(), IdentFormat::default()),
203+
("cluster".into(), IdentFormat::default().pascal_case()),
204+
(
205+
"cluster_accessor".into(),
206+
IdentFormat::default().snake_case(),
207+
),
208+
("cluster_mod".into(), IdentFormat::default().snake_case()),
209+
("register".into(), IdentFormat::default().pascal_case()),
210+
(
211+
"register_spec".into(),
212+
IdentFormat::default().pascal_case().suffix("Spec"),
213+
),
214+
(
215+
"register_accessor".into(),
216+
IdentFormat::default().snake_case(),
217+
),
218+
("register_mod".into(), IdentFormat::default().snake_case()),
219+
("peripheral".into(), IdentFormat::default().pascal_case()),
220+
(
221+
"peripheral_singleton".into(),
222+
IdentFormat::default().snake_case(),
223+
),
224+
("peripheral_mod".into(), IdentFormat::default().snake_case()),
225+
(
226+
"peripheral_feature".into(),
227+
IdentFormat::default().snake_case(),
228+
),
229+
]))
230+
}
231+
pub fn legacy_theme() -> Self {
232+
Self(HashMap::from([
233+
("field_accessor".into(), IdentFormat::default().snake_case()),
234+
(
235+
"field_reader".into(),
236+
IdentFormat::default().constant_case().suffix("_R"),
237+
),
238+
(
239+
"field_writer".into(),
240+
IdentFormat::default().constant_case().suffix("_W"),
241+
),
242+
(
243+
"enum_name".into(),
244+
IdentFormat::default().constant_case().suffix("_A"),
245+
),
246+
(
247+
"enum_write_name".into(),
248+
IdentFormat::default().constant_case().suffix("_AW"),
249+
),
250+
("enum_value".into(), IdentFormat::default().constant_case()),
251+
(
252+
"enum_value_accessor".into(),
253+
IdentFormat::default().snake_case(),
254+
),
255+
("interrupt".into(), IdentFormat::default().constant_case()),
256+
("cluster".into(), IdentFormat::default().constant_case()),
257+
(
258+
"cluster_accessor".into(),
259+
IdentFormat::default().snake_case(),
260+
),
261+
("cluster_mod".into(), IdentFormat::default().snake_case()),
262+
("register".into(), IdentFormat::default().constant_case()),
263+
(
264+
"register_spec".into(),
265+
IdentFormat::default().constant_case().suffix("_SPEC"),
266+
),
267+
(
268+
"register_accessor".into(),
269+
IdentFormat::default().snake_case(),
270+
),
271+
("register_mod".into(), IdentFormat::default().snake_case()),
272+
("peripheral".into(), IdentFormat::default().constant_case()),
273+
(
274+
"peripheral_singleton".into(),
275+
IdentFormat::default().constant_case(),
276+
),
277+
("peripheral_mod".into(), IdentFormat::default().snake_case()),
278+
(
279+
"peripheral_feature".into(),
280+
IdentFormat::default().snake_case(),
281+
),
282+
]))
231283
}
232284
}
233285

@@ -242,3 +294,15 @@ impl DerefMut for IdentFormats {
242294
&mut self.0
243295
}
244296
}
297+
298+
#[cfg_attr(
299+
feature = "serde",
300+
derive(serde::Deserialize),
301+
serde(rename_all = "lowercase")
302+
)]
303+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
304+
pub enum IdentFormatsTheme {
305+
#[default]
306+
New,
307+
Legacy,
308+
}

src/lib.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@
549549
//! The `--impl-defmt` flag can also be specified to include `defmt::Format` implementations conditionally
550550
//! behind the supplied feature name.
551551
//!
552-
//! ## the `--ident-format` flag
552+
//! ## the `--ident-format` and `--ident-formats-theme` flags
553553
//!
554554
//! The `--ident-format type:prefix:case:suffix` (`-f`) flag can also be specified if you want to change
555555
//! default behavior of formatting rust structure and enum names, register access methods, etc.
@@ -560,28 +560,30 @@
560560
//! There are identificator formats by default in the table.
561561
//! Since `svd2rust` 0.32 defaults have been changed.
562562
//!
563-
//! | IdentifierType | Prefix | Case | Case 0.31 | Suffix | Suffix 0.31 |
564-
//! |----------------------------------------------------------------------------------|:------:|:---------:|:---------:|:------:|:-----------:|
565-
//! | field_reader | | pascal | constant | R | _R |
566-
//! | field_writer | | pascal | constant | W | _W |
567-
//! | enum_name | | pascal | constant | | _A |
568-
//! | enum_write_name | | pascal | constant | WO | _AW |
569-
//! | enum_value | | pascal | constant | | |
570-
//! | interrupt | | unchanged | constant | | |
571-
//! | peripheral_singleton | | snake | constant | | |
572-
//! | peripheral <br> register <br> cluster | | pascal | constant | | |
573-
//! | register_spec | | pascal | constant | Spec | _SPEC |
574-
//! | cluster_accessor <br> register_accessor<br>field_accessor<br>enum_value_accessor | | snake | snake | | |
575-
//! | cluster_mod <br> register_mod <br> peripheral_mod | | snake | snake | | |
576-
//!
577-
//! To revert old behavior for `field_reader` you need to pass flag `-f field_reader::c:_R`. And repeat similar for other idents.
563+
//! | IdentifierType | Prefix | Case | Case 0.31 | Suffix | Suffix 0.31 |
564+
//! |--------------------------------------------------------------------------------|:------:|:---------:|:---------:|:------:|:-----------:|
565+
//! | field_reader | | pascal | constant | R | _R |
566+
//! | field_writer | | pascal | constant | W | _W |
567+
//! | enum_name | | pascal | constant | | _A |
568+
//! | enum_write_name | | pascal | constant | WO | _AW |
569+
//! | enum_value | | pascal | constant | | |
570+
//! | interrupt | | unchanged | constant | | |
571+
//! | peripheral_singleton | | snake | constant | | |
572+
//! | peripheral <br> register <br> cluster | | pascal | constant | | |
573+
//! | register_spec | | pascal | constant | Spec | _SPEC |
574+
//! | cluster_accessor<br>register_accessor<br>field_accessor<br>enum_value_accessor | | snake | snake | | |
575+
//! | cluster_mod <br> register_mod <br> peripheral_mod | | snake | snake | | |
576+
//!
577+
//! To revert old behavior for `field_reader` you need to pass flag `-f field_reader::c:_R`.
578578
//!
579579
//! Also you can do the same in config file:
580580
//! ```toml
581581
//! [ident_formats.field_reader]
582582
//! case = constant
583583
//! suffix = "_R"
584584
//! ```
585+
//!
586+
//! To revert old behavior for all identifiers you may pass `--ident-formats-theme legacy`.
585587
#![recursion_limit = "128"]
586588

587589
use quote::quote;
@@ -607,7 +609,7 @@ pub struct DeviceSpecific {
607609

608610
use anyhow::{Context, Result};
609611

610-
use crate::config::IdentFormats;
612+
use crate::config::{IdentFormats, IdentFormatsTheme};
611613

612614
#[derive(Debug, thiserror::Error)]
613615
pub enum SvdError {
@@ -622,7 +624,10 @@ pub fn generate(input: &str, config: &Config) -> Result<Generation> {
622624
use std::fmt::Write;
623625

624626
let mut config = config.clone();
625-
let mut ident_formats = IdentFormats::default();
627+
let mut ident_formats = match config.ident_formats_theme {
628+
IdentFormatsTheme::New => IdentFormats::new_theme(),
629+
IdentFormatsTheme::Legacy => IdentFormats::legacy_theme(),
630+
};
626631
ident_formats.extend(config.ident_formats.drain());
627632
config.ident_formats = ident_formats;
628633

src/main.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![recursion_limit = "128"]
22

33
use log::{debug, error, info, warn};
4-
use svd2rust::config::IdentFormats;
4+
use svd2rust::config::{IdentFormats, IdentFormatsTheme};
55
use svd2rust::util::{Case, IdentFormat};
66

77
use std::io::Write;
@@ -33,7 +33,10 @@ fn parse_configs(app: Command) -> Result<Config> {
3333
.load()?;
3434

3535
let mut config: Config = irxconfig.get()?;
36-
let mut idf = IdentFormats::default();
36+
let mut idf = match config.ident_formats_theme {
37+
IdentFormatsTheme::New => IdentFormats::new_theme(),
38+
IdentFormatsTheme::Legacy => IdentFormats::legacy_theme(),
39+
};
3740
idf.extend(config.ident_formats.drain());
3841
config.ident_formats = idf;
3942

@@ -166,9 +169,16 @@ fn run() -> Result<()> {
166169
format!("Specify `-f type:prefix:case:suffix` to change default ident formatting.
167170
Allowed values of `type` are {:?}.
168171
Allowed cases are `unchanged` (''), `pascal` ('p'), `constant` ('c') and `snake` ('s').
169-
", IdentFormats::default().keys().collect::<Vec<_>>())
172+
", IdentFormats::new_theme().keys().collect::<Vec<_>>())
170173
),
171174
)
175+
.arg(
176+
Arg::new("ident_formats_theme")
177+
.long("ident-formats-theme")
178+
.help("A set of `ident_format` settings. `new` or `legacy`")
179+
.action(ArgAction::Set)
180+
.value_name("THEME"),
181+
)
172182
.arg(
173183
Arg::new("max_cluster_size")
174184
.long("max-cluster-size")

0 commit comments

Comments
 (0)