Skip to content

optional PascalCase for Enum values #591

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

Merged
merged 1 commit into from
Apr 16, 2022
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

- Optional PascalCase for Enum values instead of UPPER_CASE

## [v0.22.2] - 2022-04-13

- Fix #579 2: support 1-element arrays
Expand Down
19 changes: 14 additions & 5 deletions src/generate/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use log::warn;
use proc_macro2::{Ident, Punct, Spacing, Span, TokenStream};
use quote::{quote, ToTokens};

use crate::util::{self, Config, ToSanitizedSnakeCase, ToSanitizedUpperCase, U32Ext};
use crate::util::{
self, Config, ToSanitizedPascalCase, ToSanitizedSnakeCase, ToSanitizedUpperCase, U32Ext,
};
use anyhow::{anyhow, Result};

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

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

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

if variants.len() == 1 << width {
unsafety = None;
Expand Down Expand Up @@ -872,7 +874,7 @@ struct Variant {
}

impl Variant {
fn from_enumerated_values(evs: &EnumeratedValues) -> Result<Vec<Self>> {
fn from_enumerated_values(evs: &EnumeratedValues, pc: bool) -> Result<Vec<Self>> {
let span = Span::call_site();
evs.values
.iter()
Expand All @@ -891,7 +893,14 @@ impl Variant {
.description
.clone()
.unwrap_or_else(|| format!("`{:b}`", value)),
pc: Ident::new(&ev.name.to_sanitized_upper_case(), span),
pc: Ident::new(
&(if pc {
ev.name.to_sanitized_pascal_case()
} else {
ev.name.to_sanitized_upper_case()
}),
span,
),
nksc: Ident::new(&nksc, span),
sc: Ident::new(&sc, span),
value,
Expand Down
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ fn run() -> Result<()> {
.short("s")
.help("Make advanced checks due to parsing SVD"),
)
.arg(
Arg::with_name("pascal_enum_values")
.long("pascal_enum_values")
.help("Use PascalCase in stead of UPPER_CASE for enumerated values"),
)
.arg(
Arg::with_name("source_type")
.long("source_type")
Expand Down Expand Up @@ -155,6 +160,8 @@ fn run() -> Result<()> {
let keep_list =
cfg.bool_flag("keep_list", Filter::Arg) || cfg.bool_flag("keep_list", Filter::Conf);
let strict = cfg.bool_flag("strict", Filter::Arg) || cfg.bool_flag("strict", Filter::Conf);
let pascal_enum_values = cfg.bool_flag("pascal_enum_values", Filter::Arg)
|| cfg.bool_flag("pascal_enum_values", Filter::Conf);

let mut source_type = cfg
.grab()
Expand All @@ -177,6 +184,7 @@ fn run() -> Result<()> {
ignore_groups,
keep_list,
strict,
pascal_enum_values,
output_dir: path.clone(),
source_type,
};
Expand Down
2 changes: 2 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct Config {
pub ignore_groups: bool,
pub keep_list: bool,
pub strict: bool,
pub pascal_enum_values: bool,
pub output_dir: PathBuf,
pub source_type: SourceType,
}
Expand All @@ -41,6 +42,7 @@ impl Default for Config {
ignore_groups: false,
keep_list: false,
strict: false,
pascal_enum_values: false,
output_dir: PathBuf::from("."),
source_type: SourceType::default(),
}
Expand Down