Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change case defaults #805

Merged
merged 5 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
include:
- { rust: stable, vendor: Atmel, options: all }
- { rust: stable, vendor: Atmel, options: "" }
- { rust: stable, vendor: Freescale, options: "--strict --atomics" }
- { rust: stable, vendor: Freescale, options: all }
- { rust: stable, vendor: Freescale, options: "" }
- { rust: stable, vendor: Fujitsu, options: "" }
- { rust: stable, vendor: Fujitsu, options: "--atomics" }
Expand Down Expand Up @@ -88,8 +88,9 @@ jobs:
# Use nightly for architectures which don't support stable
- { rust: nightly, vendor: MSP430, options: "--atomics" }
- { rust: nightly, vendor: MSP430, options: "" }
- { rust: nightly, vendor: Espressif, options: "--atomics" }
- { rust: nightly, vendor: Espressif, options: "" }
# Workaround for _1token0
- { rust: nightly, vendor: Espressif, options: "--atomics --ident-formats-theme legacy" }
- { rust: nightly, vendor: Espressif, options: "--ident-format register:::Reg" }

steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
- Bump MSRV to 1.74
- generic unsafe `W::bits` + safe `W::set`
- Add `base-address-shift` config flag
- Fix case changing bugs, add `--ident-format` (`-f`) option flag
- Use `PascalCase` for type idents, fix case changing bugs, add `--ident-format` (`-f`) option flag

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

Expand Down
175 changes: 119 additions & 56 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub struct Config {
pub reexport_core_peripherals: bool,
pub reexport_interrupt: bool,
pub ident_formats: IdentFormats,
pub ident_formats_theme: Option<IdentFormatsTheme>,
pub base_address_shift: u64,
}

Expand Down Expand Up @@ -136,6 +137,26 @@ pub enum Case {
Snake,
}

impl Case {
pub fn parse(c: &str) -> Result<Option<Self>, IdentFormatError> {
Ok(match c {
"" | "unchanged" | "svd" => None,
"p" | "pascal" | "type" => Some(Case::Pascal),
"s" | "snake" | "lower" => Some(Case::Snake),
"c" | "constant" | "upper" => Some(Case::Constant),
_ => {
return Err(IdentFormatError::UnknownCase(c.into()));
}
})
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum IdentFormatError {
UnknownCase(String),
Other,
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize), serde(default))]
pub struct IdentFormat {
Expand Down Expand Up @@ -170,67 +191,99 @@ impl IdentFormat {
self.suffix = suffix.into();
self
}
pub fn parse(s: &str) -> Result<Self, IdentFormatError> {
let mut f = s.split(":");
match (f.next(), f.next(), f.next()) {
(Some(p), Some(c), Some(s)) => {
let case = Case::parse(c)?;
Ok(Self {
case,
prefix: p.into(),
suffix: s.into(),
})
}
(Some(p), Some(c), None) => {
let case = Case::parse(c)?;
Ok(Self {
case,
prefix: p.into(),
suffix: "".into(),
})
}
(Some(c), None, None) => {
let case = Case::parse(c)?;
Ok(Self {
case,
prefix: "".into(),
suffix: "".into(),
})
}
_ => Err(IdentFormatError::Other),
}
}
}

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

impl Default for IdentFormats {
fn default() -> Self {
let mut map = HashMap::new();

map.insert("field_accessor".into(), IdentFormat::default().snake_case());
map.insert(
"field_reader".into(),
IdentFormat::default().constant_case().suffix("_R"),
);
map.insert(
"field_writer".into(),
IdentFormat::default().constant_case().suffix("_W"),
);
map.insert(
"enum_name".into(),
IdentFormat::default().constant_case().suffix("_A"),
);
map.insert(
"enum_write_name".into(),
IdentFormat::default().constant_case().suffix("_AW"),
);
map.insert("enum_value".into(), IdentFormat::default().constant_case());
map.insert(
"enum_value_accessor".into(),
IdentFormat::default().snake_case(),
);
map.insert("interrupt".into(), IdentFormat::default().constant_case());
map.insert("cluster".into(), IdentFormat::default().constant_case());
map.insert(
"cluster_accessor".into(),
IdentFormat::default().snake_case(),
);
map.insert("cluster_mod".into(), IdentFormat::default().snake_case());
map.insert("register".into(), IdentFormat::default().constant_case());
map.insert(
"register_spec".into(),
IdentFormat::default().pascal_case().suffix("_SPEC"),
);
map.insert(
"register_accessor".into(),
IdentFormat::default().snake_case(),
);
map.insert("register_mod".into(), IdentFormat::default().snake_case());
map.insert("peripheral".into(), IdentFormat::default().constant_case());
map.insert(
"peripheral_singleton".into(),
IdentFormat::default().constant_case(),
);
map.insert("peripheral_mod".into(), IdentFormat::default().snake_case());
map.insert(
"peripheral_feature".into(),
IdentFormat::default().snake_case(),
);

Self(map)
impl IdentFormats {
fn common() -> Self {
let snake = IdentFormat::default().snake_case();
Self(HashMap::from([
("field_accessor".into(), snake.clone()),
("register_accessor".into(), snake.clone()),
("enum_value_accessor".into(), snake.clone()),
("cluster_accessor".into(), snake.clone()),
("register_mod".into(), snake.clone()),
("cluster_mod".into(), snake.clone()),
("peripheral_mod".into(), snake.clone()),
("peripheral_feature".into(), snake),
]))
}

pub fn default_theme() -> Self {
let mut map = Self::common();

let pascal = IdentFormat::default().pascal_case();
map.extend([
("field_reader".into(), pascal.clone().suffix("R")),
("field_writer".into(), pascal.clone().suffix("W")),
("enum_name".into(), pascal.clone()),
("enum_write_name".into(), pascal.clone().suffix("WO")),
("enum_value".into(), pascal.clone()),
("interrupt".into(), IdentFormat::default()),
("register".into(), pascal.clone()),
("cluster".into(), pascal.clone()),
("register_spec".into(), pascal.clone().suffix("Spec")),
("peripheral".into(), pascal),
(
"peripheral_singleton".into(),
IdentFormat::default().snake_case(),
),
]);

map
}
pub fn legacy_theme() -> Self {
let mut map = Self::common();

let constant = IdentFormat::default().constant_case();
map.extend([
("field_reader".into(), constant.clone().suffix("_R")),
("field_writer".into(), constant.clone().suffix("_W")),
("enum_name".into(), constant.clone().suffix("_A")),
("enum_write_name".into(), constant.clone().suffix("_AW")),
("enum_value".into(), constant.clone()),
("interrupt".into(), constant.clone()),
("cluster".into(), constant.clone()),
("register".into(), constant.clone()),
("register_spec".into(), constant.clone().suffix("_SPEC")),
("peripheral".into(), constant.clone()),
("peripheral_singleton".into(), constant),
]);

map
}
}

Expand All @@ -245,3 +298,13 @@ impl DerefMut for IdentFormats {
&mut self.0
}
}

#[cfg_attr(
feature = "serde",
derive(serde::Deserialize),
serde(rename_all = "lowercase")
)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum IdentFormatsTheme {
Legacy,
}
47 changes: 31 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@
//! The `--impl-defmt` flag can also be specified to include `defmt::Format` implementations conditionally
//! behind the supplied feature name.
//!
//! ## the `--ident-format` flag
//! ## the `--ident-format` and `--ident-formats-theme` flags
//!
//! The `--ident-format type:prefix:case:suffix` (`-f`) flag can also be specified if you want to change
//! default behavior of formatting rust structure and enum names, register access methods, etc.
Expand All @@ -558,20 +558,32 @@
//! `CONSTANT_CASE` (pass `constant` or `c`) and `leave_CASE_as_in_SVD` (pass `unchanged` or ``).
//!
//! There are identificator formats by default in the table.
//! Since `svd2rust` 0.32 defaults have been changed.
//!
//! | IdentifierType | Prefix | Case | Case 0.31 | Suffix | Suffix 0.31 |
//! |--------------------------------------------------------------------------------|:------:|:---------:|:---------:|:------:|:-----------:|
//! | field_reader | | pascal | constant | R | _R |
//! | field_writer | | pascal | constant | W | _W |
//! | enum_name | | pascal | constant | | _A |
//! | enum_write_name | | pascal | constant | WO | _AW |
//! | enum_value | | pascal | constant | | |
//! | interrupt | | unchanged | constant | | |
//! | peripheral_singleton | | snake | constant | | |
//! | peripheral <br> register <br> cluster | | pascal | constant | | |
//! | register_spec | | pascal | constant | Spec | _SPEC |
//! | cluster_accessor<br>register_accessor<br>field_accessor<br>enum_value_accessor | | snake | snake | | |
//! | cluster_mod <br> register_mod <br> peripheral_mod <br> peripheral_feature | | snake | snake | | |
//!
//! To revert old behavior for `field_reader` you need to pass flag `-f field_reader::c:_R`.
//!
//! Also you can do the same in config file:
//! ```toml
//! [ident_formats.field_reader]
//! case = constant
//! suffix = "_R"
//! ```
//!
//! | IdentifierType | Prefix | Case 0.31 | Suffix |
//! |----------------------------------------------------------------------------------|:------:|:---------:|:-----------:|
//! | field_reader | | constant | _R |
//! | field_writer | | constant | _W |
//! | enum_name | | constant | _A |
//! | enum_write_name | | constant | _AW |
//! | enum_value | | constant | |
//! | interrupt | | constant | |
//! | peripheral_singleton | | constant | |
//! | peripheral <br> register <br> cluster | | constant | |
//! | register_spec | | constant | _SPEC |
//! | cluster_accessor <br> register_accessor<br>field_accessor<br>enum_value_accessor | | snake | |
//! | cluster_mod <br> register_mod <br> peripheral_mod | | snake | |
//! To revert old behavior for all identifiers you may pass `--ident-formats-theme legacy`.
#![recursion_limit = "128"]

use quote::quote;
Expand All @@ -597,7 +609,7 @@ pub struct DeviceSpecific {

use anyhow::{Context, Result};

use crate::config::IdentFormats;
use crate::config::{IdentFormats, IdentFormatsTheme};

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

let mut config = config.clone();
let mut ident_formats = IdentFormats::default();
let mut ident_formats = match config.ident_formats_theme {
Some(IdentFormatsTheme::Legacy) => IdentFormats::legacy_theme(),
_ => IdentFormats::default_theme(),
};
ident_formats.extend(config.ident_formats.drain());
config.ident_formats = ident_formats;

Expand Down
Loading
Loading