imag/lib/entry/libimagentryref/src/flags.rs

108 lines
3.2 KiB
Rust
Raw Normal View History

//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; version
// 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
2016-06-09 14:49:46 +00:00
use std::collections::BTreeMap;
use toml::Value;
use error::RefErrorKind as REK;
use error::ResultExt;
use error::Result;
2016-06-09 14:49:46 +00:00
pub struct RefFlags {
content_hashing: bool,
permission_tracking: bool,
}
impl RefFlags {
/// Read the RefFlags from a TOML document
///
/// Assumes that the whole TOML tree is passed. So this looks up `ref.flags` to get the flags.
/// It assumes that this is a Map with Key = <name of the setting> and Value = boolean.
pub fn read(v: &Value) -> Result<RefFlags> {
2016-06-09 17:31:12 +00:00
fn get_field(v: &Value, key: &str) -> Result<bool> {
2017-08-26 15:53:08 +00:00
use toml_query::read::TomlValueReadExt;
2017-05-03 18:27:21 +00:00
v.read(key)
.chain_err(|| REK::HeaderTomlError)
2017-05-03 18:27:21 +00:00
.and_then(|toml| match toml {
2017-08-26 15:53:08 +00:00
Some(&Value::Boolean(b)) => Ok(b),
2017-05-03 18:27:21 +00:00
Some(_) => Err(REK::HeaderTypeError.into()),
None => Err(REK::HeaderFieldMissingError.into()),
})
2016-06-09 17:31:12 +00:00
}
Ok(RefFlags {
content_hashing: try!(get_field(v, "ref.flags.content_hashing")),
permission_tracking: try!(get_field(v, "ref.flags.permission_tracking")),
})
2016-06-09 14:49:46 +00:00
}
/// Alias for `RefFlags::content_hashing()`
pub fn is_often_moving(self, b: bool) -> RefFlags {
2016-06-09 14:49:46 +00:00
self.with_content_hashing(b)
}
2016-06-09 17:42:08 +00:00
pub fn with_content_hashing(mut self, b: bool) -> RefFlags {
self.content_hashing = b;
self
2016-06-09 14:49:46 +00:00
}
pub fn with_permission_tracking(mut self, b: bool) -> RefFlags {
2016-06-09 14:49:46 +00:00
self.permission_tracking = b;
self
}
pub fn get_content_hashing(&self) -> bool {
2016-06-09 17:43:24 +00:00
self.content_hashing
2016-06-09 14:49:46 +00:00
}
pub fn get_permission_tracking(&self) -> bool {
self.permission_tracking
2016-06-09 14:49:46 +00:00
}
}
impl Into<Value> for RefFlags {
/// Build a TOML::Value from this RefFlags object.
///
/// Returns a Map which should be set in `ref.flags` in the header.
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)
}
}
2016-06-26 09:35:16 +00:00
impl Default for RefFlags {
fn default() -> RefFlags {
RefFlags {
content_hashing: false,
permission_tracking: false,
}
}
}