style: simplify string interpolation

This commit is contained in:
Hamir Mahal 2024-05-31 13:26:35 -07:00 committed by David Peter
parent e8ff88dad1
commit ef1263279d
13 changed files with 30 additions and 33 deletions

View File

@ -54,7 +54,7 @@ fn run_command_and_measure_common(
);
let result = execute_and_measure(command)
.with_context(|| format!("Failed to run command '{}'", command_name))?;
.with_context(|| format!("Failed to run command '{command_name}'"))?;
if command_failure_action == CmdFailureAction::RaiseError && !result.status.success() {
bail!(
@ -62,7 +62,7 @@ fn run_command_and_measure_common(
Alternatively, use the '--show-output' option to debug what went wrong.",
result.status.code().map_or(
"The process has been terminated by a signal".into(),
|c| format!("Command terminated with non-zero exit code: {}", c)
|c| format!("Command terminated with non-zero exit code: {c}")
)
);
}

View File

@ -312,7 +312,7 @@ impl<'a> Benchmark<'a> {
let (mean_str, time_unit) = format_duration_unit(t_mean, self.options.time_unit);
let min_str = format_duration(t_min, Some(time_unit));
let max_str = format_duration(t_max, Some(time_unit));
let num_str = format!("{} runs", t_num);
let num_str = format!("{t_num} runs");
let user_str = format_duration(user_mean, Some(time_unit));
let system_str = format_duration(system_mean, Some(time_unit));

View File

@ -86,7 +86,7 @@ impl<'a> Scheduler<'a> {
"{}{} times faster than {}",
format!("{:8.2}", item.relative_speed).bold().green(),
if let Some(stddev) = item.relative_speed_stddev {
format!(" ± {}", format!("{:.2}", stddev).green())
format!(" ± {}", format!("{stddev:.2}").green())
} else {
"".into()
},
@ -104,7 +104,7 @@ impl<'a> Scheduler<'a> {
if item.is_fastest {
" ".into()
} else if let Some(stddev) = item.relative_speed_stddev {
format!(" ± {}", format!("{:5.2}", stddev).green())
format!(" ± {}", format!("{stddev:5.2}").green())
} else {
" ".into()
},

View File

@ -62,13 +62,13 @@ impl<'a> Command<'a> {
let parameters = self
.get_unused_parameters()
.fold(String::new(), |output, (parameter, value)| {
output + &format!("{} = {}, ", parameter, value)
output + &format!("{parameter} = {value}, ")
});
let parameters = parameters.trim_end_matches(", ");
let parameters = if parameters.is_empty() {
"".into()
} else {
format!(" ({})", parameters)
format!(" ({parameters})")
};
format!("{}{}", self.get_name(), parameters)
@ -81,7 +81,7 @@ impl<'a> Command<'a> {
pub fn get_command(&self) -> Result<std::process::Command> {
let command_line = self.get_command_line();
let mut tokens = shell_words::split(&command_line)
.with_context(|| format!("Failed to parse command '{}'", command_line))?
.with_context(|| format!("Failed to parse command '{command_line}'"))?
.into_iter();
if let Some(program_name) = tokens.next() {
@ -100,7 +100,7 @@ impl<'a> Command<'a> {
pub fn get_unused_parameters(&self) -> impl Iterator<Item = &(&'a str, ParameterValue)> {
self.parameters
.iter()
.filter(move |(parameter, _)| !self.expression.contains(&format!("{{{}}}", parameter)))
.filter(move |(parameter, _)| !self.expression.contains(&format!("{{{parameter}}}")))
}
fn replace_parameters_in(&self, original: &str) -> String {
@ -108,7 +108,7 @@ impl<'a> Command<'a> {
let mut replacements = BTreeMap::<String, String>::new();
for (param_name, param_value) in &self.parameters {
replacements.insert(
format!("{{{param_name}}}", param_name = param_name),
format!("{{{param_name}}}"),
param_value.to_string(),
);
}

View File

@ -32,7 +32,7 @@ impl MarkupExporter for AsciidocExporter {
}
fn command(&self, cmd: &str) -> String {
format!("`{}`", cmd)
format!("`{cmd}`")
}
}
@ -71,8 +71,7 @@ fn test_asciidoc_exporter_table_header() {
#[cfg(test)]
fn cfg_test_table_header(unit_short_name: &str) -> String {
format!(
"[cols=\"<,>,>,>,>\"]\n|===\n| Command \n| Mean [{unit}] \n| Min [{unit}] \n| Max [{unit}] \n| Relative \n",
unit = unit_short_name
"[cols=\"<,>,>,>,>\"]\n|===\n| Command \n| Mean [{unit_short_name}] \n| Min [{unit_short_name}] \n| Max [{unit_short_name}] \n| Relative \n"
)
}

View File

@ -31,7 +31,7 @@ impl Exporter for CsvExporter {
.collect();
if let Some(res) = results.first() {
for param_name in res.parameters.keys() {
headers.push(Cow::Owned(format!("parameter_{}", param_name).into_bytes()));
headers.push(Cow::Owned(format!("parameter_{param_name}").into_bytes()));
}
}
writer.write_record(headers)?;

View File

@ -24,7 +24,7 @@ impl MarkupExporter for MarkdownExporter {
}
fn command(&self, cmd: &str) -> String {
format!("`{}`", cmd)
format!("`{cmd}`")
}
}
@ -53,8 +53,7 @@ fn test_markdown_formatter_table_divider() {
#[cfg(test)]
fn cfg_test_table_header(unit_short_name: String) -> String {
format!(
"| Command | Mean [{unit}] | Min [{unit}] | Max [{unit}] | Relative |\n|:---|---:|---:|---:|---:|\n",
unit = unit_short_name
"| Command | Mean [{unit_short_name}] | Min [{unit_short_name}] | Max [{unit_short_name}] | Relative |\n|:---|---:|---:|---:|---:|\n"
)
}

View File

@ -32,9 +32,9 @@ pub trait MarkupExporter {
// emit table header data
table.push_str(&self.table_row(&[
"Command",
&format!("Mean {}", notation),
&format!("Min {}", notation),
&format!("Max {}", notation),
&format!("Mean {notation}"),
&format!("Min {notation}"),
&format!("Max {notation}"),
"Relative",
]));
@ -59,7 +59,7 @@ pub trait MarkupExporter {
let rel_stddev_str = if entry.is_fastest {
"".into()
} else if let Some(stddev) = entry.relative_speed_stddev {
format!(" ± {:.2}", stddev)
format!(" ± {stddev:.2}")
} else {
"".into()
};
@ -67,10 +67,10 @@ pub trait MarkupExporter {
// prepare table row entries
table.push_str(&self.table_row(&[
&self.command(&cmd_str),
&format!("{}{}", mean_str, stddev_str),
&format!("{mean_str}{stddev_str}"),
&min_str,
&max_str,
&format!("{}{}", rel_str, rel_stddev_str),
&format!("{rel_str}{rel_stddev_str}"),
]))
}

View File

@ -108,7 +108,7 @@ impl ExportManager {
ExportTarget::Stdout
} else {
let _ = File::create(filename)
.with_context(|| format!("Could not create export file '{}'", filename))?;
.with_context(|| format!("Could not create export file '{filename}'"))?;
ExportTarget::File(filename.to_string())
},
});
@ -153,5 +153,5 @@ impl ExportManager {
fn write_to_file(filename: &str, content: &[u8]) -> Result<()> {
let mut file = OpenOptions::new().write(true).open(filename)?;
file.write_all(content)
.with_context(|| format!("Failed to export results to '{}'", filename))
.with_context(|| format!("Failed to export results to '{filename}'"))
}

View File

@ -18,7 +18,7 @@ impl MarkupExporter for OrgmodeExporter {
}
fn command(&self, cmd: &str) -> String {
format!("={}=", cmd)
format!("={cmd}=")
}
}
@ -58,8 +58,7 @@ fn test_orgmode_formatter_table_line() {
#[cfg(test)]
fn cfg_test_table_header(unit_short_name: String) -> String {
format!(
"| Command | Mean [{unit}] | Min [{unit}] | Max [{unit}] | Relative |\n|--+--+--+--+--|\n",
unit = unit_short_name
"| Command | Mean [{unit_short_name}] | Min [{unit_short_name}] | Max [{unit_short_name}] | Relative |\n|--+--+--+--+--|\n"
)
}

View File

@ -38,7 +38,7 @@ impl Default for Shell {
impl fmt::Display for Shell {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Shell::Default(cmd) => write!(f, "{}", cmd),
Shell::Default(cmd) => write!(f, "{cmd}"),
Shell::Custom(cmdline) => write!(f, "{}", shell_words::join(cmdline)),
}
}
@ -457,7 +457,7 @@ impl Options {
fn test_default_shell() {
let shell = Shell::default();
let s = format!("{}", shell);
let s = format!("{shell}");
assert_eq!(&s, DEFAULT_SHELL);
let cmd = shell.command();
@ -468,7 +468,7 @@ fn test_default_shell() {
fn test_can_parse_shell_command_line_from_str() {
let shell = Shell::parse_from_str("shell -x 'aaa bbb'").unwrap();
let s = format!("{}", shell);
let s = format!("{shell}");
assert_eq!(&s, "shell -x 'aaa bbb'");
let cmd = shell.command();

View File

@ -16,7 +16,7 @@ impl Display for ParameterValue {
ParameterValue::Text(ref value) => value.clone(),
ParameterValue::Numeric(value) => value.to_string(),
};
write!(f, "{}", str)
write!(f, "{str}")
}
}

View File

@ -26,7 +26,7 @@ impl Unit {
/// Returns the Second value formatted for the Unit.
pub fn format(self, value: Second) -> String {
match self {
Unit::Second => format!("{:.3}", value),
Unit::Second => format!("{value:.3}"),
Unit::MilliSecond => format!("{:.1}", value * 1e3),
Unit::MicroSecond => format!("{:.1}", value * 1e6),
}