Skip to content
Myr

style: simplify string interpolation

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Hamir Mahal · committed by David Peter · parent e8ff88d · Browse files · 13 files+30 added−33 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

13 files+30 added−33 removed

  • other:+30 added−33 removed

13 files+30 added−33 removed

modified:src/benchmark/executor.rs+2 added-2 removed
52 unmodified lines
53 randomized_environment_offset::value(),54 );5556 let result = execute_and_measure(command)57 .with_context(|| format!("Failed to run command '{}'", command_name))?;57 .with_context(|| format!("Failed to run command '{command_name}'"))?;5859 if command_failure_action == CmdFailureAction::RaiseError && !result.status.success() {60 bail!(61 "{}. Use the '-i'/'--ignore-failure' option if you want to ignore this. \62 Alternatively, use the '--show-output' option to debug what went wrong.",63 result.status.code().map_or(64 "The process has been terminated by a signal".into(),65 |c| format!("Command terminated with non-zero exit code: {}", c)65 |c| format!("Command terminated with non-zero exit code: {c}")66 )67 );68 }69
233 unmodified lines
modified:src/benchmark/mod.rs+1 added-1 removed
310 unmodified lines
311 // Formatting and console output312 let (mean_str, time_unit) = format_duration_unit(t_mean, self.options.time_unit);313 let min_str = format_duration(t_min, Some(time_unit));314 let max_str = format_duration(t_max, Some(time_unit));315 let num_str = format!("{} runs", t_num);315 let num_str = format!("{t_num} runs");316317 let user_str = format_duration(user_mean, Some(time_unit));318 let system_str = format_duration(system_mean, Some(time_unit));319
107 unmodified lines
modified:src/benchmark/scheduler.rs+2 added-2 removed
84 unmodified lines
85 println!(86 "{}{} times faster than {}",87 format!("{:8.2}", item.relative_speed).bold().green(),88 if let Some(stddev) = item.relative_speed_stddev {89 format!(" ± {}", format!("{:.2}", stddev).green())89 format!(" ± {}", format!("{stddev:.2}").green())90 } else {91 "".into()92 },93 &item.result.command_with_unused_parameters.magenta()
9 unmodified lines
103 format!("{:10.2}", item.relative_speed).bold().green(),104 if item.is_fastest {105 " ".into()106 } else if let Some(stddev) = item.relative_speed_stddev {107 format!(" ± {}", format!("{:5.2}", stddev).green())107 format!(" ± {}", format!("{stddev:5.2}").green())108 } else {109 " ".into()110 },111 &item.result.command_with_unused_parameters,
22 unmodified lines
modified:src/export/asciidoc.rs+2 added-3 removed
30 unmodified lines
31 "".to_string()32 }3334 fn command(&self, cmd: &str) -> String {35 format!("`{}`", cmd)35 format!("`{cmd}`")36 }37}3839/// Check Asciidoc-based data row formatting
30 unmodified lines
70/// independently from the markup functionality for Asciidoc.71#[cfg(test)]72fn cfg_test_table_header(unit_short_name: &str) -> String {73 format!(74 "[cols=\"<,>,>,>,>\"]\n|===\n| Command \n| Mean [{unit}] \n| Min [{unit}] \n| Max [{unit}] \n| Relative \n",75 unit = unit_short_name74 "[cols=\"<,>,>,>,>\"]\n|===\n| Command \n| Mean [{unit_short_name}] \n| Min [{unit_short_name}] \n| Max [{unit_short_name}] \n| Relative \n"75 )76}7778#[cfg(test)]
159 unmodified lines
modified:src/export/csv.rs+1 added-1 removed
29 unmodified lines
30 .map(|x| Cow::Borrowed(x.as_bytes()))31 .collect();32 if let Some(res) = results.first() {33 for param_name in res.parameters.keys() {34 headers.push(Cow::Owned(format!("parameter_{}", param_name).into_bytes()));34 headers.push(Cow::Owned(format!("parameter_{param_name}").into_bytes()));35 }36 }37 writer.write_record(headers)?;38 }
84 unmodified lines
modified:src/export/markdown.rs+2 added-3 removed
22 unmodified lines
23 )24 }2526 fn command(&self, cmd: &str) -> String {27 format!("`{}`", cmd)27 format!("`{cmd}`")28 }29}3031#[cfg(test)]
20 unmodified lines
52/// independently from the markup functionality for Markdown.53#[cfg(test)]54fn cfg_test_table_header(unit_short_name: String) -> String {55 format!(56 "| Command | Mean [{unit}] | Min [{unit}] | Max [{unit}] | Relative |\n|:---|---:|---:|---:|---:|\n",57 unit = unit_short_name56 "| Command | Mean [{unit_short_name}] | Min [{unit_short_name}] | Max [{unit_short_name}] | Relative |\n|:---|---:|---:|---:|---:|\n"57 )58}5960/// Ensure the markdown output includes the table header and the multiple
251 unmodified lines
modified:src/export/markup.rs+6 added-6 removed
30 unmodified lines
3132 // emit table header data33 table.push_str(&self.table_row(&[34 "Command",35 &format!("Mean {}", notation),36 &format!("Min {}", notation),37 &format!("Max {}", notation),35 &format!("Mean {notation}"),36 &format!("Min {notation}"),37 &format!("Max {notation}"),38 "Relative",39 ]));4041 // emit horizontal line
16 unmodified lines
58 let rel_str = format!("{:.2}", entry.relative_speed);59 let rel_stddev_str = if entry.is_fastest {60 "".into()61 } else if let Some(stddev) = entry.relative_speed_stddev {62 format!(" ± {:.2}", stddev)62 format!(" ± {stddev:.2}")63 } else {64 "".into()65 };6667 // prepare table row entries68 table.push_str(&self.table_row(&[69 &self.command(&cmd_str),70 &format!("{}{}", mean_str, stddev_str),70 &format!("{mean_str}{stddev_str}"),71 &min_str,72 &max_str,73 &format!("{}{}", rel_str, rel_stddev_str),73 &format!("{rel_str}{rel_stddev_str}"),74 ]))75 }7677 // emit table footer format
224 unmodified lines
modified:src/export/mod.rs+2 added-2 removed
106 unmodified lines
107 target: if filename == "-" {108 ExportTarget::Stdout109 } else {110 let _ = File::create(filename)111 .with_context(|| format!("Could not create export file '{}'", filename))?;111 .with_context(|| format!("Could not create export file '{filename}'"))?;112 ExportTarget::File(filename.to_string())113 },114 });115
36 unmodified lines
152/// Write the given content to a file with the specified name153fn write_to_file(filename: &str, content: &[u8]) -> Result<()> {154 let mut file = OpenOptions::new().write(true).open(filename)?;155 file.write_all(content)156 .with_context(|| format!("Failed to export results to '{}'", filename))156 .with_context(|| format!("Failed to export results to '{filename}'"))157}
modified:src/export/orgmode.rs+2 added-3 removed
16 unmodified lines
17 format!("|{}--|\n", "--+".repeat(cell_aligmnents.len() - 1))18 }1920 fn command(&self, cmd: &str) -> String {21 format!("={}=", cmd)21 format!("={cmd}=")22 }23}2425#[cfg(test)]
31 unmodified lines
57/// independently from the markup functionality for Emacs org-mode.58#[cfg(test)]59fn cfg_test_table_header(unit_short_name: String) -> String {60 format!(61 "| Command | Mean [{unit}] | Min [{unit}] | Max [{unit}] | Relative |\n|--+--+--+--+--|\n",62 unit = unit_short_name61 "| Command | Mean [{unit_short_name}] | Min [{unit_short_name}] | Max [{unit_short_name}] | Relative |\n|--+--+--+--+--|\n"62 )63}6465/// Ensure the Emacs org-mode output includes the table header and the multiple
119 unmodified lines
modified:src/parameter/mod.rs+1 added-1 removed
14 unmodified lines
15 let str = match self {16 ParameterValue::Text(ref value) => value.clone(),17 ParameterValue::Numeric(value) => value.to_string(),18 };19 write!(f, "{}", str)19 write!(f, "{str}")20 }21}2223pub type ParameterNameAndValue<'a> = (&'a str, ParameterValue);
modified:src/util/units.rs+1 added-1 removed
24 unmodified lines
2526 /// Returns the Second value formatted for the Unit.27 pub fn format(self, value: Second) -> String {28 match self {29 Unit::Second => format!("{:.3}", value),29 Unit::Second => format!("{value:.3}"),30 Unit::MilliSecond => format!("{:.1}", value * 1e3),31 Unit::MicroSecond => format!("{:.1}", value * 1e6),32 }33 }
18 unmodified lines
modified:src/command.rs+5 added-5 removed
60 unmodified lines
61 pub fn get_name_with_unused_parameters(&self) -> String {62 let parameters = self63 .get_unused_parameters()64 .fold(String::new(), |output, (parameter, value)| {65 output + &format!("{} = {}, ", parameter, value)65 output + &format!("{parameter} = {value}, ")66 });67 let parameters = parameters.trim_end_matches(", ");68 let parameters = if parameters.is_empty() {69 "".into()70 } else {71 format!(" ({})", parameters)71 format!(" ({parameters})")72 };7374 format!("{}{}", self.get_name(), parameters)75 }
4 unmodified lines
8081 pub fn get_command(&self) -> Result<std::process::Command> {82 let command_line = self.get_command_line();83 let mut tokens = shell_words::split(&command_line)84 .with_context(|| format!("Failed to parse command '{}'", command_line))?84 .with_context(|| format!("Failed to parse command '{command_line}'"))?85 .into_iter();8687 if let Some(program_name) = tokens.next() {88 let mut command_builder = std::process::Command::new(program_name);
10 unmodified lines
99100 pub fn get_unused_parameters(&self) -> impl Iterator<Item = &(&'a str, ParameterValue)> {101 self.parameters102 .iter()103 .filter(move |(parameter, _)| !self.expression.contains(&format!("{{{}}}", parameter)))103 .filter(move |(parameter, _)| !self.expression.contains(&format!("{{{parameter}}}")))104 }105106 fn replace_parameters_in(&self, original: &str) -> String {107 let mut result = String::new();108 let mut replacements = BTreeMap::<String, String>::new();109 for (param_name, param_value) in &self.parameters {110 replacements.insert(111 format!("{{{param_name}}}", param_name = param_name),111 format!("{{{param_name}}}"),112 param_value.to_string(),113 );114 }115 let mut remaining = original;
450 unmodified lines
modified:src/options.rs+3 added-3 removed
36 unmodified lines
3738impl fmt::Display for Shell {39 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {40 match self {41 Shell::Default(cmd) => write!(f, "{}", cmd),41 Shell::Default(cmd) => write!(f, "{cmd}"),42 Shell::Custom(cmdline) => write!(f, "{}", shell_words::join(cmdline)),43 }44 }45}
410 unmodified lines
456#[test]457fn test_default_shell() {458 let shell = Shell::default();459460 let s = format!("{}", shell);460 let s = format!("{shell}");461 assert_eq!(&s, DEFAULT_SHELL);462463 let cmd = shell.command();464 assert_eq!(cmd.get_program(), DEFAULT_SHELL);
2 unmodified lines
467#[test]468fn test_can_parse_shell_command_line_from_str() {469 let shell = Shell::parse_from_str("shell -x 'aaa bbb'").unwrap();470471 let s = format!("{}", shell);471 let s = format!("{shell}");472 assert_eq!(&s, "shell -x 'aaa bbb'");473474 let cmd = shell.command();475 assert_eq!(cmd.get_program().to_string_lossy(), "shell");
23 unmodified lines