use std::collections::BTreeMap;
use toml::Value;
use error::RefErrorKind as REK;
use result::Result;
pub struct RefFlags {
content_hashing: bool,
permission_tracking: bool,
}
impl RefFlags {
pub fn read(v: &Value) -> Result<RefFlags> {
fn get_field(v: &Value, key: &str) -> Result<bool> {
use libimagstore::toml_ext::TomlValueExt;
use error::MapErrInto;
v.read(key)
.map_err_into(REK::HeaderTomlError)
.and_then(|toml| match toml {
Some(Value::Boolean(b)) => Ok(b),
Some(_) => Err(REK::HeaderTypeError.into()),
None => Err(REK::HeaderFieldMissingError.into()),
})
}
Ok(RefFlags {
content_hashing: try!(get_field(v, "ref.flags.content_hashing")),
permission_tracking: try!(get_field(v, "ref.flags.permission_tracking")),
})
}
pub fn is_often_moving(self, b: bool) -> RefFlags {
self.with_content_hashing(b)
}
pub fn with_content_hashing(mut self, b: bool) -> RefFlags {
self.content_hashing = b;
self
}
pub fn with_permission_tracking(mut self, b: bool) -> RefFlags {
self.permission_tracking = b;
self
}
pub fn get_content_hashing(&self) -> bool {
self.content_hashing
}
pub fn get_permission_tracking(&self) -> bool {
self.permission_tracking
}
}
impl Into<Value> for RefFlags {
fn into(self) -> Value {
let mut btm = BTreeMap::new();
btm.insert(String::from("content_hashing"), Value::Boolean(self.content_hashing));
btm.insert(String::from("permission_tracking"), Value::Boolean(self.permission_tracking));
return Value::Table(btm)
}
}
impl Default for RefFlags {
fn default() -> RefFlags {
RefFlags {
content_hashing: false,
permission_tracking: false,
}
}
}