Refactor custom functionality in helper function

This commit is contained in:
Matthias Beyer 2015-12-06 11:25:13 +01:00
parent 7b99aee552
commit 4d3479291d

View file

@ -16,43 +16,35 @@ mod markdown {
} }
pub fn links(&self) -> Vec<String> { pub fn links(&self) -> Vec<String> {
Parser::new(&self.text[..]) self.extract_tag(|tag| {
.filter_map(|e| { match tag {
match e { Tag::Link(url, _) => Some(url.into_owned()),
Event::Start(t) => Some(t), _ => None
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::<Vec<String>>()
} }
pub fn codeblocks(&self) -> Vec<String> { pub fn codeblocks(&self) -> Vec<String> {
self.extract_tag(|tag| {
match tag {
Tag::CodeBlock(text) => Some(text.into_owned()),
_ => None
}
})
}
fn extract_tag<F>(&self, f: F) -> Vec<String>
where F: FnMut(Tag) -> Option<String>
{
Parser::new(&self.text[..]) Parser::new(&self.text[..])
.filter_map(|e| { .filter_map(|e| {
match e { match e {
Event::Start(t) => Some(t), Event::Start(t) | Event::End(t) => Some(t),
Event::End(t) => Some(t), _ => None
_ => None
} }
}) })
.filter_map(|tag| { .filter_map(f)
match tag { .collect::<Vec<String>>()
Tag::CodeBlock(text) => Some(text),
_ => None
}
})
.map(|text| {
text.into_owned()
}).collect::<Vec<String>>()
} }
} }