From cf31f32ae0ddefd7b4fc34be17be7e61c50114ab Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Fri, 16 Feb 2024 21:47:10 +0300 Subject: [PATCH] IdentFormatsTheme --- .github/workflows/ci.yml | 2 +- src/config.rs | 170 +++++++++++++++++++++++++++------------ src/lib.rs | 41 +++++----- src/main.rs | 16 +++- 4 files changed, 154 insertions(+), 75 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5a3243b9..3e9807ac 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: diff --git a/src/config.rs b/src/config.rs index ce824498..c9300c41 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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, } @@ -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); -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(), + ), + ])) } } @@ -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, +} diff --git a/src/lib.rs b/src/lib.rs index 7509dc63..fa9a4f73 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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. @@ -560,21 +560,21 @@ //! 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
register
cluster | | pascal | constant | | | -//! | register_spec | | pascal | constant | Spec | _SPEC | -//! | cluster_accessor
register_accessor
field_accessor
enum_value_accessor | | snake | snake | | | -//! | cluster_mod
register_mod
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
register
cluster | | pascal | constant | | | +//! | register_spec | | pascal | constant | Spec | _SPEC | +//! | cluster_accessor
register_accessor
field_accessor
enum_value_accessor | | snake | snake | | | +//! | cluster_mod
register_mod
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 @@ -582,6 +582,8 @@ //! case = constant //! suffix = "_R" //! ``` +//! +//! To revert old behavior for all identifiers you may pass `--ident-formats-theme legacy`. #![recursion_limit = "128"] use quote::quote; @@ -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 { @@ -622,7 +624,10 @@ pub fn generate(input: &str, config: &Config) -> Result { 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; diff --git a/src/main.rs b/src/main.rs index b4d02e94..42bc1b67 100755 --- a/src/main.rs +++ b/src/main.rs @@ -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; @@ -33,7 +33,10 @@ fn parse_configs(app: Command) -> Result { .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; @@ -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::>()) +", IdentFormats::new_theme().keys().collect::>()) ), ) + .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")