Skip to content

Commit d8f1468

Browse files
authored
Merge pull request #805 from rust-embedded/case-defaults
change case defaults
2 parents 41b9ca1 + 85c54d7 commit d8f1468

File tree

5 files changed

+188
-102
lines changed

5 files changed

+188
-102
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
include:
5757
- { rust: stable, vendor: Atmel, options: all }
5858
- { rust: stable, vendor: Atmel, options: "" }
59-
- { rust: stable, vendor: Freescale, options: "--strict --atomics" }
59+
- { rust: stable, vendor: Freescale, options: all }
6060
- { rust: stable, vendor: Freescale, options: "" }
6161
- { rust: stable, vendor: Fujitsu, options: "" }
6262
- { rust: stable, vendor: Fujitsu, options: "--atomics" }
@@ -88,8 +88,9 @@ jobs:
8888
# Use nightly for architectures which don't support stable
8989
- { rust: nightly, vendor: MSP430, options: "--atomics" }
9090
- { rust: nightly, vendor: MSP430, options: "" }
91-
- { rust: nightly, vendor: Espressif, options: "--atomics" }
92-
- { rust: nightly, vendor: Espressif, options: "" }
91+
# Workaround for _1token0
92+
- { rust: nightly, vendor: Espressif, options: "--atomics --ident-formats-theme legacy" }
93+
- { rust: nightly, vendor: Espressif, options: "--ident-format register:::Reg" }
9394

9495
steps:
9596
- uses: actions/checkout@v4

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
1010
- Bump MSRV to 1.74
1111
- generic unsafe `W::bits` + safe `W::set`
1212
- Add `base-address-shift` config flag
13-
- Fix case changing bugs, add `--ident-format` (`-f`) option flag
13+
- Use `PascalCase` for type idents, fix case changing bugs, add `--ident-format` (`-f`) option flag
1414

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

src/config.rs

Lines changed: 119 additions & 56 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: Option<IdentFormatsTheme>,
3536
pub base_address_shift: u64,
3637
}
3738

@@ -136,6 +137,26 @@ pub enum Case {
136137
Snake,
137138
}
138139

140+
impl Case {
141+
pub fn parse(c: &str) -> Result<Option<Self>, IdentFormatError> {
142+
Ok(match c {
143+
"" | "unchanged" | "svd" => None,
144+
"p" | "pascal" | "type" => Some(Case::Pascal),
145+
"s" | "snake" | "lower" => Some(Case::Snake),
146+
"c" | "constant" | "upper" => Some(Case::Constant),
147+
_ => {
148+
return Err(IdentFormatError::UnknownCase(c.into()));
149+
}
150+
})
151+
}
152+
}
153+
154+
#[derive(Clone, Debug, PartialEq, Eq)]
155+
pub enum IdentFormatError {
156+
UnknownCase(String),
157+
Other,
158+
}
159+
139160
#[derive(Clone, Debug, Default, PartialEq, Eq)]
140161
#[cfg_attr(feature = "serde", derive(serde::Deserialize), serde(default))]
141162
pub struct IdentFormat {
@@ -170,67 +191,99 @@ impl IdentFormat {
170191
self.suffix = suffix.into();
171192
self
172193
}
194+
pub fn parse(s: &str) -> Result<Self, IdentFormatError> {
195+
let mut f = s.split(":");
196+
match (f.next(), f.next(), f.next()) {
197+
(Some(p), Some(c), Some(s)) => {
198+
let case = Case::parse(c)?;
199+
Ok(Self {
200+
case,
201+
prefix: p.into(),
202+
suffix: s.into(),
203+
})
204+
}
205+
(Some(p), Some(c), None) => {
206+
let case = Case::parse(c)?;
207+
Ok(Self {
208+
case,
209+
prefix: p.into(),
210+
suffix: "".into(),
211+
})
212+
}
213+
(Some(c), None, None) => {
214+
let case = Case::parse(c)?;
215+
Ok(Self {
216+
case,
217+
prefix: "".into(),
218+
suffix: "".into(),
219+
})
220+
}
221+
_ => Err(IdentFormatError::Other),
222+
}
223+
}
173224
}
174225

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

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

@@ -245,3 +298,13 @@ impl DerefMut for IdentFormats {
245298
&mut self.0
246299
}
247300
}
301+
302+
#[cfg_attr(
303+
feature = "serde",
304+
derive(serde::Deserialize),
305+
serde(rename_all = "lowercase")
306+
)]
307+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
308+
pub enum IdentFormatsTheme {
309+
Legacy,
310+
}

src/lib.rs

Lines changed: 31 additions & 16 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.
@@ -558,20 +558,32 @@
558558
//! `CONSTANT_CASE` (pass `constant` or `c`) and `leave_CASE_as_in_SVD` (pass `unchanged` or ``).
559559
//!
560560
//! There are identificator formats by default in the table.
561+
//! Since `svd2rust` 0.32 defaults have been changed.
562+
//!
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 <br> peripheral_feature | | snake | snake | | |
576+
//!
577+
//! To revert old behavior for `field_reader` you need to pass flag `-f field_reader::c:_R`.
578+
//!
579+
//! Also you can do the same in config file:
580+
//! ```toml
581+
//! [ident_formats.field_reader]
582+
//! case = constant
583+
//! suffix = "_R"
584+
//! ```
561585
//!
562-
//! | IdentifierType | Prefix | Case 0.31 | Suffix |
563-
//! |----------------------------------------------------------------------------------|:------:|:---------:|:-----------:|
564-
//! | field_reader | | constant | _R |
565-
//! | field_writer | | constant | _W |
566-
//! | enum_name | | constant | _A |
567-
//! | enum_write_name | | constant | _AW |
568-
//! | enum_value | | constant | |
569-
//! | interrupt | | constant | |
570-
//! | peripheral_singleton | | constant | |
571-
//! | peripheral <br> register <br> cluster | | constant | |
572-
//! | register_spec | | constant | _SPEC |
573-
//! | cluster_accessor <br> register_accessor<br>field_accessor<br>enum_value_accessor | | snake | |
574-
//! | cluster_mod <br> register_mod <br> peripheral_mod | | snake | |
586+
//! To revert old behavior for all identifiers you may pass `--ident-formats-theme legacy`.
575587
#![recursion_limit = "128"]
576588

577589
use quote::quote;
@@ -597,7 +609,7 @@ pub struct DeviceSpecific {
597609

598610
use anyhow::{Context, Result};
599611

600-
use crate::config::IdentFormats;
612+
use crate::config::{IdentFormats, IdentFormatsTheme};
601613

602614
#[derive(Debug, thiserror::Error)]
603615
pub enum SvdError {
@@ -612,7 +624,10 @@ pub fn generate(input: &str, config: &Config) -> Result<Generation> {
612624
use std::fmt::Write;
613625

614626
let mut config = config.clone();
615-
let mut ident_formats = IdentFormats::default();
627+
let mut ident_formats = match config.ident_formats_theme {
628+
Some(IdentFormatsTheme::Legacy) => IdentFormats::legacy_theme(),
629+
_ => IdentFormats::default_theme(),
630+
};
616631
ident_formats.extend(config.ident_formats.drain());
617632
config.ident_formats = ident_formats;
618633

0 commit comments

Comments
 (0)