From 12369ddadf0d0d4fa1618f8fdde6866a3a10c203 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 29 May 2016 11:48:37 +0200 Subject: [PATCH] Add into_urllink() translater helper --- libimagentrymarkdown/src/error.rs | 3 ++- libimagentrymarkdown/src/link.rs | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/libimagentrymarkdown/src/error.rs b/libimagentrymarkdown/src/error.rs index f3d81d1e..3e42e21b 100644 --- a/libimagentrymarkdown/src/error.rs +++ b/libimagentrymarkdown/src/error.rs @@ -1,6 +1,7 @@ generate_error_module!( generate_error_types!(MarkdownError, MarkdownErrorKind, - MarkdownRenderError => "Markdown render error" + MarkdownRenderError => "Markdown render error", + LinkParsingError => "Link parsing error" ); ); diff --git a/libimagentrymarkdown/src/link.rs b/libimagentrymarkdown/src/link.rs index 19914308..8ac3500c 100644 --- a/libimagentrymarkdown/src/link.rs +++ b/libimagentrymarkdown/src/link.rs @@ -1,8 +1,12 @@ +use error::MarkdownErrorKind as MEK; use result::Result; use hoedown::renderer::Render; use hoedown::Buffer; use hoedown::Markdown; +use url::Url; + +use libimagerror::into::IntoError; #[derive(Debug, Clone, PartialEq, Eq)] pub struct Link { @@ -10,6 +14,23 @@ pub struct Link { pub link: String, } +impl Link { + + /// Translate a `Link` into a `UrlLink` + fn into_urllink(self) -> Result { + Url::parse(&self.link[..]) + .map(move |link| UrlLink { title: self.title, link: link, }) + .map_err(Box::new) + .map_err(|e| MEK::LinkParsingError.into_error_with_cause(e)) + } + +} + +pub struct UrlLink { + pub title: String, + pub link: Url, +} + struct LinkExtractor { links: Vec, }