Skip to content

Commit 1882951

Browse files
bors[bot]burrbull
andauthored
Merge #591
591: optional PascalCase for Enum values r=Emilgardis a=burrbull ![изображение](https://user-images.githubusercontent.com/3072754/163558809-1bd00854-7cac-4017-aab4-e379d582a52a.png) without and with `--pascal_enum_values` option related: #85 r?adamgreig Co-authored-by: Andrey Zgarbul <zgarbul.andrey@gmail.com>
2 parents 64530cb + 310b9a6 commit 1882951

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
- Optional PascalCase for Enum values instead of UPPER_CASE
11+
1012
## [v0.22.2] - 2022-04-13
1113

1214
- Fix #579 2: support 1-element arrays

src/generate/register.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use log::warn;
88
use proc_macro2::{Ident, Punct, Spacing, Span, TokenStream};
99
use quote::{quote, ToTokens};
1010

11-
use crate::util::{self, Config, ToSanitizedSnakeCase, ToSanitizedUpperCase, U32Ext};
11+
use crate::util::{
12+
self, Config, ToSanitizedPascalCase, ToSanitizedSnakeCase, ToSanitizedUpperCase, U32Ext,
13+
};
1214
use anyhow::{anyhow, Result};
1315

1416
pub fn render(
@@ -478,7 +480,7 @@ pub fn fields(
478480
derive_from_base(mod_items, &base, &name_pc_r, &base_pc_r, &readerdoc);
479481
} else {
480482
let has_reserved_variant = evs.values.len() != (1 << width);
481-
let variants = Variant::from_enumerated_values(evs)?;
483+
let variants = Variant::from_enumerated_values(evs, config.pascal_enum_values)?;
482484
let mut enum_items = TokenStream::new();
483485

484486
if variants.is_empty() {
@@ -610,7 +612,7 @@ pub fn fields(
610612
let mut unsafety = unsafety(f.write_constraint.as_ref(), width);
611613

612614
if let Some((evs, base)) = lookup_filter(&lookup_results, Usage::Write) {
613-
let variants = Variant::from_enumerated_values(evs)?;
615+
let variants = Variant::from_enumerated_values(evs, config.pascal_enum_values)?;
614616

615617
if variants.len() == 1 << width {
616618
unsafety = None;
@@ -872,7 +874,7 @@ struct Variant {
872874
}
873875

874876
impl Variant {
875-
fn from_enumerated_values(evs: &EnumeratedValues) -> Result<Vec<Self>> {
877+
fn from_enumerated_values(evs: &EnumeratedValues, pc: bool) -> Result<Vec<Self>> {
876878
let span = Span::call_site();
877879
evs.values
878880
.iter()
@@ -891,7 +893,14 @@ impl Variant {
891893
.description
892894
.clone()
893895
.unwrap_or_else(|| format!("`{:b}`", value)),
894-
pc: Ident::new(&ev.name.to_sanitized_upper_case(), span),
896+
pc: Ident::new(
897+
&(if pc {
898+
ev.name.to_sanitized_pascal_case()
899+
} else {
900+
ev.name.to_sanitized_upper_case()
901+
}),
902+
span,
903+
),
895904
nksc: Ident::new(&nksc, span),
896905
sc: Ident::new(&sc, span),
897906
value,

src/main.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ fn run() -> Result<()> {
8787
.short("s")
8888
.help("Make advanced checks due to parsing SVD"),
8989
)
90+
.arg(
91+
Arg::with_name("pascal_enum_values")
92+
.long("pascal_enum_values")
93+
.help("Use PascalCase in stead of UPPER_CASE for enumerated values"),
94+
)
9095
.arg(
9196
Arg::with_name("source_type")
9297
.long("source_type")
@@ -155,6 +160,8 @@ fn run() -> Result<()> {
155160
let keep_list =
156161
cfg.bool_flag("keep_list", Filter::Arg) || cfg.bool_flag("keep_list", Filter::Conf);
157162
let strict = cfg.bool_flag("strict", Filter::Arg) || cfg.bool_flag("strict", Filter::Conf);
163+
let pascal_enum_values = cfg.bool_flag("pascal_enum_values", Filter::Arg)
164+
|| cfg.bool_flag("pascal_enum_values", Filter::Conf);
158165

159166
let mut source_type = cfg
160167
.grab()
@@ -177,6 +184,7 @@ fn run() -> Result<()> {
177184
ignore_groups,
178185
keep_list,
179186
strict,
187+
pascal_enum_values,
180188
output_dir: path.clone(),
181189
source_type,
182190
};

src/util.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub struct Config {
2626
pub ignore_groups: bool,
2727
pub keep_list: bool,
2828
pub strict: bool,
29+
pub pascal_enum_values: bool,
2930
pub output_dir: PathBuf,
3031
pub source_type: SourceType,
3132
}
@@ -41,6 +42,7 @@ impl Default for Config {
4142
ignore_groups: false,
4243
keep_list: false,
4344
strict: false,
45+
pascal_enum_values: false,
4446
output_dir: PathBuf::from("."),
4547
source_type: SourceType::default(),
4648
}

0 commit comments

Comments
 (0)