Skip to content

Commit

Permalink
remove context (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lurk authored Apr 18, 2024
1 parent cc7771e commit d2cda6b
Show file tree
Hide file tree
Showing 22 changed files with 141 additions and 270 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ mod toolkit;
/// let yamd = deserialize(input).unwrap();
/// ```
pub fn deserialize(input: &str) -> Option<Yamd> {
Yamd::parse(input, 0, None).map(|(yamd, _)| yamd)
Yamd::parse(input, 0).map(|(yamd, _)| yamd)
}

/// Serialize a Yamd struct into a string
Expand Down
16 changes: 6 additions & 10 deletions src/nodes/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt::{Display, Formatter};

use serde::Serialize;

use crate::toolkit::{context::Context, parser::Parse};
use crate::toolkit::parser::Parse;

/// Representation of an anchor
#[derive(Debug, PartialEq, Serialize, Clone)]
Expand All @@ -27,7 +27,7 @@ impl Display for Anchor {
}

impl Parse for Anchor {
fn parse(input: &str, current_position: usize, _: Option<&Context>) -> Option<(Self, usize)> {
fn parse(input: &str, current_position: usize) -> Option<(Self, usize)> {
if input[current_position..].starts_with('[') {
if let Some(middle) = input[current_position + 1..].find("](") {
let mut level = 1;
Expand Down Expand Up @@ -76,21 +76,17 @@ mod tests {

#[test]
fn parse() {
assert_eq!(
Anchor::parse("[1](2)", 0, None),
Some((Anchor::new("1", "2"), 6))
);
assert_eq!(Anchor::parse("[1", 0, None), None);
assert_eq!(Anchor::parse("[1](2", 0, None), None);
assert_eq!(Anchor::parse("[1](2)", 0), Some((Anchor::new("1", "2"), 6)));
assert_eq!(Anchor::parse("[1", 0), None);
assert_eq!(Anchor::parse("[1](2", 0), None);
}

#[test]
fn deserilalze_with_parentesis_in_url() {
assert_eq!(
Anchor::parse(
"[the Rope data structure](https://en.wikipedia.org/wiki/Rope_(data_structure))",
0,
None
0
),
Some((
Anchor::new(
Expand Down
19 changes: 6 additions & 13 deletions src/nodes/bold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use serde::Serialize;

use crate::{
nodes::{italic::Italic, strikethrough::Strikethrough, text::Text},
toolkit::{
context::Context,
parser::{parse_to_consumer, parse_to_parser, Branch, Consumer, Parse, Parser},
},
toolkit::parser::{parse_to_consumer, parse_to_parser, Branch, Consumer, Parse, Parser},
};

#[derive(Debug, PartialEq, Serialize, Clone)]
Expand Down Expand Up @@ -59,17 +56,13 @@ impl Branch<BoldNodes> for Bold {
}

impl Parse for Bold {
fn parse(input: &str, current_position: usize, _: Option<&Context>) -> Option<(Self, usize)> {
fn parse(input: &str, current_position: usize) -> Option<(Self, usize)> {
if input[current_position..].starts_with("**") {
if let Some(end) = input[current_position + 2..].find("**") {
let b = Bold::new(vec![]);
return Some((
b.parse_branch(
&input[current_position + 2..current_position + 2 + end],
"",
None,
)
.expect("bold should always succed"),
b.parse_branch(&input[current_position + 2..current_position + 2 + end], "")
.expect("bold should always succed"),
end + 4,
));
}
Expand Down Expand Up @@ -138,12 +131,12 @@ mod tests {
#[test]
fn from_string() {
assert_eq!(
Bold::parse("**b**", 0, None),
Bold::parse("**b**", 0),
Some((Bold::new(vec![Text::new("b").into()]), 5))
);

assert_eq!(
Bold::parse("**b ~~st~~ _i t_**", 0, None),
Bold::parse("**b ~~st~~ _i t_**", 0),
Some((
Bold::new(vec![
Text::new("b ").into(),
Expand Down
12 changes: 6 additions & 6 deletions src/nodes/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt::Display;

use serde::Serialize;

use crate::toolkit::{context::Context, parser::Parse};
use crate::toolkit::parser::Parse;

#[derive(Debug, PartialEq, Serialize, Clone)]
pub struct Code {
Expand All @@ -26,7 +26,7 @@ impl Display for Code {
}

impl Parse for Code {
fn parse(input: &str, current_position: usize, _: Option<&Context>) -> Option<(Self, usize)>
fn parse(input: &str, current_position: usize) -> Option<(Self, usize)>
where
Self: Sized,
{
Expand Down Expand Up @@ -64,11 +64,11 @@ mod tests {
#[test]
fn parser() {
assert_eq!(
Code::parse("```rust\nlet a=1;\n```", 0, None),
Code::parse("```rust\nlet a=1;\n```", 0),
Some((Code::new("rust", "let a=1;"), 20))
);
assert_eq!(Code::parse("```rust\nlet a=1;\n", 0, None), None);
assert_eq!(Code::parse("not a code block", 0, None), None);
assert_eq!(Code::parse("``````", 0, None), None);
assert_eq!(Code::parse("```rust\nlet a=1;\n", 0), None);
assert_eq!(Code::parse("not a code block", 0), None);
assert_eq!(Code::parse("``````", 0), None);
}
}
21 changes: 7 additions & 14 deletions src/nodes/collapsible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use std::fmt::Display;

use serde::Serialize;

use crate::toolkit::{
context::Context,
parser::{parse_to_consumer, parse_to_parser, Branch, Consumer, Parse, Parser},
};
use crate::toolkit::parser::{parse_to_consumer, parse_to_parser, Branch, Consumer, Parse, Parser};

use super::{
code::Code, divider::Divider, embed::Embed, heading::Heading, image::Image,
Expand Down Expand Up @@ -151,7 +148,7 @@ impl Branch<CollapsibleNodes> for Collapsible {
}

impl Parse for Collapsible {
fn parse(input: &str, current_position: usize, _: Option<&Context>) -> Option<(Self, usize)> {
fn parse(input: &str, current_position: usize) -> Option<(Self, usize)> {
if input[current_position..].starts_with("{% ") {
let start = current_position + 3;
if let Some(end_of_title) = input[start..].find('\n') {
Expand All @@ -172,7 +169,6 @@ impl Parse for Collapsible {
&input[start + end_of_title + 1
..start + end_of_title + 1 + index],
"\n\n",
None,
)
.expect("collapsible branch should always succeed"),
3 + end_of_title + 1 + index + 3,
Expand Down Expand Up @@ -210,7 +206,7 @@ mod tests {
#[test]
fn test_collapsible_parse() {
assert_eq!(
Collapsible::parse("{% Title\n# Heading\n%}", 0, None),
Collapsible::parse("{% Title\n# Heading\n%}", 0),
Some((
Collapsible::new(
"Title",
Expand All @@ -235,11 +231,8 @@ mod tests {

#[test]
fn fail_to_parse_collapsible() {
assert_eq!(
Collapsible::parse("I am not an accordion tab", 0, None),
None
);
assert_eq!(Collapsible::parse("{% \n%}", 0, None), None);
assert_eq!(Collapsible::parse("I am not an accordion tab", 0), None);
assert_eq!(Collapsible::parse("{% \n%}", 0), None);
}

#[test]
Expand Down Expand Up @@ -322,14 +315,14 @@ t**b**
],
);
assert_eq!(tab.to_string(), input);
assert_eq!(Collapsible::parse(input, 0, None), Some((tab, input.len())));
assert_eq!(Collapsible::parse(input, 0), Some((tab, input.len())));
}

#[test]
fn parse_empty() {
let input = "{% Title\n\n%}";
assert_eq!(
Collapsible::parse(input, 0, None),
Collapsible::parse(input, 0),
Some((Collapsible::new("Title", vec![]), input.len()))
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/nodes/divider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt::Display;

use serde::Serialize;

use crate::toolkit::{context::Context, parser::Parse};
use crate::toolkit::parser::Parse;

#[derive(Debug, PartialEq, Serialize, Clone, Default)]
pub struct Divider {}
Expand All @@ -20,7 +20,7 @@ impl Display for Divider {
}

impl Parse for Divider {
fn parse(input: &str, current_position: usize, _: Option<&Context>) -> Option<(Self, usize)>
fn parse(input: &str, current_position: usize) -> Option<(Self, usize)>
where
Self: Sized,
{
Expand All @@ -38,7 +38,7 @@ mod tests {

#[test]
fn parse() {
assert_eq!(Divider::parse("-----", 0, None), Some((Divider {}, 5)));
assert_eq!(Divider::parse("-----", 0), Some((Divider {}, 5)));
}

#[test]
Expand Down
14 changes: 5 additions & 9 deletions src/nodes/embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt::Display;

use serde::Serialize;

use crate::toolkit::{context::Context, parser::Parse};
use crate::toolkit::parser::Parse;

#[derive(Debug, PartialEq, Serialize, Clone)]
pub struct Embed {
Expand All @@ -26,7 +26,7 @@ impl Display for Embed {
}

impl Parse for Embed {
fn parse(input: &str, current_position: usize, _: Option<&Context>) -> Option<(Self, usize)>
fn parse(input: &str, current_position: usize) -> Option<(Self, usize)>
where
Self: Sized,
{
Expand Down Expand Up @@ -63,11 +63,7 @@ mod tests {
#[test]
fn parse() {
assert_eq!(
Embed::parse(
"{{youtube|https://www.youtube.com/embed/wsfdjlkjsdf}}",
0,
None
),
Embed::parse("{{youtube|https://www.youtube.com/embed/wsfdjlkjsdf}}", 0),
Some((
Embed::new("youtube", "https://www.youtube.com/embed/wsfdjlkjsdf",),
53
Expand All @@ -77,7 +73,7 @@ mod tests {

#[test]
fn failed_parse() {
assert_eq!(Embed::parse("{{youtube}}", 0, None), None);
assert_eq!(Embed::parse("{{youtube|", 0, None), None);
assert_eq!(Embed::parse("{{youtube}}", 0), None);
assert_eq!(Embed::parse("{{youtube|", 0), None);
}
}
20 changes: 8 additions & 12 deletions src/nodes/heading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use std::fmt::Display;

use serde::Serialize;

use crate::toolkit::{
context::Context,
parser::{parse_to_consumer, parse_to_parser, Branch, Consumer, Parse, Parser},
};
use crate::toolkit::parser::{parse_to_consumer, parse_to_parser, Branch, Consumer, Parse, Parser};

use super::{anchor::Anchor, text::Text};

Expand Down Expand Up @@ -72,7 +69,7 @@ impl Branch<HeadingNodes> for Heading {
}

impl Parse for Heading {
fn parse(input: &str, current_position: usize, _: Option<&Context>) -> Option<(Self, usize)> {
fn parse(input: &str, current_position: usize) -> Option<(Self, usize)> {
let start_tokens = ["# ", "## ", "### ", "#### ", "##### ", "###### "];

for start_token in start_tokens.iter() {
Expand All @@ -88,7 +85,6 @@ impl Parse for Heading {
&input[current_position + start_token.len()
..current_position + start_token.len() + end],
"",
None,
)
.expect("heading should always succeed"),
start_token.len() + end,
Expand Down Expand Up @@ -152,22 +148,22 @@ mod tests {
#[test]
fn from_string() {
assert_eq!(
Heading::parse("## Header", 0, None),
Heading::parse("## Header", 0),
Some((Heading::new(2, vec![Text::new("Header").into()]), 9))
);
assert_eq!(
Heading::parse("### Head", 0, None),
Heading::parse("### Head", 0),
Some((Heading::new(3, vec![Text::new("Head").into()]), 8))
);
assert_eq!(Heading::parse("not a header", 0, None), None);
assert_eq!(Heading::parse("######", 0, None), None);
assert_eq!(Heading::parse("######also not a header", 0, None), None);
assert_eq!(Heading::parse("not a header", 0), None);
assert_eq!(Heading::parse("######", 0), None);
assert_eq!(Heading::parse("######also not a header", 0), None);
}

#[test]
fn with_anchor() {
let str = "## hey [a](b)";
let h = Heading::parse(str, 0, None);
let h = Heading::parse(str, 0);
assert_eq!(
h,
Some((
Expand Down
10 changes: 5 additions & 5 deletions src/nodes/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt::Display;

use serde::Serialize;

use crate::toolkit::{context::Context, parser::Parse};
use crate::toolkit::parser::Parse;

use super::paragraph::Paragraph;

Expand Down Expand Up @@ -52,7 +52,7 @@ impl Display for Highlight {
}

impl Parse for Highlight {
fn parse(input: &str, current_position: usize, _: Option<&Context>) -> Option<(Self, usize)>
fn parse(input: &str, current_position: usize) -> Option<(Self, usize)>
where
Self: Sized,
{
Expand All @@ -79,7 +79,7 @@ impl Parse for Highlight {
input[start..current_position + 4 + end]
.split("\n\n")
.for_each(|node| {
let (node, _) = Paragraph::parse(node, 0, None)
let (node, _) = Paragraph::parse(node, 0)
.expect("Paragraph should never fail to parse");
nodes.push(node);
});
Expand Down Expand Up @@ -132,7 +132,7 @@ mod tests {
#[test]
fn parse() {
assert_eq!(
Highlight::parse(">>>\n>> h\n> i\nt\n\nt\n>>>", 0, None),
Highlight::parse(">>>\n>> h\n> i\nt\n\nt\n>>>", 0),
Some((
Highlight::new(
Some("h"),
Expand Down Expand Up @@ -162,7 +162,7 @@ test
test2
>>>";
let highlight = Highlight::parse(input, 0, None).unwrap();
let highlight = Highlight::parse(input, 0).unwrap();
assert_eq!(
highlight,
(
Expand Down
Loading

0 comments on commit d2cda6b

Please sign in to comment.