Skip to content

Commit

Permalink
Format with nightly
Browse files Browse the repository at this point in the history
```
group_imports = "StdExternalCrate"
imports_granularity = "Crate"
max_width = 120
```
  • Loading branch information
Byron committed Sep 25, 2021
1 parent 72e5ca5 commit 9313bac
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 140 deletions.
8 changes: 3 additions & 5 deletions examples/stupicat.rs
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
max_width = 120
81 changes: 25 additions & 56 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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#"#\_*<>`|[]"#;

Expand Down Expand Up @@ -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,
{
Expand All @@ -177,18 +177,12 @@ where
fn padding_of(l: Option<u64>) -> Cow<'static, str> {
match l {
None => " ".into(),
Some(n) => format!("{}. ", n)
.chars()
.map(|_| ' ')
.collect::<String>()
.into(),
Some(n) => format!("{}. ", n).chars().map(|_| ' ').collect::<String>().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();

Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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) => {
Expand All @@ -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("~~"),
Expand Down Expand Up @@ -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 => {
Expand All @@ -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 {
Expand All @@ -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(())
Expand All @@ -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;
Expand Down Expand Up @@ -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<State<'static>>,
) -> Result<State<'static>, fmt::Error>
pub fn cmark<'a, I, E, F>(events: I, formatter: F, state: Option<State<'static>>) -> Result<State<'static>, fmt::Error>
where
I: Iterator<Item = E>,
E: Borrow<Event<'a>>,
Expand Down
57 changes: 25 additions & 32 deletions tests/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,37 @@ 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`")
}
}

mod rule {
use super::s;
use pulldown_cmark::Event::*;

use super::s;

#[test]
fn rule() {
assert_eq!(s(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() {
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -218,10 +214,7 @@ fn softbreak() {
}
#[test]
fn html() {
assert_eq!(
s(Event::Html("<table>hi</table>".into())),
"<table>hi</table>"
)
assert_eq!(s(Event::Html("<table>hi</table>".into())), "<table>hi</table>")
}
#[test]
fn text() {
Expand Down
Loading

0 comments on commit 9313bac

Please sign in to comment.