Skip to content

Commit 1356b5f

Browse files
committed
IdentFormatsTheme
1 parent 39afcca commit 1356b5f

File tree

3 files changed

+149
-66
lines changed

3 files changed

+149
-66
lines changed

src/config.rs

+119-49
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

@@ -178,56 +179,113 @@ pub struct IdentFormats(HashMap<String, IdentFormat>);
178179

179180
impl Default for IdentFormats {
180181
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-
);
182+
Self::new_theme()
183+
}
184+
}
229185

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

@@ -242,3 +300,15 @@ impl DerefMut for IdentFormats {
242300
&mut self.0
243301
}
244302
}
303+
304+
#[cfg_attr(
305+
feature = "serde",
306+
derive(serde::Deserialize),
307+
serde(rename_all = "lowercase")
308+
)]
309+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
310+
pub enum IdentFormatsTheme {
311+
#[default]
312+
New,
313+
Legacy,
314+
}

src/lib.rs

+18-15
Original file line numberDiff line numberDiff line change
@@ -560,19 +560,19 @@
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 | | |
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 | | |
576576
//!
577577
//! To revert old behavior for `field_reader` you need to pass flag `-f field_reader::c:_R`. And repeat similar for other idents.
578578
//!
@@ -607,7 +607,7 @@ pub struct DeviceSpecific {
607607

608608
use anyhow::{Context, Result};
609609

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

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

624624
let mut config = config.clone();
625-
let mut ident_formats = IdentFormats::default();
625+
let mut ident_formats = match config.ident_formats_theme {
626+
IdentFormatsTheme::New => IdentFormats::new_theme(),
627+
IdentFormatsTheme::Legacy => IdentFormats::legacy_theme(),
628+
};
626629
ident_formats.extend(config.ident_formats.drain());
627630
config.ident_formats = ident_formats;
628631

src/main.rs

+12-2
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

@@ -169,6 +172,13 @@ Allowed cases are `unchanged` (''), `pascal` ('p'), `constant` ('c') and `snake`
169172
", IdentFormats::default().keys().collect::<Vec<_>>())
170173
),
171174
)
175+
.arg(
176+
Arg::new("ident_formats_theme")
177+
.long("ident-format-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)