Skip to content

Commit f480534

Browse files
authored
Merge pull request #2442 from hamirmahal/style/simplify-string-formatting-for-readability
style: simplify string formatting for readability
2 parents 6f281a6 + f9add3e commit f480534

File tree

13 files changed

+59
-79
lines changed

13 files changed

+59
-79
lines changed

examples/nop-preprocessor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn main() {
2626
if let Some(sub_args) = matches.subcommand_matches("supports") {
2727
handle_supports(&preprocessor, sub_args);
2828
} else if let Err(e) = handle_preprocessing(&preprocessor) {
29-
eprintln!("{}", e);
29+
eprintln!("{e}");
3030
process::exit(1);
3131
}
3232
}

src/book/book.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ pub fn load_book<P: AsRef<Path>>(src_dir: P, cfg: &BuildConfig) -> Result<Book>
1818

1919
let mut summary_content = String::new();
2020
File::open(&summary_md)
21-
.with_context(|| format!("Couldn't open SUMMARY.md in {:?} directory", src_dir))?
21+
.with_context(|| format!("Couldn't open SUMMARY.md in {src_dir:?} directory"))?
2222
.read_to_string(&mut summary_content)?;
2323

2424
let summary = parse_summary(&summary_content)
25-
.with_context(|| format!("Summary parsing failed for file={:?}", summary_md))?;
25+
.with_context(|| format!("Summary parsing failed for file={summary_md:?}"))?;
2626

2727
if cfg.create_missing {
2828
create_missing(src_dir, &summary).with_context(|| "Unable to create missing chapters")?;
@@ -341,7 +341,7 @@ impl<'a> Iterator for BookItems<'a> {
341341
impl Display for Chapter {
342342
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
343343
if let Some(ref section_number) = self.number {
344-
write!(f, "{} ", section_number)?;
344+
write!(f, "{section_number} ")?;
345345
}
346346

347347
write!(f, "{}", self.name)

src/book/mod.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl MDBook {
9292
}
9393

9494
if log_enabled!(log::Level::Trace) {
95-
for line in format!("Config: {:#?}", config).lines() {
95+
for line in format!("Config: {config:#?}").lines() {
9696
trace!("{}", line);
9797
}
9898
}
@@ -483,15 +483,13 @@ fn determine_preprocessors(config: &Config) -> Result<Vec<Box<dyn Preprocessor>>
483483
if let Some(before) = table.get("before") {
484484
let before = before.as_array().ok_or_else(|| {
485485
Error::msg(format!(
486-
"Expected preprocessor.{}.before to be an array",
487-
name
486+
"Expected preprocessor.{name}.before to be an array"
488487
))
489488
})?;
490489
for after in before {
491490
let after = after.as_str().ok_or_else(|| {
492491
Error::msg(format!(
493-
"Expected preprocessor.{}.before to contain strings",
494-
name
492+
"Expected preprocessor.{name}.before to contain strings"
495493
))
496494
})?;
497495

@@ -510,16 +508,12 @@ fn determine_preprocessors(config: &Config) -> Result<Vec<Box<dyn Preprocessor>>
510508

511509
if let Some(after) = table.get("after") {
512510
let after = after.as_array().ok_or_else(|| {
513-
Error::msg(format!(
514-
"Expected preprocessor.{}.after to be an array",
515-
name
516-
))
511+
Error::msg(format!("Expected preprocessor.{name}.after to be an array"))
517512
})?;
518513
for before in after {
519514
let before = before.as_str().ok_or_else(|| {
520515
Error::msg(format!(
521-
"Expected preprocessor.{}.after to contain strings",
522-
name
516+
"Expected preprocessor.{name}.after to contain strings"
523517
))
524518
})?;
525519

@@ -581,7 +575,7 @@ fn get_custom_preprocessor_cmd(key: &str, table: &Value) -> String {
581575
.get("command")
582576
.and_then(Value::as_str)
583577
.map(ToString::to_string)
584-
.unwrap_or_else(|| format!("mdbook-{}", key))
578+
.unwrap_or_else(|| format!("mdbook-{key}"))
585579
}
586580

587581
fn interpret_custom_renderer(key: &str, table: &Value) -> Box<CmdRenderer> {
@@ -592,7 +586,7 @@ fn interpret_custom_renderer(key: &str, table: &Value) -> Box<CmdRenderer> {
592586
.and_then(Value::as_str)
593587
.map(ToString::to_string);
594588

595-
let command = table_dot_command.unwrap_or_else(|| format!("mdbook-{}", key));
589+
let command = table_dot_command.unwrap_or_else(|| format!("mdbook-{key}"));
596590

597591
Box::new(CmdRenderer::new(key.to_string(), command))
598592
}
@@ -786,7 +780,7 @@ mod tests {
786780
for preprocessor in &preprocessors {
787781
eprintln!(" {}", preprocessor.name());
788782
}
789-
panic!("{} should come before {}", before, after);
783+
panic!("{before} should come before {after}");
790784
}
791785
};
792786

src/book/summary.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ impl Display for SectionNumber {
616616
write!(f, "0")
617617
} else {
618618
for item in &self.0 {
619-
write!(f, "{}.", item)?;
619+
write!(f, "{item}.")?;
620620
}
621621
Ok(())
622622
}
@@ -763,7 +763,7 @@ mod tests {
763763

764764
let href = match parser.stream.next() {
765765
Some((Event::Start(Tag::Link { dest_url, .. }), _range)) => dest_url.to_string(),
766-
other => panic!("Unreachable, {:?}", other),
766+
other => panic!("Unreachable, {other:?}"),
767767
};
768768

769769
let got = parser.parse_link(href);

src/cmd/serve.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
5454
let hostname = args.get_one::<String>("hostname").unwrap();
5555
let open_browser = args.get_flag("open");
5656

57-
let address = format!("{}:{}", hostname, port);
57+
let address = format!("{hostname}:{port}");
5858

5959
let update_config = |book: &mut MDBook| {
6060
book.config
@@ -89,7 +89,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
8989
serve(build_dir, sockaddr, reload_tx, &file_404);
9090
});
9191

92-
let serving_url = format!("http://{}", address);
92+
let serving_url = format!("http://{address}");
9393
info!("Serving on: {}", serving_url);
9494

9595
if open_browser {

src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl Config {
145145
if let serde_json::Value::Object(ref map) = parsed_value {
146146
// To `set` each `key`, we wrap them as `prefix.key`
147147
for (k, v) in map {
148-
let full_key = format!("{}.{}", key, k);
148+
let full_key = format!("{key}.{k}");
149149
self.set(&full_key, v).expect("unreachable");
150150
}
151151
return;

src/preprocess/links.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ mod tests {
493493
let s = "Some random text with {{#playground file.rs}} and {{#playground test.rs }}...";
494494

495495
let res = find_links(s).collect::<Vec<_>>();
496-
println!("\nOUTPUT: {:?}\n", res);
496+
println!("\nOUTPUT: {res:?}\n");
497497

498498
assert_eq!(
499499
res,
@@ -519,7 +519,7 @@ mod tests {
519519
let s = "Some random text with {{#playground foo-bar\\baz/_c++.rs}}...";
520520

521521
let res = find_links(s).collect::<Vec<_>>();
522-
println!("\nOUTPUT: {:?}\n", res);
522+
println!("\nOUTPUT: {res:?}\n");
523523

524524
assert_eq!(
525525
res,
@@ -536,7 +536,7 @@ mod tests {
536536
fn test_find_links_with_range() {
537537
let s = "Some random text with {{#include file.rs:10:20}}...";
538538
let res = find_links(s).collect::<Vec<_>>();
539-
println!("\nOUTPUT: {:?}\n", res);
539+
println!("\nOUTPUT: {res:?}\n");
540540
assert_eq!(
541541
res,
542542
vec![Link {
@@ -555,7 +555,7 @@ mod tests {
555555
fn test_find_links_with_line_number() {
556556
let s = "Some random text with {{#include file.rs:10}}...";
557557
let res = find_links(s).collect::<Vec<_>>();
558-
println!("\nOUTPUT: {:?}\n", res);
558+
println!("\nOUTPUT: {res:?}\n");
559559
assert_eq!(
560560
res,
561561
vec![Link {
@@ -574,7 +574,7 @@ mod tests {
574574
fn test_find_links_with_from_range() {
575575
let s = "Some random text with {{#include file.rs:10:}}...";
576576
let res = find_links(s).collect::<Vec<_>>();
577-
println!("\nOUTPUT: {:?}\n", res);
577+
println!("\nOUTPUT: {res:?}\n");
578578
assert_eq!(
579579
res,
580580
vec![Link {
@@ -593,7 +593,7 @@ mod tests {
593593
fn test_find_links_with_to_range() {
594594
let s = "Some random text with {{#include file.rs::20}}...";
595595
let res = find_links(s).collect::<Vec<_>>();
596-
println!("\nOUTPUT: {:?}\n", res);
596+
println!("\nOUTPUT: {res:?}\n");
597597
assert_eq!(
598598
res,
599599
vec![Link {
@@ -612,7 +612,7 @@ mod tests {
612612
fn test_find_links_with_full_range() {
613613
let s = "Some random text with {{#include file.rs::}}...";
614614
let res = find_links(s).collect::<Vec<_>>();
615-
println!("\nOUTPUT: {:?}\n", res);
615+
println!("\nOUTPUT: {res:?}\n");
616616
assert_eq!(
617617
res,
618618
vec![Link {
@@ -631,7 +631,7 @@ mod tests {
631631
fn test_find_links_with_no_range_specified() {
632632
let s = "Some random text with {{#include file.rs}}...";
633633
let res = find_links(s).collect::<Vec<_>>();
634-
println!("\nOUTPUT: {:?}\n", res);
634+
println!("\nOUTPUT: {res:?}\n");
635635
assert_eq!(
636636
res,
637637
vec![Link {
@@ -650,7 +650,7 @@ mod tests {
650650
fn test_find_links_with_anchor() {
651651
let s = "Some random text with {{#include file.rs:anchor}}...";
652652
let res = find_links(s).collect::<Vec<_>>();
653-
println!("\nOUTPUT: {:?}\n", res);
653+
println!("\nOUTPUT: {res:?}\n");
654654
assert_eq!(
655655
res,
656656
vec![Link {
@@ -670,7 +670,7 @@ mod tests {
670670
let s = "Some random text with escaped playground \\{{#playground file.rs editable}} ...";
671671

672672
let res = find_links(s).collect::<Vec<_>>();
673-
println!("\nOUTPUT: {:?}\n", res);
673+
println!("\nOUTPUT: {res:?}\n");
674674

675675
assert_eq!(
676676
res,
@@ -690,7 +690,7 @@ mod tests {
690690
more\n text {{#playground my.rs editable no_run should_panic}} ...";
691691

692692
let res = find_links(s).collect::<Vec<_>>();
693-
println!("\nOUTPUT: {:?}\n", res);
693+
println!("\nOUTPUT: {res:?}\n");
694694
assert_eq!(
695695
res,
696696
vec![
@@ -721,7 +721,7 @@ mod tests {
721721
no_run should_panic}} ...";
722722

723723
let res = find_links(s).collect::<Vec<_>>();
724-
println!("\nOUTPUT: {:?}\n", res);
724+
println!("\nOUTPUT: {res:?}\n");
725725
assert_eq!(res.len(), 3);
726726
assert_eq!(
727727
res[0],

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,13 @@ impl HtmlHandlebars {
153153
let content_404 = if let Some(ref filename) = html_config.input_404 {
154154
let path = src_dir.join(filename);
155155
std::fs::read_to_string(&path)
156-
.with_context(|| format!("unable to open 404 input file {:?}", path))?
156+
.with_context(|| format!("unable to open 404 input file {path:?}"))?
157157
} else {
158158
// 404 input not explicitly configured try the default file 404.md
159159
let default_404_location = src_dir.join("404.md");
160160
if default_404_location.exists() {
161161
std::fs::read_to_string(&default_404_location).with_context(|| {
162-
format!("unable to open 404 input file {:?}", default_404_location)
162+
format!("unable to open 404 input file {default_404_location:?}")
163163
})?
164164
} else {
165165
"# Document not found (404)\n\nThis URL is invalid, sorry. Please use the \
@@ -237,7 +237,7 @@ impl HtmlHandlebars {
237237
)?;
238238

239239
if let Some(cname) = &html_config.cname {
240-
write_file(destination, "CNAME", format!("{}\n", cname).as_bytes())?;
240+
write_file(destination, "CNAME", format!("{cname}\n").as_bytes())?;
241241
}
242242

243243
write_file(destination, "book.js", &theme.js)?;
@@ -834,11 +834,7 @@ fn insert_link_into_header(
834834
.unwrap_or_default();
835835

836836
format!(
837-
r##"<h{level} id="{id}"{classes}><a class="header" href="#{id}">{text}</a></h{level}>"##,
838-
level = level,
839-
id = id,
840-
text = content,
841-
classes = classes
837+
r##"<h{level} id="{id}"{classes}><a class="header" href="#{id}">{content}</a></h{level}>"##
842838
)
843839
}
844840

@@ -860,12 +856,7 @@ fn fix_code_blocks(html: &str) -> String {
860856
let classes = &caps[2].replace(',', " ");
861857
let after = &caps[3];
862858

863-
format!(
864-
r#"<code{before}class="{classes}"{after}>"#,
865-
before = before,
866-
classes = classes,
867-
after = after
868-
)
859+
format!(r#"<code{before}class="{classes}"{after}>"#)
869860
})
870861
.into_owned()
871862
}
@@ -923,8 +914,7 @@ fn add_playground_pre(
923914
// we need to inject our own main
924915
let (attrs, code) = partition_source(code);
925916

926-
format!("# #![allow(unused)]\n{}#fn main() {{\n{}#}}", attrs, code)
927-
.into()
917+
format!("# #![allow(unused)]\n{attrs}#fn main() {{\n{code}#}}").into()
928918
};
929919
content
930920
}

src/renderer/html_handlebars/search.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub fn create_files(search_config: &Search, destination: &Path, book: &Book) ->
5050
utils::fs::write_file(
5151
destination,
5252
"searchindex.js",
53-
format!("Object.assign(window.search, {});", index).as_bytes(),
53+
format!("Object.assign(window.search, {index});").as_bytes(),
5454
)?;
5555
utils::fs::write_file(destination, "searcher.js", searcher::JS)?;
5656
utils::fs::write_file(destination, "mark.min.js", searcher::MARK_JS)?;
@@ -83,7 +83,7 @@ fn add_doc(
8383
});
8484

8585
let url = if let Some(id) = section_id {
86-
Cow::Owned(format!("{}#{}", anchor_base, id))
86+
Cow::Owned(format!("{anchor_base}#{id}"))
8787
} else {
8888
Cow::Borrowed(anchor_base)
8989
};
@@ -203,7 +203,7 @@ fn render_item(
203203
Event::FootnoteReference(name) => {
204204
let len = footnote_numbers.len() + 1;
205205
let number = footnote_numbers.entry(name).or_insert(len);
206-
body.push_str(&format!(" [{}] ", number));
206+
body.push_str(&format!(" [{number}] "));
207207
}
208208
Event::TaskListMarker(_checked) => {}
209209
}

0 commit comments

Comments
 (0)