From 0014dfc21f89f018c4fca101fbcafbdfa96eab36 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Fri, 14 Jul 2023 16:18:04 -0400 Subject: [PATCH] Implement the radio link parser. The parser depends on the rematch_target parser which is not yet implemented. --- src/parser/radio_link.rs | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/parser/radio_link.rs b/src/parser/radio_link.rs index 2066f24..c375708 100644 --- a/src/parser/radio_link.rs +++ b/src/parser/radio_link.rs @@ -7,6 +7,8 @@ use nom::multi::many_till; use super::Context; use super::Object; +use crate::error::CustomError; +use crate::error::MyError; use crate::error::Res; use crate::parser::exiting::ExitClass; use crate::parser::object_parser::minimal_set_object; @@ -15,14 +17,43 @@ use crate::parser::parser_context::ExitMatcherNode; use crate::parser::parser_with_context::parser_with_context; use crate::parser::util::exit_matcher_parser; use crate::parser::util::get_consumed; -use crate::parser::util::not_yet_implemented; use crate::parser::RadioLink; use crate::parser::RadioTarget; #[tracing::instrument(ret, level = "debug")] pub fn radio_link<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, RadioLink<'s>> { - not_yet_implemented()?; - todo!(); + let radio_targets = context + .iter() + .filter_map(|context_element| match context_element.get_data() { + ContextElement::RadioTarget(targets) => Some(targets), + _ => None, + }) + .flatten(); + for radio_target in radio_targets { + let rematched_target = rematch_target(context, radio_target, input); + if let Ok((remaining, rematched_target)) = rematched_target { + let source = get_consumed(input, remaining); + return Ok(( + remaining, + RadioLink { + source, + children: rematched_target, + }, + )); + } + } + Err(nom::Err::Error(CustomError::MyError(MyError( + "NoRadioLink", + )))) +} + +#[tracing::instrument(ret, level = "debug")] +pub fn rematch_target<'r, 's>( + context: Context<'r, 's>, + target: &'r Vec>, + input: &'s str, +) -> Res<&'s str, Vec>> { + todo!() } #[tracing::instrument(ret, level = "debug")]