Skip to content

Commit ff4c6c9

Browse files
bors[bot]luojia65
andauthored
Merge #626
626: Use one trait for all cases; use constant case for structures r=burrbull a=luojia65 This pull request is the first part of #612. It merges all `ToSanitizedXxxCase` traits in one `ToSanitizedCase` trait. It also replace all use of upper cases into constant cases, in case the source file did not provide underline between words. It rename variables related to cases, for example `name_sc` to `name_snake_case` to be explict. The changed code merges original case conversation traits into one trait: ```rust /// Convert self string into specific case without overlapping to svd2rust internal names pub trait ToSanitizedCase { /// Convert self into PascalCase. /// /// Use on name of enumeration values. fn to_sanitized_pascal_case(&self) -> Cow<str>; /// Convert self into CONSTANT_CASE. /// /// Use on name of reader structs, writer structs and enumerations. fn to_sanitized_constant_case(&self) -> Cow<str>; /// Convert self into snake_case, must use only if the target is used with extra prefix or suffix. fn to_sanitized_not_keyword_snake_case(&self) -> Cow<str>; /// Convert self into snake_case target and ensure target is not a Rust keyword. /// /// If the sanitized target is a Rust keyword, this function adds an underline `_` /// to it. /// /// Use on name of peripheral modules, register modules and field modules. fn to_sanitized_snake_case(&self) -> Cow<str> { let s = self.to_sanitized_not_keyword_snake_case(); sanitize_keyword(s) } } ``` Note on replacing upper case to constant case: if the source svd file provide enumeration name `InterruptEnable`, it converts to constant case `INTERRUPT_ENABLE` instead of less readable `INTERRUPTENABLE` in current code. Co-authored-by: luojia65 <[email protected]>
2 parents e2abc28 + 0f9eac4 commit ff4c6c9

File tree

6 files changed

+195
-153
lines changed

6 files changed

+195
-153
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+
- Use constant case for structure names; internal rearrangements for
11+
case conversation traits
1012
- Add new feature `feature_group` which will generate cfg attribute for
1113
every group name when it is on
1214

src/generate/device.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::svd::{array::names, Device, Peripheral};
2-
use crate::util::{ToSanitizedSnakeCase, U32Ext};
32
use proc_macro2::{Ident, Span, TokenStream};
43
use quote::{quote, ToTokens};
54

@@ -8,7 +7,7 @@ use std::borrow::Cow;
87
use std::fs::File;
98
use std::io::Write;
109

11-
use crate::util::{self, Config, ToSanitizedUpperCase};
10+
use crate::util::{self, Config, ToSanitizedCase, U32Ext};
1211
use crate::Target;
1312
use anyhow::{Context, Result};
1413

@@ -261,7 +260,7 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
261260
match p {
262261
Peripheral::Single(_p) => {
263262
let p_name = util::name_of(p, config.ignore_groups);
264-
let p = p_name.to_sanitized_upper_case();
263+
let p = p_name.to_sanitized_constant_case();
265264
let id = Ident::new(&p, Span::call_site());
266265
fields.extend(quote! {
267266
#[doc = #p]
@@ -272,7 +271,7 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
272271
}
273272
Peripheral::Array(_p, dim_element) => {
274273
let p_names: Vec<Cow<str>> = names(p, dim_element).map(|n| n.into()).collect();
275-
let p = p_names.iter().map(|p| p.to_sanitized_upper_case());
274+
let p = p_names.iter().map(|p| p.to_sanitized_constant_case());
276275
let ids_f = p.clone().map(|p| Ident::new(&p, Span::call_site()));
277276
let ids_e = ids_f.clone();
278277
fields.extend(quote! {

src/generate/interrupt.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use cast::u64;
66
use proc_macro2::{Ident, Span, TokenStream};
77
use quote::quote;
88

9-
use crate::util::{self, ToSanitizedSnakeCase, ToSanitizedUpperCase};
9+
use crate::util::{self, ToSanitizedCase};
1010
use crate::{Config, Target};
1111
use anyhow::Result;
1212

@@ -43,8 +43,8 @@ pub fn render(
4343
}
4444
pos += 1;
4545

46-
let name_uc = Ident::new(
47-
&interrupt.0.name.to_sanitized_upper_case(),
46+
let name_constant_case = Ident::new(
47+
&interrupt.0.name.to_sanitized_constant_case(),
4848
Span::call_site(),
4949
);
5050
let description = format!(
@@ -78,25 +78,25 @@ pub fn render(
7878
variants.extend(quote! {
7979
#[doc = #description]
8080
#feature_attribute
81-
#name_uc = #value,
81+
#name_constant_case = #value,
8282
});
8383

8484
from_arms.extend(quote! {
8585
#feature_attribute
86-
#value => Ok(Interrupt::#name_uc),
86+
#value => Ok(Interrupt::#name_constant_case),
8787
});
8888

8989
if feature_attribute_flag {
9090
elements.extend(quote! {
9191
#not_feature_attribute
9292
Vector { _reserved: 0 },
9393
#feature_attribute
94-
Vector { _handler: #name_uc },
94+
Vector { _handler: #name_constant_case },
9595
});
9696
} else {
97-
elements.extend(quote!(Vector { _handler: #name_uc },));
97+
elements.extend(quote!(Vector { _handler: #name_constant_case },));
9898
}
99-
names.push(name_uc);
99+
names.push(name_constant_case);
100100
names_cfg_attr.push(feature_attribute);
101101
}
102102

src/generate/peripheral.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use quote::{quote, ToTokens};
1212
use syn::{parse_str, Token};
1313

1414
use crate::util::{
15-
self, handle_cluster_error, handle_reg_error, unsuffixed, Config, FullName,
16-
ToSanitizedSnakeCase, ToSanitizedUpperCase, BITS_PER_BYTE,
15+
self, handle_cluster_error, handle_reg_error, unsuffixed, Config, FullName, ToSanitizedCase,
16+
BITS_PER_BYTE,
1717
};
1818
use anyhow::{anyhow, bail, Context, Result};
1919

@@ -44,16 +44,16 @@ pub fn render(
4444

4545
let name = util::name_of(p, config.ignore_groups);
4646
let span = Span::call_site();
47-
let name_str = name.to_sanitized_upper_case();
48-
let name_pc = Ident::new(&name_str, span);
47+
let name_str = name.to_sanitized_constant_case();
48+
let name_constant_case = Ident::new(&name_str, span);
4949
let address = util::hex(p.base_address as u64);
5050
let description = util::respace(p.description.as_ref().unwrap_or(&p.name));
5151

52-
let name_sc = Ident::new(&name.to_sanitized_snake_case(), span);
52+
let name_snake_case = Ident::new(&name.to_sanitized_snake_case(), span);
5353
let (derive_regs, base) = if let (Some(df), None) = (p_derivedfrom, &p_original.registers) {
5454
(true, Ident::new(&df.name.to_sanitized_snake_case(), span))
5555
} else {
56-
(false, name_sc.clone())
56+
(false, name_snake_case.clone())
5757
};
5858

5959
let feature_attribute = if config.feature_group && p.group_name.is_some() {
@@ -66,8 +66,8 @@ pub fn render(
6666
match p_original {
6767
Peripheral::Array(p, dim) => {
6868
let names: Vec<Cow<str>> = names(p, dim).map(|n| n.into()).collect();
69-
let names_str = names.iter().map(|n| n.to_sanitized_upper_case());
70-
let names_pc = names_str.clone().map(|n| Ident::new(&n, span));
69+
let names_str = names.iter().map(|n| n.to_sanitized_constant_case());
70+
let names_constant_case = names_str.clone().map(|n| Ident::new(&n, span));
7171
let addresses =
7272
(0..=dim.dim).map(|i| util::hex(p.base_address + (i * dim.dim_increment) as u64));
7373

@@ -76,13 +76,13 @@ pub fn render(
7676
#(
7777
#[doc = #description]
7878
#feature_attribute
79-
pub struct #names_pc { _marker: PhantomData<*const ()> }
79+
pub struct #names_constant_case { _marker: PhantomData<*const ()> }
8080

8181
#feature_attribute
82-
unsafe impl Send for #names_pc {}
82+
unsafe impl Send for #names_constant_case {}
8383

8484
#feature_attribute
85-
impl #names_pc {
85+
impl #names_constant_case {
8686
///Pointer to the register block
8787
pub const PTR: *const #base::RegisterBlock = #addresses as *const _;
8888

@@ -94,7 +94,7 @@ pub fn render(
9494
}
9595

9696
#feature_attribute
97-
impl Deref for #names_pc {
97+
impl Deref for #names_constant_case {
9898
type Target = #base::RegisterBlock;
9999

100100
#[inline(always)]
@@ -104,7 +104,7 @@ pub fn render(
104104
}
105105

106106
#feature_attribute
107-
impl core::fmt::Debug for #names_pc {
107+
impl core::fmt::Debug for #names_constant_case {
108108
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
109109
f.debug_struct(#names_str).finish()
110110
}
@@ -117,13 +117,13 @@ pub fn render(
117117
out.extend(quote! {
118118
#[doc = #description]
119119
#feature_attribute
120-
pub struct #name_pc { _marker: PhantomData<*const ()> }
120+
pub struct #name_constant_case { _marker: PhantomData<*const ()> }
121121

122122
#feature_attribute
123-
unsafe impl Send for #name_pc {}
123+
unsafe impl Send for #name_constant_case {}
124124

125125
#feature_attribute
126-
impl #name_pc {
126+
impl #name_constant_case {
127127
///Pointer to the register block
128128
pub const PTR: *const #base::RegisterBlock = #address as *const _;
129129

@@ -135,7 +135,7 @@ pub fn render(
135135
}
136136

137137
#feature_attribute
138-
impl Deref for #name_pc {
138+
impl Deref for #name_constant_case {
139139
type Target = #base::RegisterBlock;
140140

141141
#[inline(always)]
@@ -145,7 +145,7 @@ pub fn render(
145145
}
146146

147147
#feature_attribute
148-
impl core::fmt::Debug for #name_pc {
148+
impl core::fmt::Debug for #name_constant_case {
149149
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
150150
f.debug_struct(#name_str).finish()
151151
}
@@ -161,7 +161,7 @@ pub fn render(
161161
out.extend(quote! {
162162
#[doc = #description]
163163
#feature_attribute
164-
pub use #base as #name_sc;
164+
pub use #base as #name_snake_case;
165165
});
166166
return Ok(out);
167167
}
@@ -235,7 +235,7 @@ pub fn render(
235235
out.extend(quote! {
236236
#[doc = #description]
237237
#feature_attribute
238-
pub mod #name_sc #open
238+
pub mod #name_snake_case #open
239239
});
240240

241241
out.extend(mod_items);
@@ -627,7 +627,7 @@ fn register_or_cluster_block(
627627

628628
let name = Ident::new(
629629
&match name {
630-
Some(name) => name.to_sanitized_upper_case(),
630+
Some(name) => name.to_sanitized_constant_case(),
631631
None => "RegisterBlock".into(),
632632
},
633633
span,
@@ -963,7 +963,7 @@ fn cluster_block(
963963
) -> Result<TokenStream> {
964964
let mut mod_items = TokenStream::new();
965965

966-
// name_sc needs to take into account array type.
966+
// name_snake_case needs to take into account array type.
967967
let description =
968968
util::escape_brackets(util::respace(c.description.as_ref().unwrap_or(&c.name)).as_ref());
969969

@@ -975,7 +975,7 @@ fn cluster_block(
975975
},
976976
"",
977977
);
978-
let name_sc = Ident::new(&mod_name.to_sanitized_snake_case(), Span::call_site());
978+
let name_snake_case = Ident::new(&mod_name.to_sanitized_snake_case(), Span::call_site());
979979

980980
let reg_block = register_or_cluster_block(&c.children, Some(&mod_name), config)?;
981981

@@ -1006,7 +1006,7 @@ fn cluster_block(
10061006

10071007
///Register block
10081008
#[doc = #description]
1009-
pub mod #name_sc {
1009+
pub mod #name_snake_case {
10101010
#mod_items
10111011
}
10121012
})
@@ -1157,10 +1157,10 @@ fn name_to_ty_str<'a, 'b>(name: &'a str, ns: Option<&'b str>) -> Cow<'a, str> {
11571157
String::from("self::")
11581158
+ &ns.to_sanitized_snake_case()
11591159
+ "::"
1160-
+ &name.to_sanitized_upper_case(),
1160+
+ &name.to_sanitized_constant_case(),
11611161
)
11621162
} else {
1163-
name.to_sanitized_upper_case()
1163+
name.to_sanitized_constant_case()
11641164
}
11651165
}
11661166

@@ -1175,13 +1175,13 @@ fn name_to_wrapped_ty_str(name: &str, ns: Option<&str>) -> String {
11751175
"crate::Reg<self::{}::{}::{}_SPEC>",
11761176
&ns.to_sanitized_snake_case(),
11771177
&name.to_sanitized_snake_case(),
1178-
&name.to_sanitized_upper_case(),
1178+
&name.to_sanitized_constant_case(),
11791179
)
11801180
} else {
11811181
format!(
11821182
"crate::Reg<{}::{}_SPEC>",
11831183
&name.to_sanitized_snake_case(),
1184-
&name.to_sanitized_upper_case(),
1184+
&name.to_sanitized_constant_case(),
11851185
)
11861186
}
11871187
}

0 commit comments

Comments
 (0)