Skip to content

Commit 9204e9e

Browse files
committed
inline varibles in format
1 parent 2eced47 commit 9204e9e

File tree

8 files changed

+60
-82
lines changed

8 files changed

+60
-82
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
}

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);

src/generate/peripheral.rs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,8 @@ impl FieldRegions {
415415
.for_each(|r| {
416416
let new_ident = r.shortest_ident();
417417
warn!(
418-
"Found type name conflict with region {:?}, renamed to {:?}",
419-
r.ident, new_ident
418+
"Found type name conflict with region {:?}, renamed to {new_ident:?}",
419+
r.ident
420420
);
421421
r.ident = new_ident;
422422
});
@@ -425,19 +425,12 @@ impl FieldRegions {
425425
}
426426

427427
fn make_comment(size: u32, offset: u32, description: &str) -> String {
428+
let desc = util::escape_brackets(&util::respace(description));
428429
if size > 32 {
429-
format!(
430-
"0x{:02x}..0x{:02x} - {}",
431-
offset,
432-
offset + size / 8,
433-
util::escape_brackets(&util::respace(description)),
434-
)
430+
let end = offset + size / 8;
431+
format!("0x{offset:02x}..0x{end:02x} - {desc}")
435432
} else {
436-
format!(
437-
"0x{:02x} - {}",
438-
offset,
439-
util::escape_brackets(&util::respace(description)),
440-
)
433+
format!("0x{offset:02x} - {desc}")
441434
}
442435
}
443436

@@ -472,7 +465,7 @@ fn register_or_cluster_block(
472465
// Check if we need padding
473466
let pad = region.offset - last_end;
474467
if pad != 0 {
475-
let name = Ident::new(&format!("_reserved{}", i), span);
468+
let name = Ident::new(&format!("_reserved{i}"), span);
476469
let pad = util::hex(pad as u64);
477470
rbfs.extend(quote! {
478471
#name : [u8; #pad],
@@ -529,8 +522,7 @@ fn register_or_cluster_block(
529522
// nice identifier.
530523
let name = Ident::new(
531524
&format!(
532-
"_reserved_{}_{}",
533-
i,
525+
"_reserved_{i}_{}",
534526
region
535527
.compute_ident()
536528
.unwrap_or_else(|| format!("{}_{}", region.offset, region.end))
@@ -988,14 +980,14 @@ fn convert_svd_register(
988980
Register::Single(info) => {
989981
let info_name = info.fullname(ignore_group);
990982
let ty = name_to_wrapped_ty(&info_name, name)
991-
.with_context(|| format!("Error converting register name {}", info_name))?;
983+
.with_context(|| format!("Error converting register name {info_name}"))?;
992984
new_syn_field(&info_name.to_sanitized_snake_case(), ty)
993985
}
994986
Register::Array(info, array_info) => {
995987
let info_name = info.fullname(ignore_group);
996988
let nb_name = util::replace_suffix(&info_name, "");
997989
let ty = name_to_wrapped_ty(&nb_name, name)
998-
.with_context(|| format!("Error converting register name {}", nb_name))?;
990+
.with_context(|| format!("Error converting register name {nb_name}"))?;
999991
let array_ty = new_syn_array(ty, array_info.dim)?;
1000992

1001993
new_syn_field(&nb_name.to_sanitized_snake_case(), array_ty)
@@ -1013,8 +1005,7 @@ fn array_proxy(
10131005
let tys = name_to_ty_str(&ty_name, name);
10141006

10151007
let ap_path = parse_str::<syn::TypePath>(&format!(
1016-
"crate::ArrayProxy<{}, {}, {}>",
1017-
tys,
1008+
"crate::ArrayProxy<{tys}, {}, {}>",
10181009
array_info.dim,
10191010
util::hex(array_info.dim_increment as u64)
10201011
))?;
@@ -1139,5 +1130,5 @@ fn name_to_wrapped_ty(name: &str, ns: Option<&str>) -> Result<syn::Type> {
11391130
parse_str::<syn::TypePath>(&ident)
11401131
.map(syn::Type::Path)
11411132
.map_err(anyhow::Error::from)
1142-
.with_context(|| format!("Determining syn::TypePath from ident \"{}\" failed", ident))
1133+
.with_context(|| format!("Determining syn::TypePath from ident \"{ident}\" failed"))
11431134
}

src/generate/register.rs

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,11 @@ pub fn render(
229229
.iter()
230230
.map(|s| format!("[`{0}`](crate::generic::Reg::{0})", s))
231231
.collect::<Vec<_>>();
232-
let mut doc = format!("{}\n\nThis register you can {}. See [API](https://docs.rs/svd2rust/#read--modify--write-api).",
233-
&description, methods.join(", "));
232+
let mut doc = format!("{description}\n\nThis register you can {}. See [API](https://docs.rs/svd2rust/#read--modify--write-api).", methods.join(", "));
234233

235234
if name_snake_case != "cfg" {
236235
doc += format!(
237-
"\n\nFor information about available fields see [{0}](index.html) module",
238-
&name_snake_case
236+
"\n\nFor information about available fields see [{name_snake_case}](index.html) module"
239237
)
240238
.as_str();
241239
}
@@ -251,10 +249,8 @@ pub fn render(
251249
}
252250
}
253251

254-
let alias_doc = format!(
255-
"{} register accessor: an alias for `Reg<{}>`",
256-
name, name_constant_case_spec,
257-
);
252+
let alias_doc =
253+
format!("{name} register accessor: an alias for `Reg<{name_constant_case_spec}>`");
258254
out.extend(quote! {
259255
#[doc = #alias_doc]
260256
pub type #name_constant_case = crate::Reg<#name_snake_case::#name_constant_case_spec>;
@@ -269,10 +265,7 @@ pub fn render(
269265
});
270266

271267
if can_read {
272-
let doc = format!(
273-
"`read()` method returns [{0}::R](R) reader structure",
274-
&name_snake_case
275-
);
268+
let doc = format!("`read()` method returns [{name_snake_case}::R](R) reader structure",);
276269
mod_items.extend(quote! {
277270
#[doc = #doc]
278271
impl crate::Readable for #name_constant_case_spec {
@@ -281,10 +274,8 @@ pub fn render(
281274
});
282275
}
283276
if can_write {
284-
let doc = format!(
285-
"`write(|w| ..)` method takes [{0}::W](W) writer structure",
286-
&name_snake_case
287-
);
277+
let doc =
278+
format!("`write(|w| ..)` method takes [{name_snake_case}::W](W) writer structure",);
288279
mod_items.extend(quote! {
289280
#[doc = #doc]
290281
impl crate::Writable for #name_constant_case_spec {
@@ -293,7 +284,7 @@ pub fn render(
293284
});
294285
}
295286
if let Some(rv) = properties.reset_value.map(util::hex) {
296-
let doc = format!("`reset()` method sets {} to value {}", register.name, &rv);
287+
let doc = format!("`reset()` method sets {} to value {rv}", register.name);
297288
mod_items.extend(quote! {
298289
#[doc = #doc]
299290
impl crate::Resettable for #name_constant_case_spec {
@@ -416,7 +407,7 @@ pub fn fields(
416407
0
417408
};
418409
let suffixes: Vec<_> = de.indexes().collect();
419-
let suffixes_str = format!("({}-{})", first, first + de.dim - 1);
410+
let suffixes_str = format!("({first}-{})", first + de.dim - 1);
420411
Some((first, de.dim, de.dim_increment, suffixes, suffixes_str))
421412
}
422413
Field::Single(_) => {
@@ -453,12 +444,11 @@ pub fn fields(
453444
// the suffix string from field name is removed in brief description.
454445
let field_reader_brief = if let Some((_, _, _, _, suffixes_str)) = &field_dim {
455446
format!(
456-
"Fields `{}` reader - {}",
447+
"Fields `{}` reader - {description}",
457448
util::replace_suffix(&f.name, suffixes_str),
458-
description,
459449
)
460450
} else {
461-
format!("Field `{}` reader - {}", f.name, description)
451+
format!("Field `{}` reader - {description}", f.name)
462452
};
463453

464454
// get the type of value structure. It can be generated from either name field
@@ -468,11 +458,11 @@ pub fn fields(
468458
if let Some(enum_name) = &evs.name {
469459
let enum_name_constant_case = enum_name.to_sanitized_constant_case();
470460
let enum_value_read_ty =
471-
Ident::new(&format!("{}_A", enum_name_constant_case), span);
461+
Ident::new(&format!("{enum_name_constant_case}_A"), span);
472462
enum_value_read_ty
473463
} else {
474464
let derived_field_value_read_ty =
475-
Ident::new(&format!("{}_A", name_constant_case), span);
465+
Ident::new(&format!("{name_constant_case}_A"), span);
476466
derived_field_value_read_ty
477467
}
478468
} else {
@@ -604,14 +594,14 @@ pub fn fields(
604594

605595
let is_variant = Ident::new(
606596
&if sc.to_string().starts_with('_') {
607-
format!("is{}", sc)
597+
format!("is{sc}")
608598
} else {
609-
format!("is_{}", sc)
599+
format!("is_{sc}")
610600
},
611601
span,
612602
);
613603

614-
let doc = format!("Checks if the value of the field is `{}`", pc);
604+
let doc = format!("Checks if the value of the field is `{pc}`");
615605
enum_items.extend(quote! {
616606
#[doc = #doc]
617607
#inline
@@ -735,12 +725,11 @@ pub fn fields(
735725
// gets a brief of write proxy
736726
let field_writer_brief = if let Some((_, _, _, _, suffixes_str)) = &field_dim {
737727
format!(
738-
"Fields `{}` writer - {}",
728+
"Fields `{}` writer - {description}",
739729
util::replace_suffix(&f.name, suffixes_str),
740-
description,
741730
)
742731
} else {
743-
format!("Field `{}` writer - {}", f.name, description)
732+
format!("Field `{}` writer - {description}", f.name)
744733
};
745734

746735
let value_write_ty =
@@ -754,11 +743,11 @@ pub fn fields(
754743
if let Some(enum_name) = &evs.name {
755744
let enum_name_constant_case = enum_name.to_sanitized_constant_case();
756745
let enum_value_write_ty =
757-
Ident::new(&format!("{}_{}", enum_name_constant_case, ty_suffix), span);
746+
Ident::new(&format!("{enum_name_constant_case}_{ty_suffix}"), span);
758747
enum_value_write_ty
759748
} else {
760749
let derived_field_value_write_ty =
761-
Ident::new(&format!("{}_{}", name_constant_case, ty_suffix), span);
750+
Ident::new(&format!("{name_constant_case}_{ty_suffix}"), span);
762751
derived_field_value_write_ty
763752
}
764753
} else {
@@ -1007,7 +996,7 @@ impl Variant {
1007996
doc: ev
1008997
.description
1009998
.clone()
1010-
.unwrap_or_else(|| format!("`{:b}`", value)),
999+
.unwrap_or_else(|| format!("`{value:b}`")),
10111000
pc: Ident::new(
10121001
&(if pc {
10131002
ev.name.to_sanitized_pascal_case()
@@ -1039,9 +1028,9 @@ fn add_with_no_variants(
10391028
};
10401029

10411030
let desc = if let Some(rv) = reset_value {
1042-
format!("{}\n\nValue on reset: {}", desc, rv)
1031+
format!("{desc}\n\nValue on reset: {rv}")
10431032
} else {
1044-
desc.to_owned()
1033+
desc.to_string()
10451034
};
10461035

10471036
mod_items.extend(quote! {
@@ -1085,9 +1074,9 @@ fn add_from_variants(
10851074
}
10861075

10871076
let desc = if let Some(rv) = reset_value {
1088-
format!("{}\n\nValue on reset: {}", desc, rv)
1077+
format!("{desc}\n\nValue on reset: {rv}")
10891078
} else {
1090-
desc.to_owned()
1079+
desc.to_string()
10911080
};
10921081

10931082
mod_items.extend(quote! {
@@ -1140,9 +1129,9 @@ fn calculate_offset(
11401129

11411130
fn description_with_bits(description: &str, offset: u64, width: u32) -> String {
11421131
let mut res = if width == 1 {
1143-
format!("Bit {}", offset)
1132+
format!("Bit {offset}")
11441133
} else {
1145-
format!("Bits {}:{}", offset, offset + width as u64 - 1)
1134+
format!("Bits {offset}:{}", offset + width as u64 - 1)
11461135
};
11471136
if !description.is_empty() {
11481137
res.push_str(" - ");

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ fn run() -> Result<()> {
232232
if feature_group {
233233
let group_names: Vec<String> = group_names(&device)
234234
.iter()
235-
.map(|s| format!("{} = []\n", s))
235+
.map(|s| format!("{s} = []\n"))
236236
.collect();
237237
write!(
238238
File::create(path.join("features.toml"))?,
@@ -275,7 +275,7 @@ fn setup_logging<'a>(getter: &'a impl clap_conf::Getter<'a, String>) {
275275

276276
fn main() {
277277
if let Err(ref e) = run() {
278-
error!("{:?}", e);
278+
error!("{e:?}");
279279

280280
process::exit(1);
281281
}

0 commit comments

Comments
 (0)