From eacc6777b8e1dbead63eb3ba67385b25e342886e Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 30 May 2017 21:00:29 +0200 Subject: [PATCH] Fix libimagtodo for new toml-query interface --- libimagtodo/Cargo.toml | 2 ++ libimagtodo/src/lib.rs | 2 ++ libimagtodo/src/task.rs | 30 ++++++++++++++++++++++++------ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/libimagtodo/Cargo.toml b/libimagtodo/Cargo.toml index 15e08932..2f7dc087 100644 --- a/libimagtodo/Cargo.toml +++ b/libimagtodo/Cargo.toml @@ -18,6 +18,8 @@ semver = "0.2" task-hookrs = "0.2.2" uuid = "0.3" toml = "0.4.*" +toml-query = "0.1.*" +is-match = "0.1.*" log = "0.3" serde_json = "0.8" diff --git a/libimagtodo/src/lib.rs b/libimagtodo/src/lib.rs index 5a7584ec..16fcc11b 100644 --- a/libimagtodo/src/lib.rs +++ b/libimagtodo/src/lib.rs @@ -34,6 +34,8 @@ extern crate semver; extern crate uuid; extern crate toml; +extern crate toml_query; +#[macro_use] extern crate is_match; #[macro_use] extern crate log; extern crate serde_json; diff --git a/libimagtodo/src/task.rs b/libimagtodo/src/task.rs index d51d0407..1f826fb0 100644 --- a/libimagtodo/src/task.rs +++ b/libimagtodo/src/task.rs @@ -224,6 +224,17 @@ pub trait IntoTask<'a> { impl<'a> IntoTask<'a> for TTask { fn into_task(self, store : &'a Store) -> Result> { + use toml_query::read::TomlValueReadExt; + use toml_query::set::TomlValueSetExt; + use libimagerror::into::IntoError; + + // Helper for toml_query::read::TomlValueReadExt::read() return value, which does only + // return Result instead of Result>, which is a real inconvenience. + // + let no_identifier = |e: &::toml_query::error::Error| -> bool { + is_match!(e.kind(), &::toml_query::error::ErrorKind::IdentifierNotFoundInDocument(_)) + }; + let uuid = self.uuid(); ModuleEntryPath::new(format!("taskwarrior/{}", uuid)) .into_storeid() @@ -234,14 +245,21 @@ impl<'a> IntoTask<'a> for TTask { .and_then(|mut fle| { { let mut hdr = fle.get_header_mut(); - let read = hdr.read("todo").map_err_into(TodoErrorKind::StoreError); - if try!(read).is_none() { - try!(hdr - .set("todo", Value::Table(BTreeMap::new())) - .map_err_into(TodoErrorKind::StoreError)); + let todo_query = String::from("todo"); + + if let Err(e) = hdr.read(&todo_query) { + if no_identifier(&e) { + try!(hdr + .set(&String::from("todo"), Value::Table(BTreeMap::new())) + .map_err_into(TodoErrorKind::StoreError)); + } else { + let e = Box::new(e); + return Err(TodoErrorKind::StoreError.into_error_with_cause(e)) + } } - try!(hdr.set("todo.uuid", Value::String(format!("{}",uuid))) + try!(hdr.set(&String::from("todo.uuid"), + Value::String(format!("{}", uuid))) .map_err_into(TodoErrorKind::StoreError)); }