diff --git a/src/nodes/image.rs b/src/nodes/image.rs index 58f3101..2b0bd7d 100644 --- a/src/nodes/image.rs +++ b/src/nodes/image.rs @@ -13,10 +13,10 @@ pub struct Image { } impl Image { - pub fn new>(consumed_all_input: bool, alt: S, url: S) -> Self { + pub fn new>(consumed_all_input: bool, alt: S, src: S) -> Self { Self { alt: alt.into(), - src: url.into(), + src: src.into(), consumed_all_input, } } diff --git a/src/nodes/yamd.rs b/src/nodes/yamd.rs index d279698..121ed3d 100644 --- a/src/nodes/yamd.rs +++ b/src/nodes/yamd.rs @@ -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); + } } diff --git a/src/toolkit/deserializer.rs b/src/toolkit/deserializer.rs index 073a1cd..66621fe 100644 --- a/src/toolkit/deserializer.rs +++ b/src/toolkit/deserializer.rs @@ -15,31 +15,15 @@ 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(); @@ -47,15 +31,20 @@ where } } } - 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 + where + Self: Node, + { + self.push(Self::get_fallback_node().map(|f| f(slice))?); + Some(self.len() - self.get_outer_token_length()) + } } pub type MaybeNode = Box) -> Option>;