Skip to content

Commit

Permalink
handle multiple fallbacks in a row
Browse files Browse the repository at this point in the history
  • Loading branch information
Lurk committed Oct 14, 2023
1 parent 99b9178 commit 3ab47ed
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 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
15 changes: 15 additions & 0 deletions src/nodes/yamd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,4 +519,19 @@ 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);
}
}
39 changes: 19 additions & 20 deletions src/toolkit/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,46 @@ 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,
}
current_position = branch.fallback(slice)?;
fallback_position = current_position;
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,
}
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(self: &mut Self, slice: &str) -> Option<usize>
where
Self: Sized + Deserializer + 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,
}
}
}

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

0 comments on commit 3ab47ed

Please sign in to comment.