From 4d3479291d73668e6ade66382c8dcc042afb8b3e Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 6 Dec 2015 11:25:13 +0100 Subject: [PATCH] Refactor custom functionality in helper function --- src/module/helpers/content.rs | 50 +++++++++++++++-------------------- 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/src/module/helpers/content.rs b/src/module/helpers/content.rs index a082763b..6dde2c54 100644 --- a/src/module/helpers/content.rs +++ b/src/module/helpers/content.rs @@ -16,43 +16,35 @@ mod markdown { } pub fn links(&self) -> Vec { - Parser::new(&self.text[..]) - .filter_map(|e| { - match e { - Event::Start(t) => Some(t), - Event::End(t) => Some(t), - _ => None - } - }) - .filter_map(|tag| { - match tag { - Tag::Link(url, text) => Some((url, text)), - _ => None - } - }) - .map(|(url, text)| { - text.into_owned() - }).collect::>() + self.extract_tag(|tag| { + match tag { + Tag::Link(url, _) => Some(url.into_owned()), + _ => None + } + }) } pub fn codeblocks(&self) -> Vec { + self.extract_tag(|tag| { + match tag { + Tag::CodeBlock(text) => Some(text.into_owned()), + _ => None + } + }) + } + + fn extract_tag(&self, f: F) -> Vec + where F: FnMut(Tag) -> Option + { Parser::new(&self.text[..]) .filter_map(|e| { match e { - Event::Start(t) => Some(t), - Event::End(t) => Some(t), - _ => None + Event::Start(t) | Event::End(t) => Some(t), + _ => None } }) - .filter_map(|tag| { - match tag { - Tag::CodeBlock(text) => Some(text), - _ => None - } - }) - .map(|text| { - text.into_owned() - }).collect::>() + .filter_map(f) + .collect::>() } }