Skip to content

Commit e3f8f42

Browse files
authored
Merge pull request #120 from stm32-rs/fmt
formatted strings
2 parents 76bd2cf + b3dbd01 commit e3f8f42

File tree

11 files changed

+158
-230
lines changed

11 files changed

+158
-230
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ more information on creating patches.
3434

3535
## Getting Started with Rust version
3636

37-
This crate is guaranteed to compile on stable Rust 1.46.0 and up. To install:
37+
This crate is guaranteed to compile on stable Rust 1.58.0 and up. To install:
3838

3939
```bash
4040
$ cargo install svdtools

src/common/str_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ pub fn get_description(opt_str: &Option<String>) -> String {
1616

1717
/// Make everything uppercase except first two character, which should be "0x"
1818
pub fn format_address(hex_address: u64) -> String {
19-
let addr = format! {"{:x}", hex_address};
19+
let addr = format! {"{hex_address:x}"};
2020
let addr = addr.to_uppercase();
21-
format!("0x{}", addr)
21+
format!("0x{addr}")
2222
}
2323

2424
#[cfg(test)]

src/common/svd_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ pub fn access_with_brace(access: Option<Access>) -> String {
2121
if access.is_empty() {
2222
access.to_string()
2323
} else {
24-
format!(" ({})", access)
24+
format!(" ({access})")
2525
}
2626
}

src/makedeps/makedeps_cli.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub fn makedeps(yaml_file: &Path, deps_file: &Path) -> Result<()> {
4545
write_file(deps_file, deps)?;
4646
Ok(())
4747
}
48-
_ => Err(anyhow!("Incorrect yaml {:?}", yaml_file)),
48+
_ => Err(anyhow!("Incorrect yaml {yaml_file:?}")),
4949
}
5050
}
5151

src/mmap/mmap_cli.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn parse_device(svd_file: &Path) -> Result<()> {
1111
match get_text(&mut file) {
1212
Err(e) => {
1313
let path_str = svd_file.display();
14-
Err(e).with_context(|| format!("Parsing {}", path_str))
14+
Err(e).with_context(|| format!("Parsing {path_str}"))
1515
}
1616
Ok(text) => {
1717
println!("{}", text);
@@ -71,8 +71,8 @@ fn get_interrupts(peripheral: &Peripheral, mmap: &mut Vec<String>) {
7171
for i in &peripheral.interrupt {
7272
let description = str_utils::get_description(&i.description);
7373
let text = format!(
74-
"INTERRUPT {:03}: {} ({}): {}",
75-
i.value, i.name, peripheral.name, description
74+
"INTERRUPT {:03}: {} ({}): {description}",
75+
i.value, i.name, peripheral.name
7676
);
7777
mmap.push(text);
7878
}
@@ -95,10 +95,7 @@ fn get_registers(
9595
Register::Single(r) => {
9696
let addr = str_utils::format_address(first_addr);
9797
let rname = r.name.to_string() + suffix;
98-
let text = format!(
99-
"{} B REGISTER {}{}: {}",
100-
addr, rname, access, description
101-
);
98+
let text = format!("{addr} B REGISTER {rname}{access}: {description}");
10299
mmap.push(text);
103100
get_fields(r, &addr, mmap);
104101
}
@@ -109,10 +106,8 @@ fn get_registers(
109106
);
110107
let rname = r.name.replace("%s", &idx);
111108
let description = description.replace("%s", &idx);
112-
let text = format!(
113-
"{} B REGISTER {}{}: {}",
114-
addr, rname, access, description
115-
);
109+
let text =
110+
format!("{addr} B REGISTER {rname}{access}: {description}");
116111
mmap.push(text);
117112
get_fields(r, &addr, mmap);
118113
}
@@ -125,7 +120,8 @@ fn get_registers(
125120
match c {
126121
Cluster::Single(c) => {
127122
let addr = str_utils::format_address(first_addr);
128-
let text = format!("{} B CLUSTER {}: {}", addr, c.name, description);
123+
let cname = &c.name;
124+
let text = format!("{addr} B CLUSTER {cname}: {description}");
129125
mmap.push(text);
130126
get_registers(first_addr, Some(&c.children), "", mmap);
131127
}
@@ -135,8 +131,7 @@ fn get_registers(
135131
let addr = str_utils::format_address(caddr);
136132
let cname = c.name.replace("%s", &idx);
137133
let description = description.replace("%s", &idx);
138-
let text =
139-
format!("{} B CLUSTER {}: {}", addr, cname, description);
134+
let text = format!("{addr} B CLUSTER {cname}: {description}");
140135
mmap.push(text);
141136
get_registers(caddr, Some(&c.children), &idx, mmap);
142137
}
@@ -156,19 +151,20 @@ fn get_fields(register: &RegisterInfo, addr: &str, mmap: &mut Vec<String>) {
156151
match f {
157152
Field::Single(f) => {
158153
let bit_offset = f.bit_range.offset;
154+
let bit_width = f.bit_range.width;
155+
let fname = &f.name;
159156
let text = format!(
160-
"{} C FIELD {:02}w{:02} {}{}: {}",
161-
addr, bit_offset, f.bit_range.width, f.name, access, description
157+
"{addr} C FIELD {bit_offset:02}w{bit_width:02} {fname}{access}: {description}"
162158
);
163159
mmap.push(text);
164160
}
165161
Field::Array(f, d) => {
166162
for (i, idx) in d.indexes().enumerate() {
167163
let bit_offset = f.bit_range.offset + (i as u32) * d.dim_increment;
164+
let bit_width = f.bit_range.width;
168165
let fname = f.name.replace("%s", &idx);
169166
let text = format!(
170-
"{} C FIELD {:02}w{:02} {}{}: {}",
171-
addr, bit_offset, f.bit_range.width, fname, access, description
167+
"{addr} C FIELD {bit_offset:02}w{bit_width:02} {fname}{access}: {description}"
172168
);
173169
mmap.push(text);
174170
}

src/patch/device.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl DeviceExt for Device {
6666
// Handle any deletions
6767
for pspec in device.str_vec_iter("_delete") {
6868
self.delete_peripheral(pspec)
69-
.with_context(|| format!("Deleting peripheral matched to `{}`", pspec))?;
69+
.with_context(|| format!("Deleting peripheral matched to `{pspec}`"))?;
7070
}
7171

7272
// Handle any copied peripherals
@@ -77,7 +77,7 @@ impl DeviceExt for Device {
7777
val.hash()?,
7878
Path::new(device.get_str("_path")?.unwrap_or(".")),
7979
)
80-
.with_context(|| format!("Copying peripheral `{}`", pname))?;
80+
.with_context(|| format!("Copying peripheral `{pname}`"))?;
8181
}
8282

8383
// Handle any modifications
@@ -92,7 +92,7 @@ impl DeviceExt for Device {
9292
let pspec = pspec.str()?;
9393
self.modify_peripheral(pspec, pmod.hash()?)
9494
.with_context(|| {
95-
format!("Modifying peripherals matched to `{}`", pspec)
95+
format!("Modifying peripherals matched to `{pspec}`")
9696
})?;
9797
}
9898
}
@@ -115,40 +115,37 @@ impl DeviceExt for Device {
115115

116116
_ => self
117117
.modify_peripheral(key, val.hash()?)
118-
.with_context(|| format!("Modifying peripherals matched to `{}`", key))?,
118+
.with_context(|| format!("Modifying peripherals matched to `{key}`"))?,
119119
}
120120
}
121121

122122
// Handle field clearing
123123
for pspec in device.str_vec_iter("_clear_fields") {
124124
self.clear_fields(pspec).with_context(|| {
125-
format!(
126-
"Clearing contents of fields in peripherals matched to `{}` ",
127-
pspec
128-
)
125+
format!("Clearing contents of fields in peripherals matched to `{pspec}` ")
129126
})?;
130127
}
131128

132129
// Handle any new peripherals (!)
133130
for (pname, padd) in device.hash_iter("_add") {
134131
let pname = pname.str()?;
135132
self.add_peripheral(pname, padd.hash()?)
136-
.with_context(|| format!("Adding peripheral `{}`", pname))?;
133+
.with_context(|| format!("Adding peripheral `{pname}`"))?;
137134
}
138135

139136
// Handle any derived peripherals
140137
for (pname, pderive) in device.hash_iter("_derive") {
141138
let pname = pname.str()?;
142139
self.derive_peripheral(pname, pderive)
143-
.with_context(|| format!("Deriving peripheral `{}` from `{:?}`", pname, pderive))?;
140+
.with_context(|| format!("Deriving peripheral `{pname}` from `{pderive:?}`"))?;
144141
}
145142

146143
// Handle any rebased peripherals
147144
for (pname, pold) in device.hash_iter("_rebase") {
148145
let pname = pname.str()?;
149146
let pold = pold.str()?;
150147
self.rebase_peripheral(pname, pold)
151-
.with_context(|| format!("Rebasing peripheral from `{}` to `{}`", pold, pname))?;
148+
.with_context(|| format!("Rebasing peripheral from `{pold}` to `{pname}`"))?;
152149
}
153150

154151
// Now process all peripherals
@@ -157,7 +154,7 @@ impl DeviceExt for Device {
157154
if !periphspec.starts_with('_') {
158155
//val["_path"] = device["_path"]; // TODO: check
159156
self.process_peripheral(periphspec, val.hash()?, update_fields)
160-
.with_context(|| format!("According to `{}`", periphspec))?;
157+
.with_context(|| format!("According to `{periphspec}`"))?;
161158
}
162159
}
163160

@@ -181,16 +178,16 @@ impl DeviceExt for Device {
181178
let mut contents = String::new();
182179
(&f).read_to_string(&mut contents).unwrap();
183180
let filedev = svd_parser::parse(&contents)
184-
.with_context(|| format!("Parsing file {}", contents))?;
181+
.with_context(|| format!("Parsing file {contents}"))?;
185182
filedev
186183
.get_peripheral(pcopyname)
187-
.ok_or_else(|| anyhow!("peripheral {} not found", pcopyname))?
184+
.ok_or_else(|| anyhow!("peripheral {pcopyname} not found"))?
188185
.clone()
189186
}
190187
[pcopyname] => {
191188
let mut new = self
192189
.get_peripheral(pcopyname)
193-
.ok_or_else(|| anyhow!("peripheral {} not found", pcopyname))?
190+
.ok_or_else(|| anyhow!("peripheral {pcopyname} not found"))?
194191
.clone();
195192
// When copying from a peripheral in the same file, remove any interrupts.
196193
new.interrupt = Vec::new();
@@ -265,7 +262,7 @@ impl DeviceExt for Device {
265262

266263
fn add_peripheral(&mut self, pname: &str, padd: &Hash) -> PatchResult {
267264
if self.get_peripheral(pname).is_some() {
268-
return Err(anyhow!("device already has a peripheral {}", pname));
265+
return Err(anyhow!("device already has a peripheral {pname}"));
269266
}
270267

271268
self.peripherals.push(
@@ -300,7 +297,7 @@ impl DeviceExt for Device {
300297

301298
if !pderive.contains('.') {
302299
self.get_peripheral(pderive)
303-
.ok_or_else(|| anyhow!("peripheral {} not found", pderive))?;
300+
.ok_or_else(|| anyhow!("peripheral {pderive} not found"))?;
304301
}
305302

306303
match self.get_mut_peripheral(pname) {
@@ -324,7 +321,7 @@ impl DeviceExt for Device {
324321
fn rebase_peripheral(&mut self, pnew: &str, pold: &str) -> PatchResult {
325322
let old = self
326323
.get_mut_peripheral(pold)
327-
.ok_or_else(|| anyhow!("peripheral {} not found", pold))?;
324+
.ok_or_else(|| anyhow!("peripheral {pold} not found"))?;
328325
let mut d = std::mem::replace(
329326
old,
330327
PeripheralInfo::builder()
@@ -341,7 +338,7 @@ impl DeviceExt for Device {
341338
);
342339
let new = self
343340
.get_mut_peripheral(pnew)
344-
.ok_or_else(|| anyhow!("peripheral {} not found", pnew))?;
341+
.ok_or_else(|| anyhow!("peripheral {pnew} not found"))?;
345342
d.name = new.name.clone();
346343
d.base_address = new.base_address;
347344
d.interrupt = new.interrupt.clone();
@@ -380,7 +377,7 @@ impl DeviceExt for Device {
380377
.with_context(|| format!("Processing peripheral `{}`", ptag.name))?;
381378
}
382379
if pcount == 0 {
383-
Err(anyhow!("Could not find `{}`", pspec))
380+
Err(anyhow!("Could not find `{pspec}`"))
384381
} else {
385382
Ok(())
386383
}

src/patch/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub fn yaml_includes(parent: &mut Hash) -> Result<Vec<PathBuf>> {
8282
for relpath in inc {
8383
let relpath = relpath.as_str().unwrap();
8484
let path = abspath(&self_path, Path::new(relpath))
85-
.with_context(|| anyhow!("Opening file \"{}\" from file {:?}", relpath, self_path))?;
85+
.with_context(|| anyhow!("Opening file \"{relpath}\" from file {self_path:?}"))?;
8686
if included.contains(&path) {
8787
continue;
8888
}
@@ -251,10 +251,7 @@ fn make_ev_array(values: &Hash) -> Result<EnumeratedValuesBuilder> {
251251
let vd = vd.vec()?;
252252
let value = vd[0].i64()? as u64;
253253
let description = vd.get(1).and_then(Yaml::as_str).ok_or_else(|| {
254-
anyhow!(
255-
"enumeratedValue can't have empty description for value {}",
256-
value
257-
)
254+
anyhow!("enumeratedValue can't have empty description for value {value}")
258255
})?;
259256
use std::collections::btree_map::Entry;
260257
match h.entry(value) {

0 commit comments

Comments
 (0)