From d405808a9ba4f276a99d5e0e0ac2cb81a782b14e Mon Sep 17 00:00:00 2001 From: sgoudham Date: Mon, 2 May 2022 03:11:35 +0100 Subject: [PATCH] [TEM #1] - Add TemplateArgsIter --- src/links.rs | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/src/links.rs b/src/links.rs index cf8ea1a..72599f2 100644 --- a/src/links.rs +++ b/src/links.rs @@ -91,8 +91,8 @@ impl<'a> Link<'a> { .as_str() .lines() .map(|line| { - let end_trimmed = line.trim_end_matches(LINE_BREAKS); - end_trimmed.trim_start_matches(LINE_BREAKS) + line.trim_end_matches(LINE_BREAKS) + .trim_start_matches(LINE_BREAKS) }) .collect::>(); @@ -117,19 +117,7 @@ impl<'a> Link<'a> { Some(LinkType::Template(PathBuf::from(file.trim()))) } (_, _, Some(file), Some(args)) => { - let matches = TEMPLATE_ARGS.captures_iter(args.as_str()); - - let split_args = matches - .into_iter() - .map(|mat| { - let mut split_n = mat.unwrap().get(0).unwrap().as_str().splitn(2, '='); - let key = format!("{{{}}}", split_n.next().unwrap().trim()); - let value = split_n.next().unwrap(); - (key, value) - }) - .collect::>(); - all_args.extend(split_args); - + all_args.extend(extract_template_args(args.as_str()).collect::>()); Some(LinkType::Template(PathBuf::from(file.as_str()))) } (Some(mat), _, _, _) if mat.as_str().starts_with(ESCAPE_CHAR) => { @@ -200,7 +188,8 @@ pub(crate) struct LinkIter<'a>(CaptureMatches<'a, 'a>); impl<'a> Iterator for LinkIter<'a> { type Item = Link<'a>; - fn next(&mut self) -> Option> { + + fn next(&mut self) -> Option { for cap in &mut self.0 { if let Some(inc) = Link::from_capture(cap.unwrap()) { return Some(inc); @@ -214,6 +203,26 @@ pub(crate) fn extract_template_links(contents: &str) -> LinkIter<'_> { LinkIter(TEMPLATE.captures_iter(contents)) } +struct TemplateArgsIter<'a>(CaptureMatches<'a, 'a>); + +impl<'a> Iterator for TemplateArgsIter<'a> { + type Item = (String, &'a str); + + fn next(&mut self) -> Option { + for mat in &mut self.0 { + let mut split_capt = mat.unwrap().get(0).unwrap().as_str().splitn(2, '='); + let key = format!("{{{}}}", split_capt.next().unwrap().trim()); + let value = split_capt.next().unwrap(); + return Some((key, value)); + } + None + } +} + +fn extract_template_args(contents: &str) -> TemplateArgsIter<'_> { + TemplateArgsIter(TEMPLATE_ARGS.captures_iter(contents)) +} + #[cfg(test)] mod link_tests { use std::collections::HashMap;