Skip to content

Commit

Permalink
IdentFormatsTheme
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Feb 16, 2024
1 parent 39afcca commit cf31f32
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 75 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ jobs:
- { rust: nightly, vendor: MSP430, options: "--atomics" }
- { rust: nightly, vendor: MSP430, options: "" }
# Workaround for _1token0
- { rust: nightly, vendor: Espressif, options: "--atomics --ident-format register::c:" }
- { rust: nightly, vendor: Espressif, options: "--atomics --ident-formats-theme legacy" }
- { rust: nightly, vendor: Espressif, options: "--ident-format register:::Reg" }

steps:
Expand Down
170 changes: 117 additions & 53 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: IdentFormatsTheme,
pub base_address_shift: u64,
}

Expand Down Expand Up @@ -172,62 +173,113 @@ impl IdentFormat {
}
}

#[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().pascal_case().suffix("R"),
);
map.insert(
"field_writer".into(),
IdentFormat::default().pascal_case().suffix("W"),
);
map.insert("enum_name".into(), IdentFormat::default().pascal_case());
map.insert(
"enum_write_name".into(),
IdentFormat::default().pascal_case().suffix("WO"),
);
map.insert("enum_value".into(), IdentFormat::default().pascal_case());
map.insert(
"enum_value_accessor".into(),
IdentFormat::default().snake_case(),
);
map.insert("interrupt".into(), IdentFormat::default());
map.insert("cluster".into(), IdentFormat::default().pascal_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().pascal_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().pascal_case());
map.insert(
"peripheral_singleton".into(),
IdentFormat::default().snake_case(),
);
map.insert("peripheral_mod".into(), IdentFormat::default().snake_case());
map.insert(
"peripheral_feature".into(),
IdentFormat::default().snake_case(),
);

Self(map)
impl IdentFormats {
pub fn new_theme() -> Self {
Self(HashMap::from([
("field_accessor".into(), IdentFormat::default().snake_case()),
(
"field_reader".into(),
IdentFormat::default().pascal_case().suffix("R"),
),
(
"field_writer".into(),
IdentFormat::default().pascal_case().suffix("W"),
),
("enum_name".into(), IdentFormat::default().pascal_case()),
(
"enum_write_name".into(),
IdentFormat::default().pascal_case().suffix("WO"),
),
("enum_value".into(), IdentFormat::default().pascal_case()),
(
"enum_value_accessor".into(),
IdentFormat::default().snake_case(),
),
("interrupt".into(), IdentFormat::default()),
("cluster".into(), IdentFormat::default().pascal_case()),
(
"cluster_accessor".into(),
IdentFormat::default().snake_case(),
),
("cluster_mod".into(), IdentFormat::default().snake_case()),
("register".into(), IdentFormat::default().pascal_case()),
(
"register_spec".into(),
IdentFormat::default().pascal_case().suffix("Spec"),
),
(
"register_accessor".into(),
IdentFormat::default().snake_case(),
),
("register_mod".into(), IdentFormat::default().snake_case()),
("peripheral".into(), IdentFormat::default().pascal_case()),
(
"peripheral_singleton".into(),
IdentFormat::default().snake_case(),
),
("peripheral_mod".into(), IdentFormat::default().snake_case()),
(
"peripheral_feature".into(),
IdentFormat::default().snake_case(),
),
]))
}
pub fn legacy_theme() -> Self {
Self(HashMap::from([
("field_accessor".into(), IdentFormat::default().snake_case()),
(
"field_reader".into(),
IdentFormat::default().constant_case().suffix("_R"),
),
(
"field_writer".into(),
IdentFormat::default().constant_case().suffix("_W"),
),
(
"enum_name".into(),
IdentFormat::default().constant_case().suffix("_A"),
),
(
"enum_write_name".into(),
IdentFormat::default().constant_case().suffix("_AW"),
),
("enum_value".into(), IdentFormat::default().constant_case()),
(
"enum_value_accessor".into(),
IdentFormat::default().snake_case(),
),
("interrupt".into(), IdentFormat::default().constant_case()),
("cluster".into(), IdentFormat::default().constant_case()),
(
"cluster_accessor".into(),
IdentFormat::default().snake_case(),
),
("cluster_mod".into(), IdentFormat::default().snake_case()),
("register".into(), IdentFormat::default().constant_case()),
(
"register_spec".into(),
IdentFormat::default().constant_case().suffix("_SPEC"),
),
(
"register_accessor".into(),
IdentFormat::default().snake_case(),
),
("register_mod".into(), IdentFormat::default().snake_case()),
("peripheral".into(), IdentFormat::default().constant_case()),
(
"peripheral_singleton".into(),
IdentFormat::default().constant_case(),
),
("peripheral_mod".into(), IdentFormat::default().snake_case()),
(
"peripheral_feature".into(),
IdentFormat::default().snake_case(),
),
]))
}
}

Expand All @@ -242,3 +294,15 @@ impl DerefMut for IdentFormats {
&mut self.0
}
}

#[cfg_attr(
feature = "serde",
derive(serde::Deserialize),
serde(rename_all = "lowercase")
)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum IdentFormatsTheme {
#[default]
New,
Legacy,
}
41 changes: 23 additions & 18 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 @@ -560,28 +560,30 @@
//! 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 | | snake | snake | | |
//!
//! To revert old behavior for `field_reader` you need to pass flag `-f field_reader::c:_R`. And repeat similar for other idents.
//! | 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 | | 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"
//! ```
//!
//! To revert old behavior for all identifiers you may pass `--ident-formats-theme legacy`.
#![recursion_limit = "128"]

use quote::quote;
Expand All @@ -607,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 @@ -622,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 {
IdentFormatsTheme::New => IdentFormats::new_theme(),
IdentFormatsTheme::Legacy => IdentFormats::legacy_theme(),
};
ident_formats.extend(config.ident_formats.drain());
config.ident_formats = ident_formats;

Expand Down
16 changes: 13 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![recursion_limit = "128"]

use log::{debug, error, info, warn};
use svd2rust::config::IdentFormats;
use svd2rust::config::{IdentFormats, IdentFormatsTheme};
use svd2rust::util::{Case, IdentFormat};

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

let mut config: Config = irxconfig.get()?;
let mut idf = IdentFormats::default();
let mut idf = match config.ident_formats_theme {
IdentFormatsTheme::New => IdentFormats::new_theme(),
IdentFormatsTheme::Legacy => IdentFormats::legacy_theme(),
};
idf.extend(config.ident_formats.drain());
config.ident_formats = idf;

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

0 comments on commit cf31f32

Please sign in to comment.