Change crates to use toml-query crate

This commit is contained in:
Matthias Beyer 2017-08-26 17:53:08 +02:00
parent 3024fefcb9
commit c0c62bd1b5
34 changed files with 121 additions and 83 deletions

View File

@ -23,6 +23,7 @@ itertools = "0.5"
tempfile = "2.1"
ansi_term = "0.9"
is-match = "0.1"
toml-query = "0.3.0"
libimagstore = { version = "0.4.0", path = "../../../lib/core/libimagstore" }
libimagerror = { version = "0.4.0", path = "../../../lib/core/libimagerror" }

View File

@ -133,7 +133,8 @@ impl Configuration {
use self::error::ConfigErrorKind as CEK;
use self::error::MapErrInto;
use libimagerror::into::IntoError;
use libimagstore::toml_ext::TomlValueExt;
use toml_query::read::TomlValueReadExt;
v.into_iter()
.map(|s| { debug!("Trying to process '{}'", s); s })
@ -170,10 +171,10 @@ impl Configuration {
/// Returns None if string cannot be converted.
///
/// Arrays and Tables are not supported and will yield `None`.
fn into_value(value: Value, s: String) -> Option<Value> {
fn into_value(value: &Value, s: String) -> Option<Value> {
use std::str::FromStr;
match value {
match *value {
Value::String(_) => Some(Value::String(s)),
Value::Integer(_) => FromStr::from_str(&s[..]).ok().map(Value::Integer),
Value::Float(_) => FromStr::from_str(&s[..]).ok().map(Value::Float),

View File

@ -42,6 +42,7 @@ extern crate ansi_term;
extern crate clap;
extern crate toml;
extern crate toml_query;
#[macro_use] extern crate is_match;
extern crate libimagstore;

View File

@ -17,6 +17,7 @@ homepage = "http://imag-pim.org"
semver = "0.5"
log = "0.3"
toml = "^0.4"
toml-query = "0.3.0"
libimagstore = { version = "0.4.0", path = "../../../lib/core/libimagstore" }
libimagerror = { version = "0.4.0", path = "../../../lib/core/libimagerror" }

View File

@ -36,6 +36,7 @@
#[macro_use] extern crate log;
extern crate semver;
extern crate toml;
extern crate toml_query;
extern crate libimagrt;
#[macro_use] extern crate libimagstore;

View File

@ -30,7 +30,9 @@ use libimagstore::storeid::StoreId;
use libimagstore::storeid::StoreIdIterator;
use libimagstore::store::FileLockEntry;
use libimagstore::store::Store;
use libimagstore::toml_ext::TomlValueExt;
use toml_query::read::TomlValueReadExt;
use toml_query::set::TomlValueSetExt;
use module_path::ModuleEntryPath;
use result::Result;
@ -92,7 +94,7 @@ impl<'a> Note<'a> {
pub fn get_name(&self) -> Result<String> {
let header = self.entry.get_header();
match header.read("note.name") {
Ok(Some(Value::String(s))) => Ok(String::from(s)),
Ok(Some(&Value::String(ref s))) => Ok(s.clone()),
Ok(_) => {
let e = NE::new(NEK::HeaderTypeError, None);
Err(NE::new(NEK::StoreReadError, Some(Box::new(e))))

View File

@ -13,6 +13,7 @@ license = "LGPL-2.1"
uuid = { version = "0.3.1", features = ["v4"] }
lazy_static = "0.1.15"
toml = "^0.4"
toml-query = "0.3.0"
libimagstore = { version = "0.4.0", path = "../../../lib/core/libimagstore" }
libimagerror = { version = "0.4.0", path = "../../../lib/core/libimagerror" }

View File

@ -24,10 +24,12 @@ use toml::Value;
use libimagstore::store::Entry;
use libimagstore::store::FileLockEntry;
use libimagstore::store::Store;
use libimagstore::toml_ext::TomlValueExt;
use libimagentrylink::internal::InternalLinker;
use libimagerror::into::IntoError;
use toml_query::read::TomlValueReadExt;
use toml_query::insert::TomlValueInsertExt;
use result::Result;
use error::AnnotationErrorKind as AEK;
use error::MapErrInto;
@ -53,14 +55,8 @@ impl Annotateable for Entry {
.and_then(|mut anno| {
anno.get_header_mut()
.insert("annotation.is_annotation", Value::Boolean(true))
.map_err_into(AEK::StoreWriteError)
.and_then(|succeeded| {
if succeeded {
Ok(anno)
} else {
Err(AEK::HeaderWriteError.into_error())
}
})
.map_err_into(AEK::HeaderWriteError)
.map(|_| anno)
})
.and_then(|mut anno| {
anno.add_internal_link(self)
@ -74,9 +70,9 @@ impl Annotateable for Entry {
.read("annotation.is_annotation")
.map_err_into(AEK::StoreReadError)
.and_then(|res| match res {
Some(Value::Boolean(b)) => Ok(b),
None => Ok(false),
_ => Err(AEK::HeaderTypeError.into_error()),
Some(&Value::Boolean(b)) => Ok(b),
None => Ok(false),
_ => Err(AEK::HeaderTypeError.into_error()),
})
}

View File

@ -68,7 +68,8 @@ impl<'a> AnnotationFetcher<'a> for Store {
pub mod iter {
use toml::Value;
use libimagstore::toml_ext::TomlValueExt;
use toml_query::read::TomlValueReadExt;
use libimagerror::into::IntoError;
use libimagnotes::note::Note;
use libimagnotes::note::NoteIterator;
@ -95,10 +96,9 @@ pub mod iter {
loop {
match self.0.next() {
Some(Ok(note)) => {
let hdr = note.get_header().read("annotation.is_annotation");
match hdr {
match note.get_header().read("annotation.is_annotation") {
Ok(None) => continue, // not an annotation
Ok(Some(Value::Boolean(true))) => return Some(Ok(note)),
Ok(Some(&Value::Boolean(true))) => return Some(Ok(note)),
Ok(Some(_)) => return Some(Err(AEK::HeaderTypeError.into_error())),
Err(e) => return Some(Err(e).map_err_into(AEK::HeaderReadError)),
}

View File

@ -19,6 +19,7 @@
extern crate uuid;
extern crate toml;
extern crate toml_query;
#[macro_use] extern crate libimagerror;
#[macro_use] extern crate libimagstore;

View File

@ -21,6 +21,7 @@ log = "0.3"
regex = "0.2"
semver = "0.5.*"
toml = "^0.4"
toml-query = "0.3.0"
libimagstore = { version = "0.4.0", path = "../../../lib/core/libimagstore" }
libimagentrytag = { version = "0.4.0", path = "../../../lib/entry/libimagentrytag" }

View File

@ -32,8 +32,8 @@ struct EqPred {
impl Predicate for EqPred {
fn evaluate(&self, v: Value) -> bool {
self.expected == v
fn evaluate(&self, v: &Value) -> bool {
self.expected == *v
}
}

View File

@ -18,10 +18,11 @@
//
use libimagstore::store::Entry;
use libimagstore::toml_ext::TomlValueExt;
use toml_query::read::TomlValueReadExt;
use filters::filter::Filter;
use builtin::header::field_path::FieldPath;
use filters::filter::Filter;
pub struct FieldExists {
header_field_path: FieldPath,

View File

@ -33,10 +33,10 @@ struct EqGrep{
impl Predicate for EqGrep {
fn evaluate(&self, v: Value) -> bool {
match v {
Value::String(s) => self.regex.captures(&s[..]).is_some(),
_ => false,
fn evaluate(&self, v: &Value) -> bool {
match *v {
Value::String(ref s) => self.regex.captures(&s[..]).is_some(),
_ => false,
}
}

View File

@ -32,17 +32,17 @@ struct EqGt {
impl Predicate for EqGt {
fn evaluate(&self, v: Value) -> bool {
fn evaluate(&self, v: &Value) -> bool {
match self.comp {
Value::Integer(i) => {
match v {
match *v {
Value::Integer(j) => i > j,
Value::Float(f) => (i as f64) > f,
_ => false,
}
},
Value::Float(f) => {
match v {
match *v {
Value::Integer(i) => f > (i as f64),
Value::Float(d) => f > d,
_ => false,

View File

@ -18,7 +18,7 @@
//
use libimagstore::store::Entry;
use libimagstore::toml_ext::TomlValueExt;
use toml_query::read::TomlValueReadExt;
use builtin::header::field_path::FieldPath;
use filters::filter::Filter;
@ -46,12 +46,12 @@ impl Filter<Entry> for FieldIsEmpty {
.read(&self.header_field_path[..])
.map(|v| {
match v {
Some(Value::Array(a)) => a.is_empty(),
Some(Value::String(s)) => s.is_empty(),
Some(Value::Table(t)) => t.is_empty(),
Some(Value::Boolean(_)) |
Some(Value::Float(_)) |
Some(Value::Integer(_)) => false,
Some(&Value::Array(ref a)) => a.is_empty(),
Some(&Value::String(ref s)) => s.is_empty(),
Some(&Value::Table(ref t)) => t.is_empty(),
Some(&Value::Boolean(_)) |
Some(&Value::Float(_)) |
Some(&Value::Integer(_)) => false,
_ => true,
}
})

View File

@ -58,8 +58,8 @@ struct IsTypePred {
impl Predicate for IsTypePred {
fn evaluate(&self, v: Value) -> bool {
self.ty.matches(&v)
fn evaluate(&self, v: &Value) -> bool {
self.ty.matches(v)
}
}

View File

@ -32,17 +32,17 @@ struct EqLt {
impl Predicate for EqLt {
fn evaluate(&self, v: Value) -> bool {
fn evaluate(&self, v: &Value) -> bool {
match self.comp {
Value::Integer(i) => {
match v {
match *v {
Value::Integer(j) => i < j,
Value::Float(f) => (i as f64) < f,
_ => false,
}
},
Value::Float(f) => {
match v {
match *v {
Value::Integer(i) => f < (i as f64),
Value::Float(d) => f < d,
_ => false,

View File

@ -18,7 +18,8 @@
//
use libimagstore::store::Entry;
use libimagstore::toml_ext::TomlValueExt;
use toml_query::read::TomlValueReadExt;
use builtin::header::field_path::FieldPath;
use filters::filter::Filter;
@ -26,7 +27,7 @@ use filters::filter::Filter;
use toml::Value;
pub trait Predicate {
fn evaluate(&self, Value) -> bool;
fn evaluate(&self, &Value) -> bool;
}
/// Check whether certain header field in a entry is equal to a value

View File

@ -21,8 +21,8 @@ use semver::Version;
use toml::Value;
use libimagstore::store::Entry;
use libimagstore::toml_ext::TomlValueExt;
use toml_query::read::TomlValueReadExt;
use filters::filter::Filter;
pub struct VersionEq {
@ -44,8 +44,8 @@ impl Filter<Entry> for VersionEq {
.read("imag.version")
.map(|val| {
val.map_or(false, |v| {
match v {
Value::String(s) => {
match *v {
Value::String(ref s) => {
match Version::parse(&s[..]) {
Ok(v) => v == self.version,
_ => false

View File

@ -21,8 +21,8 @@ use semver::Version;
use toml::Value;
use libimagstore::store::Entry;
use libimagstore::toml_ext::TomlValueExt;
use toml_query::read::TomlValueReadExt;
use filters::filter::Filter;
pub struct VersionGt {
@ -44,8 +44,8 @@ impl Filter<Entry> for VersionGt {
.read("imag.version")
.map(|val| {
val.map_or(false, |v| {
match v {
Value::String(s) => {
match *v {
Value::String(ref s) => {
match Version::parse(&s[..]) {
Ok(v) => v > self.version,
_ => false

View File

@ -21,8 +21,8 @@ use semver::Version;
use toml::Value;
use libimagstore::store::Entry;
use libimagstore::toml_ext::TomlValueExt;
use toml_query::read::TomlValueReadExt;
use filters::filter::Filter;
pub struct VersionLt {
@ -44,8 +44,8 @@ impl Filter<Entry> for VersionLt {
.read("imag.version")
.map(|val| {
val.map_or(false, |v| {
match v {
Value::String(s) => {
match *v {
Value::String(ref s) => {
match Version::parse(&s[..]) {
Ok(v) => v < self.version,
_ => false

View File

@ -37,6 +37,7 @@ extern crate itertools;
extern crate regex;
extern crate semver;
extern crate toml;
extern crate toml_query;
extern crate libimagstore;
extern crate libimagentrytag;

View File

@ -22,6 +22,7 @@ url = "1.2"
rust-crypto = "0.2"
env_logger = "0.3"
is-match = "0.1"
toml-query = "0.3.0"
libimagstore = { version = "0.4.0", path = "../../../lib/core/libimagstore" }
libimagerror = { version = "0.4.0", path = "../../../lib/core/libimagerror" }

View File

@ -39,9 +39,11 @@ use libimagstore::store::FileLockEntry;
use libimagstore::store::Store;
use libimagstore::storeid::StoreId;
use libimagstore::storeid::IntoStoreId;
use libimagstore::toml_ext::TomlValueExt;
use libimagutil::debug_result::*;
use toml_query::read::TomlValueReadExt;
use toml_query::set::TomlValueSetExt;
use error::LinkError as LE;
use error::LinkErrorKind as LEK;
use error::MapErrInto;
@ -73,7 +75,7 @@ impl<'a> Link<'a> {
.read("imag.content.url")
.ok()
.and_then(|opt| match opt {
Some(Value::String(s)) => {
Some(&Value::String(ref s)) => {
debug!("Found url, parsing: {:?}", s);
Url::parse(&s[..]).ok()
},
@ -87,7 +89,7 @@ impl<'a> Link<'a> {
.read("imag.content.url");
match opt {
Ok(Some(Value::String(s))) => {
Ok(Some(&Value::String(ref s))) => {
Url::parse(&s[..])
.map(Some)
.map_err(|e| LE::new(LEK::EntryHeaderReadError, Some(Box::new(e))))
@ -351,7 +353,7 @@ impl ExternalLinker for Entry {
let mut hdr = file.deref_mut().get_header_mut();
let mut table = match hdr.read("imag.content") {
Ok(Some(Value::Table(table))) => table,
Ok(Some(&Value::Table(ref table))) => table.clone(),
Ok(Some(_)) => {
warn!("There is a value at 'imag.content' which is not a table.");
warn!("Going to override this value");

View File

@ -25,9 +25,11 @@ use libimagstore::storeid::StoreId;
use libimagstore::storeid::IntoStoreId;
use libimagstore::store::Entry;
use libimagstore::store::Result as StoreResult;
use libimagstore::toml_ext::TomlValueExt;
use libimagerror::into::IntoError;
use toml_query::read::TomlValueReadExt;
use toml_query::set::TomlValueSetExt;
use error::LinkErrorKind as LEK;
use error::MapErrInto;
use result::Result;
@ -388,7 +390,12 @@ pub mod iter {
impl InternalLinker for Entry {
fn get_internal_links(&self) -> Result<LinkIter> {
process_rw_result(self.get_header().read("imag.links"))
let res = self
.get_header()
.read("imag.links")
.map_err_into(LEK::EntryHeaderReadError)
.map(|r| r.cloned());
process_rw_result(res)
}
/// Set the links in a header and return the old links, if any.
@ -417,7 +424,11 @@ impl InternalLinker for Entry {
})
})
}));
process_rw_result(self.get_header_mut().set("imag.links", Value::Array(new_links)))
let res = self
.get_header_mut()
.set("imag.links", Value::Array(new_links))
.map_err_into(LEK::EntryHeaderReadError);
process_rw_result(res)
}
fn add_internal_link(&mut self, link: &mut Entry) -> Result<()> {
@ -485,7 +496,9 @@ fn rewrite_links<I: Iterator<Item = Link>>(header: &mut Value, links: I) -> Resu
}));
debug!("Setting new link array: {:?}", links);
let process = header.set("imag.links", Value::Array(links));
let process = header
.set("imag.links", Value::Array(links))
.map_err_into(LEK::EntryHeaderReadError);
process_rw_result(process).map(|_| ())
}
@ -509,12 +522,17 @@ fn add_foreign_link(target: &mut Entry, from: StoreId) -> Result<()> {
})
}));
debug!("Setting links in {:?}: {:?}", target.get_location(), links);
process_rw_result(target.get_header_mut().set("imag.links", Value::Array(links)))
.map(|_| ())
let res = target
.get_header_mut()
.set("imag.links", Value::Array(links))
.map_err_into(LEK::EntryHeaderReadError);
process_rw_result(res).map(|_| ())
})
}
fn process_rw_result(links: StoreResult<Option<Value>>) -> Result<LinkIter> {
fn process_rw_result(links: Result<Option<Value>>) -> Result<LinkIter> {
use std::path::PathBuf;
let links = match links {

View File

@ -34,6 +34,7 @@
extern crate itertools;
#[macro_use] extern crate log;
extern crate toml;
extern crate toml_query;
extern crate semver;
extern crate url;
extern crate crypto;

View File

@ -21,6 +21,7 @@ semver = "0.5"
toml = "^0.4"
version = "2.0.1"
walkdir = "1.0.*"
toml-query = "0.3.0"
libimagstore = { version = "0.4.0", path = "../../../lib/core/libimagstore" }
libimagerror = { version = "0.4.0", path = "../../../lib/core/libimagerror" }

View File

@ -37,13 +37,13 @@ impl RefFlags {
/// It assumes that this is a Map with Key = <name of the setting> and Value = boolean.
pub fn read(v: &Value) -> Result<RefFlags> {
fn get_field(v: &Value, key: &str) -> Result<bool> {
use libimagstore::toml_ext::TomlValueExt;
use toml_query::read::TomlValueReadExt;
use error::MapErrInto;
v.read(key)
.map_err_into(REK::HeaderTomlError)
.and_then(|toml| match toml {
Some(Value::Boolean(b)) => Ok(b),
Some(&Value::Boolean(b)) => Ok(b),
Some(_) => Err(REK::HeaderTypeError.into()),
None => Err(REK::HeaderFieldMissingError.into()),
})

View File

@ -36,6 +36,7 @@ extern crate crypto;
extern crate itertools;
extern crate semver;
extern crate toml;
extern crate toml_query;
extern crate version;
extern crate walkdir;

View File

@ -33,10 +33,12 @@ use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreId;
use libimagstore::storeid::IntoStoreId;
use libimagstore::store::Store;
use libimagstore::toml_ext::TomlValueExt;
use libimagerror::into::IntoError;
use toml::Value;
use toml_query::read::TomlValueReadExt;
use toml_query::set::TomlValueSetExt;
use toml_query::insert::TomlValueInsertExt;
use error::RefErrorKind as REK;
use error::MapErrInto;
@ -89,7 +91,7 @@ impl<'a> Ref<'a> {
fn read_reference(fle: &FileLockEntry<'a>) -> Result<PathBuf> {
match fle.get_header().read("ref.path") {
Ok(Some(Value::String(s))) => Ok(PathBuf::from(s)),
Ok(Some(&Value::String(ref s))) => Ok(PathBuf::from(s)),
Ok(Some(_)) => Err(REK::HeaderTypeError.into_error()),
Ok(None) => Err(REK::HeaderFieldMissingError.into_error()),
Err(e) => Err(REK::StoreReadError.into_error_with_cause(Box::new(e))),
@ -204,18 +206,17 @@ impl<'a> Ref<'a> {
match tpl {
&Some((ref s, ref v)) => {
match fle.get_header_mut().insert(s, v.clone()) {
Ok(false) => {
let e = REK::HeaderFieldAlreadyExistsError.into_error();
let e = Box::new(e);
let e = REK::HeaderFieldWriteError.into_error_with_cause(e);
return Err(e);
Ok(None) => {
debug!("Header insert worked");
}
Ok(Some(val)) => {
debug!("Overwrote: {}, which was: {:?}", s, val);
},
Err(e) => {
let e = Box::new(e);
let e = REK::HeaderFieldWriteError.into_error_with_cause(e);
return Err(e);
},
_ => (),
}
}
&None => {
@ -274,7 +275,7 @@ impl<'a> Ref<'a> {
pub fn get_stored_hash_with_hasher<H: Hasher>(&self, h: &H) -> Result<String> {
match self.0.get_header().read(&format!("ref.content_hash.{}", h.hash_name())[..]) {
// content hash stored...
Ok(Some(Value::String(s))) => Ok(s),
Ok(Some(&Value::String(ref s))) => Ok(s.clone()),
// content hash header field has wrong type
Ok(Some(_)) => Err(REK::HeaderTypeError.into_error()),
@ -365,7 +366,7 @@ impl<'a> Ref<'a> {
.map_err(|e| REK::HeaderFieldReadError.into_error_with_cause(e))
.and_then(|ro| {
match ro {
Some(Value::Boolean(b)) => Ok(b),
Some(&Value::Boolean(b)) => Ok(b),
Some(_) => Err(REK::HeaderTypeError.into_error()),
None => Err(REK::HeaderFieldMissingError.into_error()),
}
@ -414,7 +415,7 @@ impl<'a> Ref<'a> {
/// Get the path of the file which is reffered to by this Ref
pub fn fs_file(&self) -> Result<PathBuf> {
match self.0.get_header().read("ref.path") {
Ok(Some(Value::String(ref s))) => Ok(PathBuf::from(s)),
Ok(Some(&Value::String(ref s))) => Ok(PathBuf::from(s)),
Ok(Some(_)) => Err(REK::HeaderTypeError.into_error()),
Ok(None) => Err(REK::HeaderFieldMissingError.into_error()),
Err(e) => Err(REK::StoreReadError.into_error_with_cause(Box::new(e))),

View File

@ -21,6 +21,7 @@ toml = "^0.4"
itertools = "0.5"
is-match = "0.1"
filters = "0.1"
toml-query = "0.3.0"
libimagstore = { version = "0.4.0", path = "../../../lib/core/libimagstore" }
libimagerror = { version = "0.4.0", path = "../../../lib/core/libimagerror" }

View File

@ -36,6 +36,7 @@ extern crate itertools;
#[macro_use] extern crate log;
extern crate regex;
extern crate toml;
extern crate toml_query;
#[macro_use] extern crate is_match;
extern crate filters;

View File

@ -21,7 +21,9 @@ use itertools::Itertools;
use libimagstore::store::Entry;
use libimagerror::into::IntoError;
use libimagstore::toml_ext::TomlValueExt;
use toml_query::read::TomlValueReadExt;
use toml_query::set::TomlValueSetExt;
use error::TagErrorKind;
use error::MapErrInto;
@ -50,7 +52,7 @@ impl Tagable for Value {
let tags = try!(self.read("imag.tags").map_err_into(TagErrorKind::HeaderReadError));
match tags {
Some(Value::Array(tags)) => {
Some(&Value::Array(ref tags)) => {
if !tags.iter().all(|t| is_match!(*t, Value::String(_))) {
return Err(TagErrorKind::TagTypeError.into());
}
@ -120,7 +122,7 @@ impl Tagable for Value {
fn has_tag(&self, t: TagSlice) -> Result<bool> {
let tags = try!(self.read("imag.tags").map_err_into(TagErrorKind::HeaderReadError));
if !tags.iter().all(|t| is_match!(*t, Value::String(_))) {
if !tags.iter().all(|t| is_match!(*t, &Value::String(_))) {
return Err(TagErrorKind::TagTypeError.into());
}
@ -128,7 +130,7 @@ impl Tagable for Value {
.iter()
.any(|tag| {
match *tag {
Value::String(ref s) => { s == t },
&Value::String(ref s) => { s == t },
_ => unreachable!()
}
}))