From beda08be0a723e8f22491fe7bddbf29ea17efea5 Mon Sep 17 00:00:00 2001 From: Serhiy Barhamon Date: Sat, 14 Oct 2023 13:20:43 +0200 Subject: [PATCH] handle multiple fallbacks before non fallback node --- src/nodes/yamd.rs | 16 ++++++++++++++++ src/toolkit/deserializer.rs | 19 +++++++------------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/nodes/yamd.rs b/src/nodes/yamd.rs index 54ed751..121ed3d 100644 --- a/src/nodes/yamd.rs +++ b/src/nodes/yamd.rs @@ -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); + } } diff --git a/src/toolkit/deserializer.rs b/src/toolkit/deserializer.rs index c13c080..060b38a 100644 --- a/src/toolkit/deserializer.rs +++ b/src/toolkit/deserializer.rs @@ -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(); @@ -42,18 +43,12 @@ where Some(branch) } - fn fallback(self: &mut Self, slice: &str) -> Option + fn fallback(&mut self, slice: &str) -> Option 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()) } }