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::Value;
use toml::Table; use toml::Table;
pub fn get_link(header: &EntryHeader) -> Result<Option<Link>> { pub trait ExternalLinker {
let uri = header.read("imag.content.uri");
/// 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() { if uri.is_err() {
let kind = LinkErrorKind::EntryHeaderReadError; let kind = LinkErrorKind::EntryHeaderReadError;
@ -26,8 +38,8 @@ pub fn get_link(header: &EntryHeader) -> Result<Option<Link>> {
/// Set an external link in the header /// Set an external link in the header
/// ///
/// Return the previous set link if there was any /// Return the previous set link if there was any
pub fn set_link(header: &mut EntryHeader, l: Link) -> Result<Option<Link>> { fn set_external_link(&mut self, l: Link) -> Result<Option<Link>> {
let old_link = header.set("imag.content.uri", Value::String(l.into())); let old_link = self.set("imag.content.uri", Value::String(l.into()));
if old_link.is_err() { if old_link.is_err() {
let kind = LinkErrorKind::EntryHeaderWriteError; let kind = LinkErrorKind::EntryHeaderWriteError;
@ -48,3 +60,5 @@ pub fn set_link(header: &mut EntryHeader, l: Link) -> Result<Option<Link>> {
} }
} }
}