From 14189f92ba18a9c47791e98eb77a14b3466a22d8 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Mon, 2 May 2022 10:15:35 +0100 Subject: [PATCH] [TEM #2] - Remove TemplateArgsIter --- src/links.rs | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/src/links.rs b/src/links.rs index b7daadc..63ea4c7 100644 --- a/src/links.rs +++ b/src/links.rs @@ -116,7 +116,19 @@ impl<'a> Link<'a> { Some(LinkType::Template(PathBuf::from(file.trim()))) } (_, _, Some(file), Some(args)) => { - all_args.extend(extract_template_args(args.as_str()).collect::>()); + 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 = split_n.next().unwrap().trim(); + let value = split_n.next().unwrap(); + (key, value) + }) + .collect::>(); + all_args.extend(split_args); + Some(LinkType::Template(PathBuf::from(file.as_str()))) } (Some(mat), _, _, _) if mat.as_str().starts_with(ESCAPE_CHAR) => { @@ -196,26 +208,6 @@ 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 = (&'a str, &'a str); - - fn next(&mut self) -> Option { - if let Some(cap) = (&mut self.0).next() { - let mut split_args = cap.unwrap().get(0).unwrap().as_str().splitn(2, '='); - let key = split_args.next().unwrap().trim(); - let value = split_args.next().unwrap(); - return Some((key, value)); - } - None - } -} - -fn extract_template_args(contents: &str) -> TemplateArgsIter<'_> { - TemplateArgsIter(TEMPLATE_ARGS.captures_iter(contents)) -} - #[derive(PartialEq, Debug, Clone)] struct Args<'a> { start_index: usize,