From c7b67e363bb9ce3383636ee615e8e761bf185b33 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 21 Jul 2025 10:31:41 -0700 Subject: [PATCH] Rustfmt for 2024 --- .../mdbook-remove-emphasis/src/main.rs | 2 +- src/book/book.rs | 2 +- src/book/mod.rs | 20 +- src/book/summary.rs | 10 +- src/cmd/build.rs | 2 +- src/cmd/command_prelude.rs | 2 +- src/cmd/init.rs | 4 +- src/cmd/serve.rs | 6 +- src/cmd/test.rs | 4 +- src/cmd/watch.rs | 2 +- src/config.rs | 2 +- src/lib.rs | 2 +- src/preprocess/links.rs | 6 +- src/renderer/html_handlebars/hbs_renderer.rs | 188 +++++++++++------- src/renderer/html_handlebars/static_files.rs | 2 +- src/utils/mod.rs | 2 +- tests/testsuite/book_test.rs | 2 +- tests/testsuite/search.rs | 7 +- 18 files changed, 162 insertions(+), 103 deletions(-) diff --git a/examples/remove-emphasis/mdbook-remove-emphasis/src/main.rs b/examples/remove-emphasis/mdbook-remove-emphasis/src/main.rs index e3a5d607..6112a692 100644 --- a/examples/remove-emphasis/mdbook-remove-emphasis/src/main.rs +++ b/examples/remove-emphasis/mdbook-remove-emphasis/src/main.rs @@ -1,10 +1,10 @@ //! This is a demonstration of an mdBook preprocessor which parses markdown //! and removes any instances of emphasis. +use mdbook::BookItem; use mdbook::book::{Book, Chapter}; use mdbook::errors::Error; use mdbook::preprocess::{CmdPreprocessor, Preprocessor, PreprocessorContext}; -use mdbook::BookItem; use pulldown_cmark::{Event, Parser, Tag, TagEnd}; use std::io; diff --git a/src/book/book.rs b/src/book/book.rs index 72efa989..fe5c3f38 100644 --- a/src/book/book.rs +++ b/src/book/book.rs @@ -4,7 +4,7 @@ use std::fs::{self, File}; use std::io::{Read, Write}; use std::path::{Path, PathBuf}; -use super::summary::{parse_summary, Link, SectionNumber, Summary, SummaryItem}; +use super::summary::{Link, SectionNumber, Summary, SummaryItem, parse_summary}; use crate::config::BuildConfig; use crate::errors::*; use crate::utils::bracket_escape; diff --git a/src/book/mod.rs b/src/book/mod.rs index dffac05f..7c3f0b4c 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -9,9 +9,9 @@ mod book; mod init; mod summary; -pub use self::book::{load_book, Book, BookItem, BookItems, Chapter}; +pub use self::book::{Book, BookItem, BookItems, Chapter, load_book}; pub use self::init::BookBuilder; -pub use self::summary::{parse_summary, Link, SectionNumber, Summary, SummaryItem}; +pub use self::summary::{Link, SectionNumber, Summary, SummaryItem, parse_summary}; use log::{debug, error, info, log_enabled, trace, warn}; use std::ffi::OsString; @@ -817,9 +817,11 @@ mod tests { let preprocessors = determine_preprocessors(&cfg).unwrap(); - assert!(!preprocessors - .iter() - .any(|preprocessor| preprocessor.name() == "random")); + assert!( + !preprocessors + .iter() + .any(|preprocessor| preprocessor.name() == "random") + ); } #[test] @@ -836,9 +838,11 @@ mod tests { let preprocessors = determine_preprocessors(&cfg).unwrap(); - assert!(!preprocessors - .iter() - .any(|preprocessor| preprocessor.name() == "links")); + assert!( + !preprocessors + .iter() + .any(|preprocessor| preprocessor.name() == "links") + ); } #[test] diff --git a/src/book/summary.rs b/src/book/summary.rs index d25b5667..01d6f5f6 100644 --- a/src/book/summary.rs +++ b/src/book/summary.rs @@ -776,7 +776,10 @@ mod tests { assert!(got.is_err()); let error_message = got.err().unwrap().to_string(); - assert_eq!(error_message, "failed to parse SUMMARY.md line 2, column 1: Suffix chapters cannot be followed by a list"); + assert_eq!( + error_message, + "failed to parse SUMMARY.md line 2, column 1: Suffix chapters cannot be followed by a list" + ); } #[test] @@ -788,7 +791,10 @@ mod tests { assert!(got.is_err()); let error_message = got.err().unwrap().to_string(); - assert_eq!(error_message, "failed to parse SUMMARY.md line 1, column 0: Suffix chapters cannot be followed by a list"); + assert_eq!( + error_message, + "failed to parse SUMMARY.md line 1, column 0: Suffix chapters cannot be followed by a list" + ); } #[test] diff --git a/src/cmd/build.rs b/src/cmd/build.rs index e40e5c0c..53792e4e 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -1,7 +1,7 @@ use super::command_prelude::*; use crate::{get_book_dir, open}; -use mdbook::errors::Result; use mdbook::MDBook; +use mdbook::errors::Result; use std::path::PathBuf; // Create clap subcommand arguments diff --git a/src/cmd/command_prelude.rs b/src/cmd/command_prelude.rs index 37199425..d5df3af9 100644 --- a/src/cmd/command_prelude.rs +++ b/src/cmd/command_prelude.rs @@ -1,6 +1,6 @@ //! Helpers for building the command-line arguments for commands. -pub use clap::{arg, Arg, ArgMatches, Command}; +pub use clap::{Arg, ArgMatches, Command, arg}; use std::path::PathBuf; pub trait CommandExt: Sized { diff --git a/src/cmd/init.rs b/src/cmd/init.rs index f15fb968..b31beee9 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -1,8 +1,8 @@ use crate::get_book_dir; -use clap::{arg, ArgMatches, Command as ClapCommand}; +use clap::{ArgMatches, Command as ClapCommand, arg}; +use mdbook::MDBook; use mdbook::config; use mdbook::errors::Result; -use mdbook::MDBook; use std::io; use std::io::Write; use std::process::Command; diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index beab121f..e3a0abdd 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -2,15 +2,15 @@ use super::command_prelude::*; #[cfg(feature = "watch")] use super::watch; use crate::{get_book_dir, open}; +use axum::Router; use axum::extract::ws::{Message, WebSocket, WebSocketUpgrade}; use axum::routing::get; -use axum::Router; use clap::builder::NonEmptyStringValueParser; -use futures_util::sink::SinkExt; use futures_util::StreamExt; +use futures_util::sink::SinkExt; +use mdbook::MDBook; use mdbook::errors::*; use mdbook::utils::fs::get_404_output_file; -use mdbook::MDBook; use std::net::{SocketAddr, ToSocketAddrs}; use std::path::PathBuf; use tokio::sync::broadcast; diff --git a/src/cmd/test.rs b/src/cmd/test.rs index d41e9ef9..d32410f6 100644 --- a/src/cmd/test.rs +++ b/src/cmd/test.rs @@ -1,9 +1,9 @@ use super::command_prelude::*; use crate::get_book_dir; -use clap::builder::NonEmptyStringValueParser; use clap::ArgAction; -use mdbook::errors::Result; +use clap::builder::NonEmptyStringValueParser; use mdbook::MDBook; +use mdbook::errors::Result; use std::path::PathBuf; // Create clap subcommand arguments diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs index 7adb2bbb..b20cbd11 100644 --- a/src/cmd/watch.rs +++ b/src/cmd/watch.rs @@ -1,7 +1,7 @@ use super::command_prelude::*; use crate::{get_book_dir, open}; -use mdbook::errors::Result; use mdbook::MDBook; +use mdbook::errors::Result; use std::path::{Path, PathBuf}; mod native; diff --git a/src/config.rs b/src/config.rs index 41290436..ef0f5dfd 100644 --- a/src/config.rs +++ b/src/config.rs @@ -55,8 +55,8 @@ use std::fs::File; use std::io::Read; use std::path::{Path, PathBuf}; use std::str::FromStr; -use toml::value::Table; use toml::Value; +use toml::value::Table; use crate::errors::*; use crate::utils::{self, toml_ext::TomlExt}; diff --git a/src/lib.rs b/src/lib.rs index 5f521761..93c41769 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -101,6 +101,6 @@ pub use crate::renderer::Renderer; /// The error types used through out this crate. pub mod errors { - pub(crate) use anyhow::{bail, ensure, Context}; + pub(crate) use anyhow::{Context, bail, ensure}; pub use anyhow::{Error, Result}; } diff --git a/src/preprocess/links.rs b/src/preprocess/links.rs index 951a3436..93eaf813 100644 --- a/src/preprocess/links.rs +++ b/src/preprocess/links.rs @@ -684,8 +684,7 @@ mod tests { #[test] fn test_find_playgrounds_with_properties() { - let s = - "Some random text with escaped playground {{#playground file.rs editable }} and some \ + let s = "Some random text with escaped playground {{#playground file.rs editable }} and some \ more\n text {{#playground my.rs editable no_run should_panic}} ..."; let res = find_links(s).collect::>(); @@ -714,8 +713,7 @@ mod tests { #[test] fn test_find_all_link_types() { - let s = - "Some random text with escaped playground {{#include file.rs}} and \\{{#contents are \ + let s = "Some random text with escaped playground {{#include file.rs}} and \\{{#contents are \ insignifficant in escaped link}} some more\n text {{#playground my.rs editable \ no_run should_panic}} ..."; diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index b1ea7520..98203b60 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -1,8 +1,8 @@ use crate::book::{Book, BookItem}; use crate::config::{BookConfig, Code, Config, HtmlConfig, Playground, RustEdition}; use crate::errors::*; -use crate::renderer::html_handlebars::helpers; use crate::renderer::html_handlebars::StaticFiles; +use crate::renderer::html_handlebars::helpers; use crate::renderer::{RenderContext, Renderer}; use crate::theme::{self, Theme}; use crate::utils; @@ -1066,20 +1066,34 @@ mod tests { #[test] fn add_playground() { let inputs = [ - ("x()", - "
# #![allow(unused)]\n# fn main() {\nx()\n# }
"), - ("fn main() {}", - "
fn main() {}
"), - ("let s = \"foo\n # bar\n\";", - "
let s = \"foo\n # bar\n\";
"), - ("let s = \"foo\n ## bar\n\";", - "
let s = \"foo\n ## bar\n\";
"), - ("let s = \"foo\n # bar\n#\n\";", - "
let s = \"foo\n # bar\n#\n\";
"), - ("let s = \"foo\n # bar\n\";", - "let s = \"foo\n # bar\n\";"), - ("#![no_std]\nlet s = \"foo\";\n #[some_attr]", - "
#![no_std]\nlet s = \"foo\";\n #[some_attr]
"), + ( + "x()", + "
# #![allow(unused)]\n# fn main() {\nx()\n# }
", + ), + ( + "fn main() {}", + "
fn main() {}
", + ), + ( + "let s = \"foo\n # bar\n\";", + "
let s = \"foo\n # bar\n\";
", + ), + ( + "let s = \"foo\n ## bar\n\";", + "
let s = \"foo\n ## bar\n\";
", + ), + ( + "let s = \"foo\n # bar\n#\n\";", + "
let s = \"foo\n # bar\n#\n\";
", + ), + ( + "let s = \"foo\n # bar\n\";", + "let s = \"foo\n # bar\n\";", + ), + ( + "#![no_std]\nlet s = \"foo\";\n #[some_attr]", + "
#![no_std]\nlet s = \"foo\";\n #[some_attr]
", + ), ]; for (src, should_be) in &inputs { let got = add_playground_pre( @@ -1096,14 +1110,22 @@ mod tests { #[test] fn add_playground_edition2015() { let inputs = [ - ("x()", - "
# #![allow(unused)]\n# fn main() {\nx()\n# }
"), - ("fn main() {}", - "
fn main() {}
"), - ("fn main() {}", - "
fn main() {}
"), - ("fn main() {}", - "
fn main() {}
"), + ( + "x()", + "
# #![allow(unused)]\n# fn main() {\nx()\n# }
", + ), + ( + "fn main() {}", + "
fn main() {}
", + ), + ( + "fn main() {}", + "
fn main() {}
", + ), + ( + "fn main() {}", + "
fn main() {}
", + ), ]; for (src, should_be) in &inputs { let got = add_playground_pre( @@ -1120,14 +1142,22 @@ mod tests { #[test] fn add_playground_edition2018() { let inputs = [ - ("x()", - "
# #![allow(unused)]\n# fn main() {\nx()\n# }
"), - ("fn main() {}", - "
fn main() {}
"), - ("fn main() {}", - "
fn main() {}
"), - ("fn main() {}", - "
fn main() {}
"), + ( + "x()", + "
# #![allow(unused)]\n# fn main() {\nx()\n# }
", + ), + ( + "fn main() {}", + "
fn main() {}
", + ), + ( + "fn main() {}", + "
fn main() {}
", + ), + ( + "fn main() {}", + "
fn main() {}
", + ), ]; for (src, should_be) in &inputs { let got = add_playground_pre( @@ -1144,14 +1174,22 @@ mod tests { #[test] fn add_playground_edition2021() { let inputs = [ - ("x()", - "
# #![allow(unused)]\n# fn main() {\nx()\n# }
"), - ("fn main() {}", - "
fn main() {}
"), - ("fn main() {}", - "
fn main() {}
"), - ("fn main() {}", - "
fn main() {}
"), + ( + "x()", + "
# #![allow(unused)]\n# fn main() {\nx()\n# }
", + ), + ( + "fn main() {}", + "
fn main() {}
", + ), + ( + "fn main() {}", + "
fn main() {}
", + ), + ( + "fn main() {}", + "
fn main() {}
", + ), ]; for (src, should_be) in &inputs { let got = add_playground_pre( @@ -1169,31 +1207,39 @@ mod tests { #[test] fn hide_lines_language_rust() { let inputs = [ - ( - "
\n# #![allow(unused)]\n# fn main() {\nx()\n# }
", - "
\n#![allow(unused)]\nfn main() {\nx()\n}
",), - // # must be followed by a space for a line to be hidden - ( - "
\n#fn main() {\nx()\n#}
", - "
\n#fn main() {\nx()\n#}
",), - ( - "
fn main() {}
", - "
fn main() {}
",), - ( - "
let s = \"foo\n # bar\n\";
", - "
let s = \"foo\n bar\n\";
",), - ( - "
let s = \"foo\n ## bar\n\";
", - "
let s = \"foo\n # bar\n\";
",), - ( - "
let s = \"foo\n # bar\n#\n\";
", - "
let s = \"foo\n bar\n\n\";
",), - ( - "let s = \"foo\n # bar\n\";", - "let s = \"foo\n bar\n\";",), - ( - "
#![no_std]\nlet s = \"foo\";\n #[some_attr]
", - "
#![no_std]\nlet s = \"foo\";\n #[some_attr]
",), + ( + "
\n# #![allow(unused)]\n# fn main() {\nx()\n# }
", + "
\n#![allow(unused)]\nfn main() {\nx()\n}
", + ), + // # must be followed by a space for a line to be hidden + ( + "
\n#fn main() {\nx()\n#}
", + "
\n#fn main() {\nx()\n#}
", + ), + ( + "
fn main() {}
", + "
fn main() {}
", + ), + ( + "
let s = \"foo\n # bar\n\";
", + "
let s = \"foo\n bar\n\";
", + ), + ( + "
let s = \"foo\n ## bar\n\";
", + "
let s = \"foo\n # bar\n\";
", + ), + ( + "
let s = \"foo\n # bar\n#\n\";
", + "
let s = \"foo\n bar\n\n\";
", + ), + ( + "let s = \"foo\n # bar\n\";", + "let s = \"foo\n bar\n\";", + ), + ( + "
#![no_std]\nlet s = \"foo\";\n #[some_attr]
", + "
#![no_std]\nlet s = \"foo\";\n #[some_attr]
", + ), ]; for (src, should_be) in &inputs { let got = hide_lines(src, &Code::default()); @@ -1204,12 +1250,14 @@ mod tests { #[test] fn hide_lines_language_other() { let inputs = [ - ( - "~hidden()\nnothidden():\n~ hidden()\n ~hidden()\n nothidden()", - "hidden()\nnothidden():\n hidden()\n hidden()\n nothidden()\n",), - ( - "!!!hidden()\nnothidden():\n!!! hidden()\n !!!hidden()\n nothidden()", - "hidden()\nnothidden():\n hidden()\n hidden()\n nothidden()\n",), + ( + "~hidden()\nnothidden():\n~ hidden()\n ~hidden()\n nothidden()", + "hidden()\nnothidden():\n hidden()\n hidden()\n nothidden()\n", + ), + ( + "!!!hidden()\nnothidden():\n!!! hidden()\n !!!hidden()\n nothidden()", + "hidden()\nnothidden():\n hidden()\n hidden()\n nothidden()\n", + ), ]; for (src, should_be) in &inputs { let got = hide_lines( diff --git a/src/renderer/html_handlebars/static_files.rs b/src/renderer/html_handlebars/static_files.rs index ac205415..a3cd1322 100644 --- a/src/renderer/html_handlebars/static_files.rs +++ b/src/renderer/html_handlebars/static_files.rs @@ -5,7 +5,7 @@ use log::{debug, warn}; use crate::config::HtmlConfig; use crate::errors::*; use crate::renderer::html_handlebars::helpers::resources::ResourceHelper; -use crate::theme::{self, playground_editor, Theme}; +use crate::theme::{self, Theme, playground_editor}; use crate::utils; use std::borrow::Cow; diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 597f0ea4..14fdd0ad 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -5,7 +5,7 @@ mod string; pub(crate) mod toml_ext; use crate::errors::Error; use log::error; -use pulldown_cmark::{html, CodeBlockKind, CowStr, Event, Options, Parser, Tag, TagEnd}; +use pulldown_cmark::{CodeBlockKind, CowStr, Event, Options, Parser, Tag, TagEnd, html}; use regex::Regex; use std::borrow::Cow; diff --git a/tests/testsuite/book_test.rs b/tests/testsuite/book_test.rs index 427c38d1..ed5ebe83 100644 --- a/tests/testsuite/book_test.rs +++ b/tests/testsuite/book_test.rs @@ -1,7 +1,7 @@ //! Utility for building and running tests against mdbook. -use mdbook::book::BookBuilder; use mdbook::MDBook; +use mdbook::book::BookBuilder; use snapbox::IntoData; use std::collections::BTreeMap; use std::path::{Path, PathBuf}; diff --git a/tests/testsuite/search.rs b/tests/testsuite/search.rs index 8ab571b2..5eca0e43 100644 --- a/tests/testsuite/search.rs +++ b/tests/testsuite/search.rs @@ -1,8 +1,8 @@ //! Tests for search support. use crate::prelude::*; -use mdbook::book::Chapter; use mdbook::BookItem; +use mdbook::book::Chapter; use snapbox::file; use std::path::{Path, PathBuf}; @@ -62,7 +62,10 @@ fn reasonable_search_index() { // See note about InlineHtml in search.rs. Ideally the `alert()` part // should not be in the index, but we don't have a way to scrub inline // html. - assert_eq!(docs[&sneaky]["body"], "I put <HTML> in here! Sneaky inline event alert(\"inline\");. But regular inline is indexed."); + assert_eq!( + docs[&sneaky]["body"], + "I put <HTML> in here! Sneaky inline event alert(\"inline\");. But regular inline is indexed." + ); assert_eq!( docs[&no_headers]["breadcrumbs"], "First Chapter ยป No Headers"