Refactor internal link helpers into trait
This commit is contained in:
parent
1e17b10568
commit
defec535c8
1 changed files with 41 additions and 19 deletions
|
@ -7,31 +7,53 @@ use result::Result;
|
||||||
|
|
||||||
use toml::Value;
|
use toml::Value;
|
||||||
|
|
||||||
pub fn get_links(header: &EntryHeader) -> Result<Links> {
|
pub trait InternalLinker {
|
||||||
process_rw_result(header.read("imag.links"))
|
|
||||||
|
/// Get the internal links from the implementor object
|
||||||
|
fn get_internal_links(&self) -> Result<Links>;
|
||||||
|
|
||||||
|
/// Set the internal links for the implementor object
|
||||||
|
fn set_internal_links(&mut self, links: Links) -> Result<Links>;
|
||||||
|
|
||||||
|
/// Add an internal link to the implementor object
|
||||||
|
fn add_internal_link(&mut self, link: Link) -> Result<()>;
|
||||||
|
|
||||||
|
/// Remove an internal link from the implementor object
|
||||||
|
fn remove_internal_link(&mut self, link: Link) -> Result<()>;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl InternalLinker for EntryHeader {
|
||||||
|
|
||||||
|
fn get_internal_links(self: &EntryHeader) -> Result<Links> {
|
||||||
|
process_rw_result(self.read("imag.links"))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set the links in a header and return the old links, if any.
|
/// Set the links in a header and return the old links, if any.
|
||||||
pub fn set_links(header: &mut EntryHeader, links: Links) -> Result<Links> {
|
fn set_internal_links(&mut self, links: Links) -> Result<Links> {
|
||||||
let links : Vec<Link> = links.into();
|
let links : Vec<Link> = links.into();
|
||||||
let links : Vec<Value> = links.into_iter().map(|link| Value::String(link.into())).collect();
|
let links : Vec<Value> = links.into_iter().map(|link| Value::String(link.into())).collect();
|
||||||
process_rw_result(header.set("imag.links", Value::Array(links)))
|
process_rw_result(self.set("imag.links", Value::Array(links)))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_link(header: &mut EntryHeader, link: Link) -> Result<()> {
|
fn add_internal_link(&mut self, link: Link) -> Result<()> {
|
||||||
get_links(header).and_then(|mut links| {
|
self.get_internal_links()
|
||||||
|
.and_then(|mut links| {
|
||||||
links.add(link);
|
links.add(link);
|
||||||
set_links(header, links).map(|_| ())
|
self.set_internal_links(links).map(|_| ())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn remove_link(header: &mut EntryHeader, link: Link) -> Result<()> {
|
fn remove_internal_link(&mut self, link: Link) -> Result<()> {
|
||||||
get_links(header).and_then(|mut links| {
|
self.get_internal_links()
|
||||||
|
.and_then(|mut links| {
|
||||||
links.remove(link);
|
links.remove(link);
|
||||||
set_links(header, links).map(|_| ())
|
self.set_internal_links(links).map(|_| ())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
fn process_rw_result(links: StoreResult<Option<Value>>) -> Result<Links> {
|
fn process_rw_result(links: StoreResult<Option<Value>>) -> Result<Links> {
|
||||||
if links.is_err() {
|
if links.is_err() {
|
||||||
let lerr = LinkError::new(LinkErrorKind::EntryHeaderReadError,
|
let lerr = LinkError::new(LinkErrorKind::EntryHeaderReadError,
|
||||||
|
|
Loading…
Reference in a new issue