Format better and remove unnecessary borrows

This commit is contained in:
Ben Armstead 2021-08-24 08:45:06 +01:00
parent e6629cd75b
commit 1700783594
10 changed files with 32 additions and 36 deletions

View File

@ -37,7 +37,7 @@ fn handle_preprocessing(pre: &dyn Preprocessor) -> Result<(), Error> {
let book_version = Version::parse(&ctx.mdbook_version)?; let book_version = Version::parse(&ctx.mdbook_version)?;
let version_req = VersionReq::parse(mdbook::MDBOOK_VERSION)?; let version_req = VersionReq::parse(mdbook::MDBOOK_VERSION)?;
if version_req.matches(&book_version) != true { if !version_req.matches(&book_version) {
eprintln!( eprintln!(
"Warning: The {} plugin was built against version {} of mdbook, \ "Warning: The {} plugin was built against version {} of mdbook, \
but we're being called from version {}", but we're being called from version {}",
@ -55,7 +55,7 @@ fn handle_preprocessing(pre: &dyn Preprocessor) -> Result<(), Error> {
fn handle_supports(pre: &dyn Preprocessor, sub_args: &ArgMatches) -> ! { fn handle_supports(pre: &dyn Preprocessor, sub_args: &ArgMatches) -> ! {
let renderer = sub_args.value_of("renderer").expect("Required argument"); let renderer = sub_args.value_of("renderer").expect("Required argument");
let supported = pre.supports_renderer(&renderer); let supported = pre.supports_renderer(renderer);
// Signal whether the renderer is supported by exiting with 1 or 0. // Signal whether the renderer is supported by exiting with 1 or 0.
if supported { if supported {

View File

@ -22,7 +22,7 @@ pub fn load_book<P: AsRef<Path>>(src_dir: P, cfg: &BuildConfig) -> Result<Book>
.with_context(|| format!("Summary parsing failed for file={:?}", summary_md))?; .with_context(|| format!("Summary parsing failed for file={:?}", summary_md))?;
if cfg.create_missing { if cfg.create_missing {
create_missing(&src_dir, &summary).with_context(|| "Unable to create missing chapters")?; create_missing(src_dir, &summary).with_context(|| "Unable to create missing chapters")?;
} }
load_book_from_disk(&summary, src_dir) load_book_from_disk(&summary, src_dir)
@ -381,7 +381,7 @@ And here is some \
root.nested_items.push(second.clone().into()); root.nested_items.push(second.clone().into());
root.nested_items.push(SummaryItem::Separator); root.nested_items.push(SummaryItem::Separator);
root.nested_items.push(second.clone().into()); root.nested_items.push(second.into());
(root, temp_dir) (root, temp_dir)
} }
@ -454,7 +454,7 @@ And here is some \
sub_items: vec![ sub_items: vec![
BookItem::Chapter(nested.clone()), BookItem::Chapter(nested.clone()),
BookItem::Separator, BookItem::Separator,
BookItem::Chapter(nested.clone()), BookItem::Chapter(nested),
], ],
}); });

View File

@ -382,7 +382,7 @@ impl<'a> SummaryParser<'a> {
} }
Some(ev @ Event::Start(Tag::List(..))) => { Some(ev @ Event::Start(Tag::List(..))) => {
self.back(ev); self.back(ev);
let mut bunch_of_items = self.parse_nested_numbered(&root_number)?; let mut bunch_of_items = self.parse_nested_numbered(root_number)?;
// if we've resumed after something like a rule the root sections // if we've resumed after something like a rule the root sections
// will be numbered from 1. We need to manually go back and update // will be numbered from 1. We need to manually go back and update

View File

@ -49,7 +49,7 @@ impl CmdPreprocessor {
fn write_input_to_child(&self, child: &mut Child, book: &Book, ctx: &PreprocessorContext) { fn write_input_to_child(&self, child: &mut Child, book: &Book, ctx: &PreprocessorContext) {
let stdin = child.stdin.take().expect("Child has stdin"); let stdin = child.stdin.take().expect("Child has stdin");
if let Err(e) = self.write_input(stdin, &book, &ctx) { if let Err(e) = self.write_input(stdin, book, ctx) {
// Looks like the backend hung up before we could finish // Looks like the backend hung up before we could finish
// sending it the render context. Log the error and keep going // sending it the render context. Log the error and keep going
warn!("Error writing the RenderContext to the backend, {}", e); warn!("Error writing the RenderContext to the backend, {}", e);

View File

@ -57,7 +57,7 @@ impl HtmlHandlebars {
let fixed_content = utils::render_markdown_with_path( let fixed_content = utils::render_markdown_with_path(
&ch.content, &ch.content,
ctx.html_config.curly_quotes, ctx.html_config.curly_quotes,
Some(&path), Some(path),
); );
if !ctx.is_index { if !ctx.is_index {
// Add page break between chapters // Add page break between chapters
@ -178,7 +178,7 @@ impl HtmlHandlebars {
let rendered = let rendered =
self.post_process(rendered, &html_config.playground, ctx.config.rust.edition); self.post_process(rendered, &html_config.playground, ctx.config.rust.edition);
let output_file = get_404_output_file(&html_config.input_404); let output_file = get_404_output_file(&html_config.input_404);
utils::fs::write_file(&destination, output_file, rendered.as_bytes())?; utils::fs::write_file(destination, output_file, rendered.as_bytes())?;
debug!("Creating 404.html ✓"); debug!("Creating 404.html ✓");
Ok(()) Ok(())
} }
@ -223,10 +223,10 @@ impl HtmlHandlebars {
} }
write_file(destination, "css/variables.css", &theme.variables_css)?; write_file(destination, "css/variables.css", &theme.variables_css)?;
if let Some(contents) = &theme.favicon_png { if let Some(contents) = &theme.favicon_png {
write_file(destination, "favicon.png", &contents)?; write_file(destination, "favicon.png", contents)?;
} }
if let Some(contents) = &theme.favicon_svg { if let Some(contents) = &theme.favicon_svg {
write_file(destination, "favicon.svg", &contents)?; write_file(destination, "favicon.svg", contents)?;
} }
write_file(destination, "highlight.css", &theme.highlight_css)?; write_file(destination, "highlight.css", &theme.highlight_css)?;
write_file(destination, "tomorrow-night.css", &theme.tomorrow_night_css)?; write_file(destination, "tomorrow-night.css", &theme.tomorrow_night_css)?;
@ -509,7 +509,7 @@ impl Renderer for HtmlHandlebars {
debug!("Register handlebars helpers"); debug!("Register handlebars helpers");
self.register_hbs_helpers(&mut handlebars, &html_config); self.register_hbs_helpers(&mut handlebars, &html_config);
let mut data = make_data(&ctx.root, &book, &ctx.config, &html_config, &theme)?; let mut data = make_data(&ctx.root, book, &ctx.config, &html_config, &theme)?;
// Print version // Print version
let mut print_content = String::new(); let mut print_content = String::new();
@ -552,14 +552,14 @@ impl Renderer for HtmlHandlebars {
let rendered = let rendered =
self.post_process(rendered, &html_config.playground, ctx.config.rust.edition); self.post_process(rendered, &html_config.playground, ctx.config.rust.edition);
utils::fs::write_file(&destination, "print.html", rendered.as_bytes())?; utils::fs::write_file(destination, "print.html", rendered.as_bytes())?;
debug!("Creating print.html ✓"); debug!("Creating print.html ✓");
} }
debug!("Copy static files"); debug!("Copy static files");
self.copy_static_files(&destination, &theme, &html_config) self.copy_static_files(destination, &theme, &html_config)
.with_context(|| "Unable to copy across static files")?; .with_context(|| "Unable to copy across static files")?;
self.copy_additional_css_and_js(&html_config, &ctx.root, &destination) self.copy_additional_css_and_js(&html_config, &ctx.root, destination)
.with_context(|| "Unable to copy across additional CSS and JS")?; .with_context(|| "Unable to copy across additional CSS and JS")?;
// Render search index // Render search index
@ -567,7 +567,7 @@ impl Renderer for HtmlHandlebars {
{ {
let search = html_config.search.unwrap_or_default(); let search = html_config.search.unwrap_or_default();
if search.enable { if search.enable {
super::search::create_files(&search, &destination, &book)?; super::search::create_files(&search, destination, book)?;
} }
} }
@ -575,7 +575,7 @@ impl Renderer for HtmlHandlebars {
.context("Unable to emit redirects")?; .context("Unable to emit redirects")?;
// Copy all remaining files, avoid a recursive copy from/to the book build dir // Copy all remaining files, avoid a recursive copy from/to the book build dir
utils::fs::copy_files_except_ext(&src_dir, &destination, true, Some(&build_dir), &["md"])?; utils::fs::copy_files_except_ext(&src_dir, destination, true, Some(&build_dir), &["md"])?;
Ok(()) Ok(())
} }
@ -983,7 +983,7 @@ mod tests {
]; ];
for (src, should_be) in inputs { for (src, should_be) in inputs {
let got = build_header_links(&src); let got = build_header_links(src);
assert_eq!(got, should_be); assert_eq!(got, should_be);
} }
} }

View File

@ -91,7 +91,7 @@ fn find_chapter(
match item.get("path") { match item.get("path") {
Some(path) if !path.is_empty() => { Some(path) if !path.is_empty() => {
if let Some(previous) = previous { if let Some(previous) = previous {
if let Some(item) = target.find(&base_path, &path, &item, &previous)? { if let Some(item) = target.find(&base_path, path, &item, &previous)? {
return Ok(Some(item)); return Ok(Some(item));
} }
} }

View File

@ -142,7 +142,7 @@ impl HelperDef for RenderToc {
// Section does not necessarily exist // Section does not necessarily exist
if let Some(section) = item.get("section") { if let Some(section) = item.get("section") {
out.write("<strong aria-hidden=\"true\">")?; out.write("<strong aria-hidden=\"true\">")?;
out.write(&section)?; out.write(section)?;
out.write("</strong> ")?; out.write("</strong> ")?;
} }
} }

View File

@ -17,10 +17,10 @@ pub fn create_files(search_config: &Search, destination: &Path, book: &Book) ->
let mut doc_urls = Vec::with_capacity(book.sections.len()); let mut doc_urls = Vec::with_capacity(book.sections.len());
for item in book.iter() { for item in book.iter() {
render_item(&mut index, &search_config, &mut doc_urls, item)?; render_item(&mut index, search_config, &mut doc_urls, item)?;
} }
let index = write_to_json(index, &search_config, doc_urls)?; let index = write_to_json(index, search_config, doc_urls)?;
debug!("Writing search index ✓"); debug!("Writing search index ✓");
if index.len() > 10_000_000 { if index.len() > 10_000_000 {
warn!("searchindex.json is very large ({} bytes)", index.len()); warn!("searchindex.json is very large ({} bytes)", index.len());
@ -134,7 +134,7 @@ fn render_item(
// in an HtmlBlock tag. We must collect consecutive Html events // in an HtmlBlock tag. We must collect consecutive Html events
// into a block ourselves. // into a block ourselves.
while let Some(Event::Html(html)) = p.peek() { while let Some(Event::Html(html)) = p.peek() {
html_block.push_str(&html); html_block.push_str(html);
p.next(); p.next();
} }

View File

@ -247,7 +247,7 @@ mod tests {
} }
if let Err(e) = if let Err(e) =
copy_files_except_ext(&tmp.path(), &tmp.path().join("output"), true, None, &["md"]) copy_files_except_ext(tmp.path(), &tmp.path().join("output"), true, None, &["md"])
{ {
panic!("Error while executing the function:\n{:?}", e); panic!("Error while executing the function:\n{:?}", e);
} }

View File

@ -264,7 +264,7 @@ fn root_index_html() -> Result<Document> {
fn check_second_toc_level() { fn check_second_toc_level() {
let doc = root_index_html().unwrap(); let doc = root_index_html().unwrap();
let mut should_be = Vec::from(TOC_SECOND_LEVEL); let mut should_be = Vec::from(TOC_SECOND_LEVEL);
should_be.sort(); should_be.sort_unstable();
let pred = descendants!( let pred = descendants!(
Class("chapter"), Class("chapter"),
@ -288,7 +288,7 @@ fn check_first_toc_level() {
let mut should_be = Vec::from(TOC_TOP_LEVEL); let mut should_be = Vec::from(TOC_TOP_LEVEL);
should_be.extend(TOC_SECOND_LEVEL); should_be.extend(TOC_SECOND_LEVEL);
should_be.sort(); should_be.sort_unstable();
let pred = descendants!( let pred = descendants!(
Class("chapter"), Class("chapter"),
@ -535,7 +535,7 @@ fn redirects_are_emitted_correctly() {
let mut redirect_file = md.build_dir_for("html"); let mut redirect_file = md.build_dir_for("html");
// append everything except the bits that make it absolute // append everything except the bits that make it absolute
// (e.g. "/" or "C:\") // (e.g. "/" or "C:\")
redirect_file.extend(remove_absolute_components(&original)); redirect_file.extend(remove_absolute_components(original));
let contents = fs::read_to_string(&redirect_file).unwrap(); let contents = fs::read_to_string(&redirect_file).unwrap();
assert!(contents.contains(redirect)); assert!(contents.contains(redirect));
} }
@ -552,7 +552,7 @@ fn edit_url_has_default_src_dir_edit_url() {
edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path}" edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path}"
"#; "#;
write_file(&temp.path(), "book.toml", book_toml.as_bytes()).unwrap(); write_file(temp.path(), "book.toml", book_toml.as_bytes()).unwrap();
let md = MDBook::load(temp.path()).unwrap(); let md = MDBook::load(temp.path()).unwrap();
md.build().unwrap(); md.build().unwrap();
@ -560,9 +560,7 @@ fn edit_url_has_default_src_dir_edit_url() {
let index_html = temp.path().join("book").join("index.html"); let index_html = temp.path().join("book").join("index.html");
assert_contains_strings( assert_contains_strings(
index_html, index_html,
&vec![ &[r#"href="https://github.com/rust-lang/mdBook/edit/master/guide/src/README.md" title="Suggest an edit""#],
r#"href="https://github.com/rust-lang/mdBook/edit/master/guide/src/README.md" title="Suggest an edit""#,
],
); );
} }
@ -578,7 +576,7 @@ fn edit_url_has_configured_src_dir_edit_url() {
edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path}" edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path}"
"#; "#;
write_file(&temp.path(), "book.toml", book_toml.as_bytes()).unwrap(); write_file(temp.path(), "book.toml", book_toml.as_bytes()).unwrap();
let md = MDBook::load(temp.path()).unwrap(); let md = MDBook::load(temp.path()).unwrap();
md.build().unwrap(); md.build().unwrap();
@ -586,9 +584,7 @@ fn edit_url_has_configured_src_dir_edit_url() {
let index_html = temp.path().join("book").join("index.html"); let index_html = temp.path().join("book").join("index.html");
assert_contains_strings( assert_contains_strings(
index_html, index_html,
&vec![ &[r#"href="https://github.com/rust-lang/mdBook/edit/master/guide/src2/README.md" title="Suggest an edit""#],
r#"href="https://github.com/rust-lang/mdBook/edit/master/guide/src2/README.md" title="Suggest an edit""#,
],
); );
} }
@ -611,7 +607,7 @@ mod search {
let index = fs::read_to_string(index).unwrap(); let index = fs::read_to_string(index).unwrap();
let index = index.trim_start_matches("Object.assign(window.search, "); let index = index.trim_start_matches("Object.assign(window.search, ");
let index = index.trim_end_matches(");"); let index = index.trim_end_matches(");");
serde_json::from_str(&index).unwrap() serde_json::from_str(index).unwrap()
} }
#[test] #[test]