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,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 old_link.is_none() {
return Ok(None);
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();
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),
}
}
}