style: simplify string interpolation

This commit is contained in:
Hamir Mahal authored and David Peter committed 2024-06-01 11:07:23 +02:00
1 parent e8ff88dad1
commit ef1263279d
13 files changed
+30 -33

No files matched your search

+2 -2
View File
@@ -54,7 +54,7 @@ fn run_command_and_measure_common(
); );
let result = execute_and_measure(command) 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() { if command_failure_action == CmdFailureAction::RaiseError && !result.status.success() {
bail!( bail!(
@@ -62,7 +62,7 @@ fn run_command_and_measure_common(
Alternatively, use the '--show-output' option to debug what went wrong.", Alternatively, use the '--show-output' option to debug what went wrong.",
result.status.code().map_or( result.status.code().map_or(
"The process has been terminated by a signal".into(), "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}")
) )
); );
} }
+1 -1
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 (mean_str, time_unit) = format_duration_unit(t_mean, self.options.time_unit);
let min_str = format_duration(t_min, Some(time_unit)); let min_str = format_duration(t_min, Some(time_unit));
let max_str = format_duration(t_max, 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 user_str = format_duration(user_mean, Some(time_unit));
let system_str = format_duration(system_mean, Some(time_unit)); let system_str = format_duration(system_mean, Some(time_unit));
+2 -2
View File
@@ -86,7 +86,7 @@ impl<'a> Scheduler<'a> {
"{}{} times faster than {}", "{}{} times faster than {}",
format!("{:8.2}", item.relative_speed).bold().green(), format!("{:8.2}", item.relative_speed).bold().green(),
if let Some(stddev) = item.relative_speed_stddev { if let Some(stddev) = item.relative_speed_stddev {
format!(" ± {}", format!("{:.2}", stddev).green()) format!(" ± {}", format!("{stddev:.2}").green())
} else { } else {
"".into() "".into()
}, },
@@ -104,7 +104,7 @@ impl<'a> Scheduler<'a> {
if item.is_fastest { if item.is_fastest {
" ".into() " ".into()
} else if let Some(stddev) = item.relative_speed_stddev { } else if let Some(stddev) = item.relative_speed_stddev {
format!(" ± {}", format!("{:5.2}", stddev).green()) format!(" ± {}", format!("{stddev:5.2}").green())
} else { } else {
" ".into() " ".into()
}, },
+5 -5
View File
@@ -62,13 +62,13 @@ impl<'a> Command<'a> {
let parameters = self let parameters = self
.get_unused_parameters() .get_unused_parameters()
.fold(String::new(), |output, (parameter, value)| { .fold(String::new(), |output, (parameter, value)| {
output + &format!("{} = {}, ", parameter, value) output + &format!("{parameter} = {value}, ")
}); });
let parameters = parameters.trim_end_matches(", "); let parameters = parameters.trim_end_matches(", ");
let parameters = if parameters.is_empty() { let parameters = if parameters.is_empty() {
"".into() "".into()
} else { } else {
format!(" ({})", parameters) format!(" ({parameters})")
}; };
format!("{}{}", self.get_name(), parameters) format!("{}{}", self.get_name(), parameters)
@@ -81,7 +81,7 @@ impl<'a> Command<'a> {
pub fn get_command(&self) -> Result<std::process::Command> { pub fn get_command(&self) -> Result<std::process::Command> {
let command_line = self.get_command_line(); let command_line = self.get_command_line();
let mut tokens = shell_words::split(&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(); .into_iter();
if let Some(program_name) = tokens.next() { 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)> { pub fn get_unused_parameters(&self) -> impl Iterator<Item = &(&'a str, ParameterValue)> {
self.parameters self.parameters
.iter() .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 { fn replace_parameters_in(&self, original: &str) -> String {
@@ -108,7 +108,7 @@ impl<'a> Command<'a> {
let mut replacements = BTreeMap::<String, String>::new(); let mut replacements = BTreeMap::<String, String>::new();
for (param_name, param_value) in &self.parameters { for (param_name, param_value) in &self.parameters {
replacements.insert( replacements.insert(
format!("{{{param_name}}}", param_name = param_name), format!("{{{param_name}}}"),
param_value.to_string(), param_value.to_string(),
); );
} }
+2 -3
View File
@@ -32,7 +32,7 @@ impl MarkupExporter for AsciidocExporter {
} }
fn command(&self, cmd: &str) -> String { fn command(&self, cmd: &str) -> String {
format!("`{}`", cmd) format!("`{cmd}`")
} }
} }
@@ -71,8 +71,7 @@ fn test_asciidoc_exporter_table_header() {
#[cfg(test)] #[cfg(test)]
fn cfg_test_table_header(unit_short_name: &str) -> String { fn cfg_test_table_header(unit_short_name: &str) -> String {
format!( format!(
"[cols=\"<,>,>,>,>\"]\n|===\n| Command \n| Mean [{unit}] \n| Min [{unit}] \n| Max [{unit}] \n| Relative \n", "[cols=\"<,>,>,>,>\"]\n|===\n| Command \n| Mean [{unit_short_name}] \n| Min [{unit_short_name}] \n| Max [{unit_short_name}] \n| Relative \n"
unit = unit_short_name
) )
} }
+1 -1
View File
@@ -31,7 +31,7 @@ impl Exporter for CsvExporter {
.collect(); .collect();
if let Some(res) = results.first() { if let Some(res) = results.first() {
for param_name in res.parameters.keys() { 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)?; writer.write_record(headers)?;
+2 -3
View File
@@ -24,7 +24,7 @@ impl MarkupExporter for MarkdownExporter {
} }
fn command(&self, cmd: &str) -> String { fn command(&self, cmd: &str) -> String {
format!("`{}`", cmd) format!("`{cmd}`")
} }
} }
@@ -53,8 +53,7 @@ fn test_markdown_formatter_table_divider() {
#[cfg(test)] #[cfg(test)]
fn cfg_test_table_header(unit_short_name: String) -> String { fn cfg_test_table_header(unit_short_name: String) -> String {
format!( format!(
"| Command | Mean [{unit}] | Min [{unit}] | Max [{unit}] | Relative |\n|:---|---:|---:|---:|---:|\n", "| Command | Mean [{unit_short_name}] | Min [{unit_short_name}] | Max [{unit_short_name}] | Relative |\n|:---|---:|---:|---:|---:|\n"
unit = unit_short_name
) )
} }
+6 -6
View File
@@ -32,9 +32,9 @@ pub trait MarkupExporter {
// emit table header data // emit table header data
table.push_str(&self.table_row(&[ table.push_str(&self.table_row(&[
"Command", "Command",
&format!("Mean {}", notation), &format!("Mean {notation}"),
&format!("Min {}", notation), &format!("Min {notation}"),
&format!("Max {}", notation), &format!("Max {notation}"),
"Relative", "Relative",
])); ]));
@@ -59,7 +59,7 @@ pub trait MarkupExporter {
let rel_stddev_str = if entry.is_fastest { let rel_stddev_str = if entry.is_fastest {
"".into() "".into()
} else if let Some(stddev) = entry.relative_speed_stddev { } else if let Some(stddev) = entry.relative_speed_stddev {
format!(" ± {:.2}", stddev) format!(" ± {stddev:.2}")
} else { } else {
"".into() "".into()
}; };
@@ -67,10 +67,10 @@ pub trait MarkupExporter {
// prepare table row entries // prepare table row entries
table.push_str(&self.table_row(&[ table.push_str(&self.table_row(&[
&self.command(&cmd_str), &self.command(&cmd_str),
&format!("{}{}", mean_str, stddev_str), &format!("{mean_str}{stddev_str}"),
&min_str, &min_str,
&max_str, &max_str,
&format!("{}{}", rel_str, rel_stddev_str), &format!("{rel_str}{rel_stddev_str}"),
])) ]))
} }
+2 -2
View File
@@ -108,7 +108,7 @@ impl ExportManager {
ExportTarget::Stdout ExportTarget::Stdout
} else { } else {
let _ = File::create(filename) 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()) ExportTarget::File(filename.to_string())
}, },
}); });
@@ -153,5 +153,5 @@ impl ExportManager {
fn write_to_file(filename: &str, content: &[u8]) -> Result<()> { fn write_to_file(filename: &str, content: &[u8]) -> Result<()> {
let mut file = OpenOptions::new().write(true).open(filename)?; let mut file = OpenOptions::new().write(true).open(filename)?;
file.write_all(content) file.write_all(content)
.with_context(|| format!("Failed to export results to '{}'", filename)) .with_context(|| format!("Failed to export results to '{filename}'"))
} }
+2 -3
View File
@@ -18,7 +18,7 @@ impl MarkupExporter for OrgmodeExporter {
} }
fn command(&self, cmd: &str) -> String { fn command(&self, cmd: &str) -> String {
format!("={}=", cmd) format!("={cmd}=")
} }
} }
@@ -58,8 +58,7 @@ fn test_orgmode_formatter_table_line() {
#[cfg(test)] #[cfg(test)]
fn cfg_test_table_header(unit_short_name: String) -> String { fn cfg_test_table_header(unit_short_name: String) -> String {
format!( format!(
"| Command | Mean [{unit}] | Min [{unit}] | Max [{unit}] | Relative |\n|--+--+--+--+--|\n", "| Command | Mean [{unit_short_name}] | Min [{unit_short_name}] | Max [{unit_short_name}] | Relative |\n|--+--+--+--+--|\n"
unit = unit_short_name
) )
} }
+3 -3
View File
@@ -38,7 +38,7 @@ impl Default for Shell {
impl fmt::Display for Shell { impl fmt::Display for Shell {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
Shell::Default(cmd) => write!(f, "{}", cmd), Shell::Default(cmd) => write!(f, "{cmd}"),
Shell::Custom(cmdline) => write!(f, "{}", shell_words::join(cmdline)), Shell::Custom(cmdline) => write!(f, "{}", shell_words::join(cmdline)),
} }
} }
@@ -457,7 +457,7 @@ impl Options {
fn test_default_shell() { fn test_default_shell() {
let shell = Shell::default(); let shell = Shell::default();
let s = format!("{}", shell); let s = format!("{shell}");
assert_eq!(&s, DEFAULT_SHELL); assert_eq!(&s, DEFAULT_SHELL);
let cmd = shell.command(); let cmd = shell.command();
@@ -468,7 +468,7 @@ fn test_default_shell() {
fn test_can_parse_shell_command_line_from_str() { fn test_can_parse_shell_command_line_from_str() {
let shell = Shell::parse_from_str("shell -x 'aaa bbb'").unwrap(); 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'"); assert_eq!(&s, "shell -x 'aaa bbb'");
let cmd = shell.command(); let cmd = shell.command();
+1 -1
View File
@@ -16,7 +16,7 @@ impl Display for ParameterValue {
ParameterValue::Text(ref value) => value.clone(), ParameterValue::Text(ref value) => value.clone(),
ParameterValue::Numeric(value) => value.to_string(), ParameterValue::Numeric(value) => value.to_string(),
}; };
write!(f, "{}", str) write!(f, "{str}")
} }
} }
+1 -1
View File
@@ -26,7 +26,7 @@ impl Unit {
/// Returns the Second value formatted for the Unit. /// Returns the Second value formatted for the Unit.
pub fn format(self, value: Second) -> String { pub fn format(self, value: Second) -> String {
match self { match self {
Unit::Second => format!("{:.3}", value), Unit::Second => format!("{value:.3}"),
Unit::MilliSecond => format!("{:.1}", value * 1e3), Unit::MilliSecond => format!("{:.1}", value * 1e3),
Unit::MicroSecond => format!("{:.1}", value * 1e6), Unit::MicroSecond => format!("{:.1}", value * 1e6),
} }