From 9313bac4d0946a2e8bd0c8ca68d5fd3573891e45 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sat, 25 Sep 2021 10:41:35 +0800 Subject: [PATCH] Format with nightly ``` group_imports = "StdExternalCrate" imports_granularity = "Crate" max_width = 120 ``` --- examples/stupicat.rs | 8 ++--- rustfmt.toml | 1 + src/lib.rs | 81 ++++++++++++++------------------------------ tests/display.rs | 57 ++++++++++++++----------------- tests/fmt.rs | 34 ++++++------------- tests/spec.rs | 33 ++++++------------ 6 files changed, 74 insertions(+), 140 deletions(-) create mode 100644 rustfmt.toml diff --git a/examples/stupicat.rs b/examples/stupicat.rs index 0d3ceb3..0b8963b 100644 --- a/examples/stupicat.rs +++ b/examples/stupicat.rs @@ -1,14 +1,12 @@ -use pulldown_cmark::Parser; -use pulldown_cmark_to_cmark::cmark; use std::{ env, ffi::OsString, fs::File, - io::stdout, - io::{Read, Write}, + io::{stdout, Read, Write}, }; -use pulldown_cmark::Options; +use pulldown_cmark::{Options, Parser}; +use pulldown_cmark_to_cmark::cmark; fn main() { let path = env::args_os() diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..7530651 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1 @@ +max_width = 120 diff --git a/src/lib.rs b/src/lib.rs index 0fac66f..1d431ab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,9 @@ +use std::{ + borrow::{Borrow, Cow}, + fmt, +}; + use pulldown_cmark::{Alignment as TableAlignment, Event}; -use std::{borrow::Borrow, borrow::Cow, fmt}; pub const SPECIAL_CHARACTERS: &[u8] = br#"#\_*<>`|[]"#; @@ -151,11 +155,7 @@ where } } - fn print_text_without_trailing_newline<'a, F>( - t: &str, - f: &mut F, - p: &[Cow<'a, str>], - ) -> fmt::Result + fn print_text_without_trailing_newline<'a, F>(t: &str, f: &mut F, p: &[Cow<'a, str>]) -> fmt::Result where F: fmt::Write, { @@ -177,18 +177,12 @@ where fn padding_of(l: Option) -> Cow<'static, str> { match l { None => " ".into(), - Some(n) => format!("{}. ", n) - .chars() - .map(|_| ' ') - .collect::() - .into(), + Some(n) => format!("{}. ", n).chars().map(|_| ' ').collect::().into(), } } for event in events { - use pulldown_cmark::CodeBlockKind; - use pulldown_cmark::Event::*; - use pulldown_cmark::Tag::*; + use pulldown_cmark::{CodeBlockKind, Event::*, Tag::*}; let event = event.borrow(); @@ -233,9 +227,7 @@ where Start(ref tag) => { if let List(ref list_type) = *tag { state.list_stack.push(*list_type); - if state.list_stack.len() > 1 - && state.newlines_before_start < options.newlines_after_rest - { + if state.list_stack.len() > 1 && state.newlines_before_start < options.newlines_after_rest { state.newlines_before_start = options.newlines_after_rest; } } @@ -284,9 +276,7 @@ where if consumed_newlines { formatter.write_str(" > ") } else { - formatter - .write_char('\n') - .and(padding(&mut formatter, &state.padding)) + formatter.write_char('\n').and(padding(&mut formatter, &state.padding)) } } CodeBlock(CodeBlockKind::Indented) => { @@ -306,12 +296,10 @@ where Ok(()) }; - s.and_then(|_| { - formatter.write_str(&"`".repeat(options.code_block_backticks)) - }) - .and_then(|_| formatter.write_str(info)) - .and_then(|_| formatter.write_char('\n')) - .and_then(|_| padding(&mut formatter, &state.padding)) + s.and_then(|_| formatter.write_str(&"`".repeat(options.code_block_backticks))) + .and_then(|_| formatter.write_str(info)) + .and_then(|_| formatter.write_char('\n')) + .and_then(|_| padding(&mut formatter, &state.padding)) } List(_) => Ok(()), Strikethrough => formatter.write_str("~~"), @@ -359,12 +347,10 @@ where Ok(()) } TableCell => { - state - .table_headers - .push(match state.text_for_header.take() { - Some(text) => text, - None => " ".into(), - }); + state.table_headers.push(match state.text_for_header.take() { + Some(text) => text, + None => " ".into(), + }); Ok(()) } ref t @ TableRow | ref t @ TableHead => { @@ -377,23 +363,16 @@ where formatter .write_char('\n') .and(padding(&mut formatter, &state.padding))?; - for (alignment, name) in state - .table_alignments - .iter() - .zip(state.table_headers.iter()) - { + for (alignment, name) in state.table_alignments.iter().zip(state.table_headers.iter()) { formatter.write_char('|')?; // NOTE: For perfect counting, count grapheme clusters. // The reason this is not done is to avoid the dependency. let last_minus_one = name.chars().count().saturating_sub(1); for c in 0..name.len() { formatter.write_char( - if (c == 0 - && (alignment == &Alignment::Center - || alignment == &Alignment::Left)) + if (c == 0 && (alignment == &Alignment::Center || alignment == &Alignment::Left)) || (c == last_minus_one - && (alignment == &Alignment::Center - || alignment == &Alignment::Right)) + && (alignment == &Alignment::Center || alignment == &Alignment::Right)) { ':' } else { @@ -415,9 +394,7 @@ where } List(_) => { state.list_stack.pop(); - if state.list_stack.is_empty() - && state.newlines_before_start < options.newlines_after_list - { + if state.list_stack.is_empty() && state.newlines_before_start < options.newlines_after_list { state.newlines_before_start = options.newlines_after_list; } Ok(()) @@ -434,12 +411,8 @@ where FootnoteDefinition(_) => Ok(()), Strikethrough => formatter.write_str("~~"), }, - HardBreak => formatter - .write_str(" \n") - .and(padding(&mut formatter, &state.padding)), - SoftBreak => formatter - .write_char('\n') - .and(padding(&mut formatter, &state.padding)), + HardBreak => formatter.write_str(" \n").and(padding(&mut formatter, &state.padding)), + SoftBreak => formatter.write_char('\n').and(padding(&mut formatter, &state.padding)), Text(ref text) => { if state.store_next_text { state.store_next_text = false; @@ -468,11 +441,7 @@ where } /// As [`cmark_with_options()`], but with default [`Options`]. -pub fn cmark<'a, I, E, F>( - events: I, - formatter: F, - state: Option>, -) -> Result, fmt::Error> +pub fn cmark<'a, I, E, F>(events: I, formatter: F, state: Option>) -> Result, fmt::Error> where I: Iterator, E: Borrow>, diff --git a/tests/display.rs b/tests/display.rs index eac6c64..47492c7 100644 --- a/tests/display.rs +++ b/tests/display.rs @@ -7,9 +7,10 @@ fn s(e: Event) -> String { buf } mod code { - use super::s; use pulldown_cmark::Event::*; + use super::s; + #[test] fn code() { assert_eq!(s(Code("foo\nbar".into())), "`foo\nbar`") @@ -17,9 +18,10 @@ mod code { } mod rule { - use super::s; use pulldown_cmark::Event::*; + use super::s; + #[test] fn rule() { assert_eq!(s(Rule), "---") @@ -27,12 +29,15 @@ mod rule { } mod start { + use pulldown_cmark::{ + Alignment::{self, Center, Left, Right}, + CodeBlockKind, + Event::*, + LinkType::*, + Tag::*, + }; + use super::s; - use pulldown_cmark::Alignment::{self, Center, Left, Right}; - use pulldown_cmark::CodeBlockKind; - use pulldown_cmark::Event::*; - use pulldown_cmark::LinkType::*; - use pulldown_cmark::Tag::*; #[test] fn paragraph() { @@ -99,10 +104,7 @@ mod start { } #[test] fn table() { - assert_eq!( - s(Start(Table(vec![Left, Center, Right, Alignment::None]))), - "" - ) + assert_eq!(s(Start(Table(vec![Left, Center, Right, Alignment::None]))), "") } #[test] fn table_head() { @@ -119,12 +121,15 @@ mod start { } mod end { + use pulldown_cmark::{ + Alignment::{self, Center, Left, Right}, + CodeBlockKind, + Event::*, + LinkType::*, + Tag::*, + }; + use super::s; - use pulldown_cmark::Alignment::{self, Center, Left, Right}; - use pulldown_cmark::CodeBlockKind; - use pulldown_cmark::Event::*; - use pulldown_cmark::LinkType::*; - use pulldown_cmark::Tag::*; #[test] fn header() { @@ -140,10 +145,7 @@ mod end { } #[test] fn codeblock() { - assert_eq!( - s(End(CodeBlock(CodeBlockKind::Fenced("asdf".into())))), - "````" - ) + assert_eq!(s(End(CodeBlock(CodeBlockKind::Fenced("asdf".into())))), "````") } #[test] fn footnote_definition() { @@ -171,10 +173,7 @@ mod end { } #[test] fn link() { - assert_eq!( - s(End(Link(Inline, "/uri".into(), "title".into()))), - "](/uri \"title\")" - ) + assert_eq!(s(End(Link(Inline, "/uri".into(), "title".into()))), "](/uri \"title\")") } #[test] fn link_without_title() { @@ -193,10 +192,7 @@ mod end { } #[test] fn table() { - assert_eq!( - s(End(Table(vec![Left, Center, Right, Alignment::None]))), - "" - ) + assert_eq!(s(End(Table(vec![Left, Center, Right, Alignment::None]))), "") } #[test] fn table_row() { @@ -218,10 +214,7 @@ fn softbreak() { } #[test] fn html() { - assert_eq!( - s(Event::Html("hi
".into())), - "hi
" - ) + assert_eq!(s(Event::Html("hi
".into())), "hi
") } #[test] fn text() { diff --git a/tests/fmt.rs b/tests/fmt.rs index 1a86718..5815a0b 100644 --- a/tests/fmt.rs +++ b/tests/fmt.rs @@ -33,15 +33,11 @@ fn assert_events_eq(s: &str) { let before_events = Parser::new_ext(s, Options::all()); let after_events = Parser::new_ext(&buf, Options::all()); println!("{}", buf); - assert_eq!( - before_events.collect::>(), - after_events.collect::>() - ); + assert_eq!(before_events.collect::>(), after_events.collect::>()); } mod lazy_newlines { - use super::{fmte, fmts}; - use super::{Event, LinkType, State, Tag}; + use super::{fmte, fmts, Event, LinkType, State, Tag}; #[test] fn after_emphasis_there_is_no_newline() { @@ -287,10 +283,7 @@ mod blockquote { } #[test] fn with_markdown_nested_in_html() { - assert_eq!( - fmts("\n\n*foo*\n\n").0, - "\n\n*foo*\n\n" - ) + assert_eq!(fmts("\n\n*foo*\n\n").0, "\n\n*foo*\n\n") } #[test] fn with_codeblock() { @@ -498,10 +491,7 @@ mod codeblock { #[test] fn it_keeps_track_of_the_presence_of_a_code_block() { assert_eq!( - fmte(&[Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced( - "s".into() - ))),]) - .1, + fmte(&[Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced("s".into()))),]).1, State { is_in_code_block: true, ..Default::default() @@ -551,10 +541,11 @@ mod codeblock { } mod table { - use super::{fmte, fmtes, Alignment as TableAlignment, Event, State, Tag}; use pretty_assertions::assert_eq; use pulldown_cmark_to_cmark::Alignment; + use super::{fmte, fmtes, Alignment as TableAlignment, Event, State, Tag}; + #[test] fn it_forgets_alignments_and_headers_at_the_end_of_tables() { assert_eq!( @@ -577,10 +568,7 @@ mod table { fn it_keeps_track_of_alignments_and_headers() { assert_eq!( fmte(&[ - Event::Start(Tag::Table(vec![ - TableAlignment::None, - TableAlignment::Center, - ])), + Event::Start(Tag::Table(vec![TableAlignment::None, TableAlignment::Center,])), Event::Start(Tag::TableHead), Event::Start(Tag::TableCell), Event::Text("a".into()), @@ -634,9 +622,10 @@ mod table { } mod escapes { - use crate::{fmts, Event, Parser, Tag, SPECIAL_CHARACTERS}; use pulldown_cmark::CowStr; + use crate::{fmts, Event, Parser, Tag, SPECIAL_CHARACTERS}; + fn run_test_on_each_special_char(f: impl Fn(String, CowStr)) { use std::convert::TryFrom; for c in SPECIAL_CHARACTERS.iter() { @@ -657,10 +646,7 @@ mod escapes { #[test] fn it_recreates_escapes_for_known_special_characters_at_the_beginning_of_the_word() { run_test_on_each_special_char(|escaped_special_character, _| { - assert_eq!( - fmts(&escaped_special_character).0, - escaped_special_character - ); + assert_eq!(fmts(&escaped_special_character).0, escaped_special_character); }) } diff --git a/tests/spec.rs b/tests/spec.rs index e582e62..120aff5 100644 --- a/tests/spec.rs +++ b/tests/spec.rs @@ -14,27 +14,15 @@ fn is_example_fence(tag: &Tag<'_>) -> bool { } fn collect_test_case<'a>(events: &mut impl Iterator>) -> Option<(String, String)> { - let begin_tag = events.next().and_then(|e| { - if let Event::Start(tag) = e { - Some(tag) - } else { - None - } - })?; - let text = events.next().and_then(|e| { - if let Event::Text(text) = e { - Some(text) - } else { - None - } - })?; - let end_tag = events.next().and_then(|e| { - if let Event::End(tag) = e { - Some(tag) - } else { - None - } - })?; + let begin_tag = events + .next() + .and_then(|e| if let Event::Start(tag) = e { Some(tag) } else { None })?; + let text = events + .next() + .and_then(|e| if let Event::Text(text) = e { Some(text) } else { None })?; + let end_tag = events + .next() + .and_then(|e| if let Event::End(tag) = e { Some(tag) } else { None })?; if !(is_example_fence(&begin_tag) && is_example_fence(&end_tag)) { return None; } @@ -86,8 +74,7 @@ fn commonmark_spec() { if let Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(fence_value))) = peeked_event { if &**fence_value == "example" { // a new example, insert it into the testsuite. - let new_test_case = - collect_test_case(&mut p).expect("Error parsing example text from spec."); + let new_test_case = collect_test_case(&mut p).expect("Error parsing example text from spec."); testsuite.push(new_test_case); continue; }