Skip to content

Commit

Permalink
handle multiple fallbacks before non fallback node
Browse files Browse the repository at this point in the history
  • Loading branch information
Lurk committed Oct 14, 2023
1 parent 3ab47ed commit beda08b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
16 changes: 16 additions & 0 deletions src/nodes/yamd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,4 +534,20 @@ end"#;
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);
}
}
19 changes: 7 additions & 12 deletions src/toolkit/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ where
}
for parser in &maybe_nodes {
if let Some(node) = parser(slice, branch.context()) {
if fallback_position != current_position - 1 {
branch.fallback(&input[fallback_position..current_position - 1])?;
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();
Expand All @@ -42,18 +43,12 @@ where
Some(branch)
}

fn fallback(self: &mut Self, slice: &str) -> Option<usize>
fn fallback(&mut self, slice: &str) -> Option<usize>
where
Self: Sized + Deserializer + Node,
Self: Node,
{
let fallback_node = Self::get_fallback_node();
match fallback_node.as_ref() {
Some(fallback_node) => {
self.push(fallback_node(slice));
Some(self.len() - self.get_outer_token_length())
}
None => return None,
}
self.push(Self::get_fallback_node().map(|f| f(slice))?);
Some(self.len() - self.get_outer_token_length())
}
}

Expand Down

0 comments on commit beda08b

Please sign in to comment.