Skip to content

Commit

Permalink
Run cargo fmt and assure it doesn't regress anymore.
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Oct 5, 2024
1 parent db76794 commit 3fdf308
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 69 deletions.
11 changes: 7 additions & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: tests
run: |
make tests
- uses: actions/checkout@v2
- name: cargo fmt
run: |
cargo fmt --check
- name: tests
run: |
make tests
msrv:
runs-on: ubuntu-latest
Expand Down
102 changes: 63 additions & 39 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
};

use pulldown_cmark::{
Alignment as TableAlignment, BlockQuoteKind, Event, HeadingLevel, LinkType, MetadataBlockKind, Tag, TagEnd
Alignment as TableAlignment, BlockQuoteKind, Event, HeadingLevel, LinkType, MetadataBlockKind, Tag, TagEnd,
};

mod source_range;
Expand Down Expand Up @@ -98,18 +98,44 @@ impl State<'_> {
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum LinkCategory<'a> {
AngleBracketed,
Reference { uri: Cow<'a, str>, title: Cow<'a, str>, id: Cow<'a, str> },
Collapsed { uri: Cow<'a, str>, title: Cow<'a, str> },
Shortcut { uri: Cow<'a, str>, title: Cow<'a, str> },
Other { uri: Cow<'a, str>, title: Cow<'a, str> },
Reference {
uri: Cow<'a, str>,
title: Cow<'a, str>,
id: Cow<'a, str>,
},
Collapsed {
uri: Cow<'a, str>,
title: Cow<'a, str>,
},
Shortcut {
uri: Cow<'a, str>,
title: Cow<'a, str>,
},
Other {
uri: Cow<'a, str>,
title: Cow<'a, str>,
},
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ImageLink<'a> {
Reference { uri: Cow<'a, str>, title: Cow<'a, str>, id: Cow<'a, str> },
Collapsed { uri: Cow<'a, str>, title: Cow<'a, str> },
Shortcut { uri: Cow<'a, str>, title: Cow<'a, str> },
Other { uri: Cow<'a, str>, title: Cow<'a, str> },
Reference {
uri: Cow<'a, str>,
title: Cow<'a, str>,
id: Cow<'a, str>,
},
Collapsed {
uri: Cow<'a, str>,
title: Cow<'a, str>,
},
Shortcut {
uri: Cow<'a, str>,
title: Cow<'a, str>,
},
Other {
uri: Cow<'a, str>,
title: Cow<'a, str>,
},
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down Expand Up @@ -310,7 +336,7 @@ where
}
None => Ok(()),
}
},
}
Table(alignments) => {
state.table_alignments = alignments.iter().map(From::from).collect();
Ok(())
Expand Down Expand Up @@ -384,14 +410,14 @@ where
uri: dest_url.clone().into(),
title: title.clone().into(),
}
},
}
LinkType::Shortcut => {
state.current_shortcut_text = Some(String::new());
ImageLink::Shortcut {
uri: dest_url.clone().into(),
title: title.clone().into(),
}
},
}
_ => ImageLink::Other {
uri: dest_url.clone().into(),
title: title.clone().into(),
Expand All @@ -408,7 +434,7 @@ where
Paragraph => {
state.last_was_paragraph_start = true;
Ok(())
},
}
Heading {
level,
id,
Expand Down Expand Up @@ -528,37 +554,35 @@ where
}
LinkCategory::Other { uri, title } => close_link(&uri, &title, formatter, LinkType::Inline),
},
TagEnd::Image => {
match state.image_stack.pop().unwrap() {
ImageLink::Reference { uri, title, id } => {
TagEnd::Image => match state.image_stack.pop().unwrap() {
ImageLink::Reference { uri, title, id } => {
state
.shortcuts
.push((id.to_string(), uri.to_string(), title.to_string()));
formatter.write_str("][")?;
formatter.write_str(&id)?;
formatter.write_char(']')
}
ImageLink::Collapsed { uri, title } => {
if let Some(shortcut_text) = state.current_shortcut_text.take() {
state
.shortcuts
.push((id.to_string(), uri.to_string(), title.to_string()));
formatter.write_str("][")?;
formatter.write_str(&id)?;
formatter.write_char(']')
}
ImageLink::Collapsed { uri, title } => {
if let Some(shortcut_text) = state.current_shortcut_text.take() {
state
.shortcuts
.push((shortcut_text, uri.to_string(), title.to_string()));
}
formatter.write_str("][]")
}
ImageLink::Shortcut { uri, title } => {
if let Some(shortcut_text) = state.current_shortcut_text.take() {
state
.shortcuts
.push((shortcut_text, uri.to_string(), title.to_string()));
}
formatter.write_char(']')
.push((shortcut_text, uri.to_string(), title.to_string()));
}
ImageLink::Other { uri, title } => {
close_link(uri.as_ref(), title.as_ref(), formatter, LinkType::Inline)
formatter.write_str("][]")
}
ImageLink::Shortcut { uri, title } => {
if let Some(shortcut_text) = state.current_shortcut_text.take() {
state
.shortcuts
.push((shortcut_text, uri.to_string(), title.to_string()));
}
formatter.write_char(']')
}
}
ImageLink::Other { uri, title } => {
close_link(uri.as_ref(), title.as_ref(), formatter, LinkType::Inline)
}
},
TagEnd::Emphasis => formatter.write_char(options.emphasis_token),
TagEnd::Strong => formatter.write_str(options.strong_token),
TagEnd::Heading(_) => {
Expand Down
2 changes: 1 addition & 1 deletion src/source_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use super::{cmark_resume_one_event, fmt, Borrow, Event, Options, Range, State};
/// * Markdown source from which `event_and_ranges` are created.
/// 1. **event_and_ranges**
/// * An iterator over [`Event`]-range pairs, for example as returned by [`pulldown_cmark::OffsetIter`].
/// Must match what's provided in `source`.
/// Must match what's provided in `source`.
/// 1. **formatter**
/// * A format writer, can be a `String`.
/// 1. **state**
Expand Down
62 changes: 37 additions & 25 deletions tests/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,37 +216,49 @@ mod end {
}
#[test]
fn codeblock() {
assert_eq!(es([Start(Tag::CodeBlock(CodeBlockKind::Fenced("".into()))), End(TagEnd::CodeBlock)]), "\n````\n````");
assert_eq!(
es([
Start(Tag::CodeBlock(CodeBlockKind::Fenced("".into()))),
End(TagEnd::CodeBlock)
]),
"\n````\n````"
);
}
#[test]
fn codeblock_in_list_item() {
assert_eq!(es([
Start(Tag::List(None)),
Start(Tag::Item),
Start(Tag::CodeBlock(CodeBlockKind::Fenced("".into()))),
Text("foo".into()),
End(TagEnd::CodeBlock),
End(TagEnd::Item),
End(TagEnd::List(false)),
Start(Tag::Paragraph),
Text("bar".into()),
End(TagEnd::Paragraph),
]), "* \n ````\n foo\n ````\n\nbar");
assert_eq!(
es([
Start(Tag::List(None)),
Start(Tag::Item),
Start(Tag::CodeBlock(CodeBlockKind::Fenced("".into()))),
Text("foo".into()),
End(TagEnd::CodeBlock),
End(TagEnd::Item),
End(TagEnd::List(false)),
Start(Tag::Paragraph),
Text("bar".into()),
End(TagEnd::Paragraph),
]),
"* \n ````\n foo\n ````\n\nbar"
);
}
#[test]
fn codeblock_indented_in_list_item() {
assert_eq!(es([
Start(Tag::List(None)),
Start(Tag::Item),
Start(Tag::CodeBlock(CodeBlockKind::Indented)),
Text("foo".into()),
End(TagEnd::CodeBlock),
End(TagEnd::Item),
End(TagEnd::List(false)),
Start(Tag::Paragraph),
Text("bar".into()),
End(TagEnd::Paragraph),
]), "* \n foo\n \n\nbar");
assert_eq!(
es([
Start(Tag::List(None)),
Start(Tag::Item),
Start(Tag::CodeBlock(CodeBlockKind::Indented)),
Text("foo".into()),
End(TagEnd::CodeBlock),
End(TagEnd::Item),
End(TagEnd::List(false)),
Start(Tag::Paragraph),
Text("bar".into()),
End(TagEnd::Paragraph),
]),
"* \n foo\n \n\nbar"
);
}
#[test]
fn footnote_definition() {
Expand Down

0 comments on commit 3fdf308

Please sign in to comment.