diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2016-02-15 13:22:10 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2016-03-11 15:15:02 +0100 |
commit | 1e17b10568124bb40ae38d59b3bdf7f43c71a8c0 (patch) | |
tree | 00337521c39cd1e02c4a7e32c97b4cde9a17fca1 /libimaglink | |
parent | 277c31237ab6c4bedc6d78f52a8f0fc453149978 (diff) |
Refactor external linking into trait
Diffstat (limited to 'libimaglink')
-rw-r--r-- | libimaglink/src/external.rs | 72 |
1 files changed, 43 insertions, 29 deletions
diff --git a/libimaglink/src/external.rs b/libimaglink/src/external.rs index 1aa0c1ed..faca38ac 100644 --- a/libimaglink/src/external.rs +++ b/libimaglink/src/external.rs @@ -7,44 +7,58 @@ use result::Result; use toml::Value; use toml::Table; -pub fn get_link(header: &EntryHeader) -> Result<Option<Link>> { - let uri = header.read("imag.content.uri"); +pub trait ExternalLinker { - if uri.is_err() { - let kind = LinkErrorKind::EntryHeaderReadError; - let lerr = LinkError::new(kind, Some(Box::new(uri.err().unwrap()))); - return Err(lerr); - } - let uri = uri.unwrap(); + /// get the external link from the implementor object + fn get_external_link(&self) -> Result<Option<Link>>; - match uri { - Some(Value::String(s)) => Ok(Some(Link::new(s))), - _ => Err(LinkError::new(LinkErrorKind::ExistingLinkTypeWrong, None)), - } + /// set the external link for the implementor object and return the current link from the entry, + /// if any. + fn set_external_link(&mut self, l: Link) -> Result<Option<Link>>; } -/// Set an external link in the header -/// -/// Return the previous set link if there was any -pub fn set_link(header: &mut EntryHeader, l: Link) -> Result<Option<Link>> { - let old_link = header.set("imag.content.uri", Value::String(l.into())); +impl ExternalLinker for EntryHeader { - if old_link.is_err() { - let kind = LinkErrorKind::EntryHeaderWriteError; - let lerr = LinkError::new(kind, Some(Box::new(old_link.err().unwrap()))); - return Err(lerr); - } - let old_link = old_link.unwrap(); + fn get_external_link(&self) -> Result<Option<Link>> { + let uri = self.read("imag.content.uri"); + + if uri.is_err() { + let kind = LinkErrorKind::EntryHeaderReadError; + let lerr = LinkError::new(kind, Some(Box::new(uri.err().unwrap()))); + return Err(lerr); + } + let uri = uri.unwrap(); - if old_link.is_none() { - return Ok(None); + match uri { + Some(Value::String(s)) => Ok(Some(Link::new(s))), + _ => Err(LinkError::new(LinkErrorKind::ExistingLinkTypeWrong, None)), + } } - match old_link.unwrap() { - Value::String(s) => Ok(Some(Link::new(s))), + /// Set an external link in the header + /// + /// Return the previous set link if there was any + fn set_external_link(&mut self, l: Link) -> Result<Option<Link>> { + let old_link = self.set("imag.content.uri", Value::String(l.into())); - // We don't do anything in this case and be glad we corrected the type error with this set() - _ => Ok(None), + if old_link.is_err() { + let kind = LinkErrorKind::EntryHeaderWriteError; + let lerr = LinkError::new(kind, Some(Box::new(old_link.err().unwrap()))); + return Err(lerr); + } + let old_link = old_link.unwrap(); + + if old_link.is_none() { + return Ok(None); + } + + match old_link.unwrap() { + Value::String(s) => Ok(Some(Link::new(s))), + + // We don't do anything in this case and be glad we corrected the type error with this set() + _ => Ok(None), + } } + } |