Refactor external linking into trait

This commit is contained in:
Matthias Beyer 2016-02-15 13:22:10 +01:00
parent 277c31237a
commit 1e17b10568

View file

@ -7,8 +7,20 @@ 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 {
/// get the external link from the implementor object
fn get_external_link(&self) -> Result<Option<Link>>;
/// 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>>;
}
impl ExternalLinker for EntryHeader {
fn get_external_link(&self) -> Result<Option<Link>> {
let uri = self.read("imag.content.uri");
if uri.is_err() {
let kind = LinkErrorKind::EntryHeaderReadError;
@ -21,13 +33,13 @@ pub fn get_link(header: &EntryHeader) -> Result<Option<Link>> {
Some(Value::String(s)) => Ok(Some(Link::new(s))),
_ => Err(LinkError::new(LinkErrorKind::ExistingLinkTypeWrong, None)),
}
}
}
/// 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()));
/// 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()));
if old_link.is_err() {
let kind = LinkErrorKind::EntryHeaderWriteError;
@ -46,5 +58,7 @@ pub fn set_link(header: &mut EntryHeader, l: Link) -> Result<Option<Link>> {
// We don't do anything in this case and be glad we corrected the type error with this set()
_ => Ok(None),
}
}
}