Skip to content

Commit 3846eda

Browse files
bors[bot]burrbull
andauthored
Merge #638
638: inline variables in format && to_xx_case_ident && field_arrays r=therealprof a=burrbull r? `@Emilgardis` Co-authored-by: Andrey Zgarbul <[email protected]>
2 parents 2eced47 + f9e8a4c commit 3846eda

File tree

9 files changed

+202
-299
lines changed

9 files changed

+202
-299
lines changed

CHANGELOG.md

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

88
## [Unreleased]
99

10+
- Use inlined variables in `format!` (Rust 1.58)
1011
- Refactor `periperal.rs`
1112
- use `svd_parser::expand::Index` for derive
1213
- Generated enum names now consider `name` field in `enumeratedValues`

ci/svd2rust-regress/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,15 @@ fn validate_architecture(s: String) -> Result<(), String> {
9494
if tests::TESTS.iter().any(|t| format!("{:?}", t.arch) == s) {
9595
Ok(())
9696
} else {
97-
Err(format!("Architecture `{}` is not a valid value", s))
97+
Err(format!("Architecture `{s}` is not a valid value"))
9898
}
9999
}
100100

101101
fn validate_manufacturer(s: String) -> Result<(), String> {
102102
if tests::TESTS.iter().any(|t| format!("{:?}", t.mfgr) == s) {
103103
Ok(())
104104
} else {
105-
Err(format!("Manufacturer `{}` is not a valid value", s))
105+
Err(format!("Manufacturer `{s}` is not a valid value"))
106106
}
107107
}
108108

ci/svd2rust-regress/src/svd_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ fn path_helper_base(base: &PathBuf, input: &[&str]) -> PathBuf {
2828

2929
/// Create and write to file
3030
fn file_helper(payload: &str, path: &PathBuf) -> Result<()> {
31-
let mut f = File::create(path).chain_err(|| format!("Failed to create {:?}", path))?;
31+
let mut f = File::create(path).chain_err(|| format!("Failed to create {path:?}"))?;
3232

3333
f.write_all(payload.as_bytes())
34-
.chain_err(|| format!("Failed to write to {:?}", path))?;
34+
.chain_err(|| format!("Failed to write to {path:?}"))?;
3535

3636
Ok(())
3737
}

ci/svd2rust-regress/src/tests.rs

Lines changed: 36 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use inflections::Inflect;
2+
use std::borrow::Cow;
23

34
#[derive(Debug, PartialEq)]
45
pub enum Architecture {
@@ -68,7 +69,9 @@ impl TestCase {
6869
}
6970

7071
pub fn name(&self) -> String {
71-
format!("{:?}-{}", self.mfgr, self.chip.replace(".", "_")).to_sanitized_snake_case()
72+
format!("{:?}-{}", self.mfgr, self.chip.replace(".", "_"))
73+
.to_sanitized_snake_case()
74+
.into()
7275
}
7376
}
7477

@@ -80,95 +83,52 @@ use self::RunWhen::*;
8083
/// that are not valid in Rust ident
8184
const BLACKLIST_CHARS: &[char] = &['(', ')', '[', ']'];
8285

83-
/// Lovingly stolen from `svd2rust`. Probably could be `Cow`
84-
pub trait ToSanitizedSnakeCase {
85-
fn to_sanitized_snake_case(&self) -> String;
86+
/// Lovingly stolen from `svd2rust`
87+
pub trait ToSanitizedCase {
88+
fn to_sanitized_not_keyword_snake_case(&self) -> Cow<str>;
89+
fn to_sanitized_snake_case(&self) -> Cow<str> {
90+
let s = self.to_sanitized_not_keyword_snake_case();
91+
sanitize_keyword(s)
92+
}
8693
}
8794

88-
impl ToSanitizedSnakeCase for str {
89-
fn to_sanitized_snake_case(&self) -> String {
90-
macro_rules! keywords {
91-
($s:expr, $($kw:ident),+,) => {
92-
String::from(match &$s.to_lowercase()[..] {
93-
$(stringify!($kw) => concat!(stringify!($kw), "_")),+,
94-
_ => return String::from($s.to_snake_case())
95-
})
96-
}
97-
}
95+
impl ToSanitizedCase for str {
96+
fn to_sanitized_not_keyword_snake_case(&self) -> Cow<str> {
97+
const INTERNALS: [&str; 4] = ["set_bit", "clear_bit", "bit", "bits"];
9898

9999
let s = self.replace(BLACKLIST_CHARS, "");
100-
101100
match s.chars().next().unwrap_or('\0') {
102101
'0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => {
103-
format!("_{}", s.to_snake_case())
102+
format!("_{}", s.to_snake_case()).into()
104103
}
105104
_ => {
106-
keywords! {
107-
s,
108-
abstract,
109-
alignof,
110-
as,
111-
r#async,
112-
r#await,
113-
become,
114-
box,
115-
break,
116-
const,
117-
continue,
118-
crate,
119-
do,
120-
else,
121-
enum,
122-
extern,
123-
false,
124-
final,
125-
fn,
126-
for,
127-
if,
128-
impl,
129-
in,
130-
let,
131-
loop,
132-
macro,
133-
match,
134-
mod,
135-
move,
136-
mut,
137-
offsetof,
138-
override,
139-
priv,
140-
proc,
141-
pub,
142-
pure,
143-
ref,
144-
return,
145-
self,
146-
sizeof,
147-
static,
148-
struct,
149-
super,
150-
trait,
151-
true,
152-
r#try,
153-
type,
154-
typeof,
155-
unsafe,
156-
unsized,
157-
use,
158-
virtual,
159-
where,
160-
while,
161-
yield,
162-
set_bit,
163-
clear_bit,
164-
bit,
165-
bits,
105+
let s = Cow::from(s.to_snake_case());
106+
if INTERNALS.contains(&s.as_ref()) {
107+
s + "_"
108+
} else {
109+
s
166110
}
167111
}
168112
}
169113
}
170114
}
171115

116+
pub fn sanitize_keyword(sc: Cow<str>) -> Cow<str> {
117+
const KEYWORDS: [&str; 55] = [
118+
"abstract", "alignof", "as", "async", "await", "become", "box", "break", "const",
119+
"continue", "crate", "do", "dyn", "else", "enum", "extern", "false", "final", "fn", "for",
120+
"if", "impl", "in", "let", "loop", "macro", "match", "mod", "move", "mut", "offsetof",
121+
"override", "priv", "proc", "pub", "pure", "ref", "return", "self", "sizeof", "static",
122+
"struct", "super", "trait", "true", "try", "type", "typeof", "unsafe", "unsized", "use",
123+
"virtual", "where", "while", "yield",
124+
];
125+
if KEYWORDS.contains(&sc.as_ref()) {
126+
sc + "_"
127+
} else {
128+
sc
129+
}
130+
}
131+
172132
// NOTE: All chip names must be unique!
173133
pub const TESTS: &[&TestCase] = &[
174134
// BAD-SVD missing resetValue

src/generate/device.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
3030

3131
let doc = format!(
3232
"Peripheral access API for {0} microcontrollers \
33-
(generated using svd2rust v{1}{2})\n\n\
33+
(generated using svd2rust v{1}{commit_info})\n\n\
3434
You can find an overview of the generated API [here].\n\n\
3535
API features to be included in the [next] svd2rust \
3636
release can be generated by cloning the svd2rust [repository], \
@@ -40,7 +40,6 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
4040
[repository]: https://github.com/rust-embedded/svd2rust",
4141
d.name.to_uppercase(),
4242
env!("CARGO_PKG_VERSION"),
43-
commit_info
4443
);
4544

4645
if config.target == Target::Msp430 {
@@ -212,7 +211,9 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
212211
)?);
213212

214213
for p in &d.peripherals {
215-
if config.target == Target::CortexM && core_peripherals.contains(&&*p.name.to_uppercase()) {
214+
if config.target == Target::CortexM
215+
&& core_peripherals.contains(&p.name.to_uppercase().as_ref())
216+
{
216217
// Core peripherals are handled above
217218
continue;
218219
}
@@ -224,15 +225,11 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
224225
let descrip = p.description.as_deref().unwrap_or("No description");
225226
let group_name = p.group_name.as_deref().unwrap_or("No group name");
226227
let mut context_string = format!(
227-
"Rendering error at peripheral\nName: {}\nDescription: {}\nGroup: {}",
228-
p.name, descrip, group_name
228+
"Rendering error at peripheral\nName: {}\nDescription: {descrip}\nGroup: {group_name}",
229+
p.name
229230
);
230-
if p.derived_from.is_some() {
231-
context_string = format!(
232-
"{}\nDerived from: {}",
233-
context_string,
234-
p.derived_from.as_deref().unwrap()
235-
);
231+
if let Some(dname) = p.derived_from.as_ref() {
232+
context_string = format!("{context_string}\nDerived from: {dname}");
236233
}
237234
let mut e = Err(e);
238235
e = e.with_context(|| context_string);

0 commit comments

Comments
 (0)