Skip to content

Commit

Permalink
feat: add Display for detached modifiers, remove custom to_string
Browse files Browse the repository at this point in the history
… code for ParagraphSegmentTokens
  • Loading branch information
vhyrro committed Aug 15, 2024
1 parent d89c701 commit 7a34faf
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/snapshots/rust_norg__tests__carryover_tags.snap
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ expression: examples
name:
- tag-name_
parameters:
- one large parameter
- "one\\ large\\ parameter"
next_object:
Paragraph:
- Token:
Expand All @@ -45,7 +45,7 @@ expression: examples
name:
- tag-name_
parameters:
- one large parameter
- "one\\ large\\ parameter"
- "&^@!"
- third
- parameter
Expand Down Expand Up @@ -98,7 +98,7 @@ expression: examples
name:
- tag-name_
parameters:
- one large parameter
- "one\\ large\\ parameter"
next_object:
Paragraph:
- Token:
Expand All @@ -108,7 +108,7 @@ expression: examples
name:
- tag-name_
parameters:
- one large parameter
- "one\\ large\\ parameter"
- "&^@!"
- third
- parameter
Expand Down
6 changes: 3 additions & 3 deletions src/snapshots/rust_norg__tests__infirm_tags.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: src/main.rs
source: src/lib.rs
expression: examples
---
- - InfirmTag:
Expand All @@ -19,12 +19,12 @@ expression: examples
name:
- tag-name_
parameters:
- one large parameter
- "one\\ large\\ parameter"
- - InfirmTag:
name:
- tag-name_
parameters:
- one large parameter
- "one\\ large\\ parameter"
- "&^@!"
- third
- parameter
Expand Down
4 changes: 2 additions & 2 deletions src/snapshots/rust_norg__tests__ranged_tags.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ expression: examples
- some-text
- here
parameters:
- one parameter
- "one\\ parameter"
- second-parameter
content:
- CarryoverTag:
Expand Down Expand Up @@ -109,7 +109,7 @@ expression: examples
- some-text
- here
parameters:
- one parameter
- "one\\ parameter"
- second-parameter
content:
- CarryoverTag:
Expand Down
2 changes: 1 addition & 1 deletion src/snapshots/rust_norg__tests__ranged_verbatim_tags.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ expression: examples
- some-text
- here
parameters:
- lua language
- "lua\\ language"
- second-parameter
content: "print(\"Hello world!\")\n"
- - VerbatimRangedTag:
Expand Down
4 changes: 1 addition & 3 deletions src/stage_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ pub enum ParagraphSegmentToken {
impl std::fmt::Display for ParagraphSegmentToken {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
// Here, we purposefully strip the `\` backslash from the escape sequence to make
// output cleaner. This is unlikely to have any serious consequences.
Self::Escape(c) => f.write_char(*c),
Self::Escape(c) => write!(f, "\\{}", c),
Self::Text(str) => f.write_str(str),
Self::Special(c) => f.write_char(*c),
Self::Whitespace => f.write_char(' '),
Expand Down
31 changes: 24 additions & 7 deletions src/stage_3.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Write;

use chumsky::prelude::*;
use itertools::Itertools;
use serde::Serialize;
Expand All @@ -11,13 +13,33 @@ pub enum NestableDetachedModifier {
OrderedList,
}

impl std::fmt::Display for NestableDetachedModifier {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Quote => f.write_char('>'),
Self::UnorderedList => f.write_char('-'),
Self::OrderedList => f.write_char('~'),
}
}
}

#[derive(Clone, Hash, Debug, PartialEq, Eq, Serialize)]
pub enum RangeableDetachedModifier {
Definition,
Footnote,
Table,
}

impl std::fmt::Display for RangeableDetachedModifier {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Definition => f.write_char('$'),
Self::Footnote => f.write_char('^'),
Self::Table => f.write_char(':'),
}
}
}

#[derive(Clone, Hash, Debug, PartialEq, Eq, Serialize)]
pub enum TodoStatus {
/// ` `
Expand Down Expand Up @@ -531,13 +553,8 @@ fn detached_modifier_extensions() -> impl Parser<
.map(|tokens| {
if let Some(tokens) = tokens {
tokens
.iter()
.map(|spec| match spec {
Special(char) => char.to_string(),
Text(str) => str.to_owned(),
Escape(char) => format!(r"\{}", char),
Whitespace => ' '.to_string(),
})
.into_iter()
.map_into::<String>()
.collect()
} else {
String::from("")
Expand Down

0 comments on commit 7a34faf

Please sign in to comment.