Skip to content

Commit

Permalink
Merge pull request #32 from Lurk/multiple_fallbacks_in_a_row
Browse files Browse the repository at this point in the history
handle multiple fallbacks in a row
  • Loading branch information
Lurk authored Oct 14, 2023
2 parents 99b9178 + ad30a6e commit 8b486ab
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/nodes/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ pub struct Image {
}

impl Image {
pub fn new<S: Into<String>>(consumed_all_input: bool, alt: S, url: S) -> Self {
pub fn new<S: Into<String>>(consumed_all_input: bool, alt: S, src: S) -> Self {
Self {
alt: alt.into(),
src: url.into(),
src: src.into(),
consumed_all_input,
}
}
Expand Down
31 changes: 31 additions & 0 deletions src/nodes/yamd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,4 +519,35 @@ end"#;
fn default() {
assert_eq!(Yamd::default().to_string(), String::new());
}

#[test]
fn multiple_fallbacks_in_a_row() {
let input = "1\n\n2\n\n3";
let expected = Yamd::new_with_nodes(
None,
vec![
Paragraph::new_with_nodes(false, vec![Text::new("1").into()]).into(),
Paragraph::new_with_nodes(false, vec![Text::new("2").into()]).into(),
Paragraph::new_with_nodes(true, vec![Text::new("3").into()]).into(),
],
);
let actual = Yamd::deserialize(input).unwrap();
assert_eq!(expected, actual);
}

#[test]
fn multiple_fallbacks_in_a_row_before_non_fallback() {
let input = "1\n\n2\n\n3\n\n# header";
let expected = Yamd::new_with_nodes(
None,
vec![
Paragraph::new_with_nodes(false, vec![Text::new("1").into()]).into(),
Paragraph::new_with_nodes(false, vec![Text::new("2").into()]).into(),
Paragraph::new_with_nodes(false, vec![Text::new("3").into()]).into(),
Heading::new(true, "header", 1).into(),
],
);
let actual = Yamd::deserialize(input).unwrap();
assert_eq!(expected, actual);
}
}
37 changes: 13 additions & 24 deletions src/toolkit/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,36 @@ where
{
let mut current_position = 0;
let mut fallback_position = 0;
let fallback_node = Self::get_fallback_node();
let maybe_nodes = Self::get_maybe_nodes();
while current_position < input.len() {
let slice = &input[current_position..];
current_position += slice.chars().next().unwrap().len_utf8();
if maybe_nodes.is_empty() {
match fallback_node.as_ref() {
Some(fallback_node) => {
branch.push(fallback_node(slice));
current_position = branch.len() - branch.get_outer_token_length();
fallback_position = current_position;
}
None => return None,
}
continue;
}
for parser in &maybe_nodes {
if let Some(node) = parser(slice, branch.context()) {
if fallback_position != current_position - 1 {
match &fallback_node {
Some(fallback_node) => branch.push(fallback_node(
&input[fallback_position..current_position - 1],
)),
None => return None,
}
while fallback_position != current_position - 1 {
fallback_position =
branch.fallback(&input[fallback_position..current_position - 1])?;
}
branch.push(node);
current_position = branch.len() - branch.get_outer_token_length();
fallback_position = current_position;
}
}
}
if fallback_position < input.len() {
match fallback_node {
Some(fallback_node) => branch.push(fallback_node(&input[fallback_position..])),
None => return None,
}
while fallback_position < input.len() {
fallback_position = branch.fallback(&input[fallback_position..])?;
}

Some(branch)
}

fn fallback(&mut self, slice: &str) -> Option<usize>
where
Self: Node,
{
self.push(Self::get_fallback_node().map(|f| f(slice))?);
Some(self.len() - self.get_outer_token_length())
}
}

pub type MaybeNode<BranchNodes> = Box<dyn Fn(&str, Option<Context>) -> Option<BranchNodes>>;
Expand Down

0 comments on commit 8b486ab

Please sign in to comment.