2017-05-30 18:25:00 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2019-01-03 01:32:07 +00:00
|
|
|
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2017-05-30 18:25:00 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
|
|
|
|
use std::io::BufRead;
|
|
|
|
use std::result::Result as RResult;
|
|
|
|
|
|
|
|
use toml::Value;
|
2019-05-29 10:34:47 +00:00
|
|
|
use toml::map::Map;
|
2017-05-30 18:25:00 +00:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
use task_hookrs::task::Task as TTask;
|
|
|
|
use task_hookrs::import::{import_task, import_tasks};
|
2018-10-30 17:40:51 +00:00
|
|
|
use failure::Fallible as Result;
|
|
|
|
use failure::ResultExt;
|
|
|
|
use failure::Error;
|
|
|
|
use failure::err_msg;
|
2017-05-30 18:25:00 +00:00
|
|
|
|
|
|
|
use libimagstore::store::{FileLockEntry, Store};
|
2018-10-30 17:40:51 +00:00
|
|
|
use libimagerror::errors::ErrorMsg as EM;
|
2017-05-30 18:25:00 +00:00
|
|
|
|
2019-02-28 16:28:32 +00:00
|
|
|
use crate::iter::TaskIdIterator;
|
2017-05-30 18:25:00 +00:00
|
|
|
|
|
|
|
/// Task struct containing a `FileLockEntry`
|
2017-09-02 12:30:27 +00:00
|
|
|
pub trait TaskStore<'a> {
|
|
|
|
fn import_task_from_reader<R: BufRead>(&'a self, r: R) -> Result<(FileLockEntry<'a>, String, Uuid)>;
|
|
|
|
fn get_task_from_import<R: BufRead>(&'a self, r: R) -> Result<RResult<FileLockEntry<'a>, String>>;
|
|
|
|
fn get_task_from_string(&'a self, s: String) -> Result<RResult<FileLockEntry<'a>, String>>;
|
|
|
|
fn get_task_from_uuid(&'a self, uuid: Uuid) -> Result<Option<FileLockEntry<'a>>>;
|
|
|
|
fn retrieve_task_from_import<R: BufRead>(&'a self, r: R) -> Result<FileLockEntry<'a>>;
|
|
|
|
fn retrieve_task_from_string(&'a self, s: String) -> Result<FileLockEntry<'a>>;
|
|
|
|
fn delete_tasks_by_imports<R: BufRead>(&self, r: R) -> Result<()>;
|
|
|
|
fn delete_task_by_uuid(&self, uuid: Uuid) -> Result<()>;
|
2018-04-20 18:59:39 +00:00
|
|
|
fn all_tasks(&self) -> Result<TaskIdIterator>;
|
2017-09-02 12:30:27 +00:00
|
|
|
fn new_from_twtask(&'a self, task: TTask) -> Result<FileLockEntry<'a>>;
|
2017-09-02 10:23:29 +00:00
|
|
|
}
|
2017-05-30 18:25:00 +00:00
|
|
|
|
2017-09-02 12:30:27 +00:00
|
|
|
impl<'a> TaskStore<'a> for Store {
|
2017-05-30 18:25:00 +00:00
|
|
|
|
2017-09-02 12:30:27 +00:00
|
|
|
fn import_task_from_reader<R: BufRead>(&'a self, mut r: R) -> Result<(FileLockEntry<'a>, String, Uuid)> {
|
2017-05-30 18:25:00 +00:00
|
|
|
let mut line = String::new();
|
2018-10-30 17:40:51 +00:00
|
|
|
r.read_line(&mut line).context(EM::UTF8Error)?;
|
2017-05-30 18:25:00 +00:00
|
|
|
import_task(&line.as_str())
|
2018-10-30 17:40:51 +00:00
|
|
|
.context(err_msg("Error importing"))
|
|
|
|
.map_err(Error::from)
|
2017-05-30 18:25:00 +00:00
|
|
|
.and_then(|t| {
|
|
|
|
let uuid = t.uuid().clone();
|
2017-09-02 12:30:27 +00:00
|
|
|
self.new_from_twtask(t).map(|t| (t, line, uuid))
|
2017-05-30 18:25:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a task from an import string. That is: read the imported string, get the UUID from it
|
|
|
|
/// and try to load this UUID from store.
|
|
|
|
///
|
|
|
|
/// Possible return values are:
|
|
|
|
///
|
|
|
|
/// * Ok(Ok(Task))
|
|
|
|
/// * Ok(Err(String)) - where the String is the String read from the `r` parameter
|
|
|
|
/// * Err(_) - where the error is an error that happened during evaluation
|
|
|
|
///
|
2017-09-02 12:30:27 +00:00
|
|
|
fn get_task_from_import<R: BufRead>(&'a self, mut r: R) -> Result<RResult<FileLockEntry<'a>, String>> {
|
2017-05-30 18:25:00 +00:00
|
|
|
let mut line = String::new();
|
2018-10-30 17:40:51 +00:00
|
|
|
r.read_line(&mut line).context(EM::UTF8Error)?;
|
2017-09-02 12:30:27 +00:00
|
|
|
self.get_task_from_string(line)
|
2017-05-30 18:25:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a task from a String. The String is expected to contain the JSON-representation of the
|
|
|
|
/// Task to get from the store (only the UUID really matters in this case)
|
|
|
|
///
|
|
|
|
/// For an explanation on the return values see `Task::get_from_import()`.
|
2017-09-02 12:30:27 +00:00
|
|
|
fn get_task_from_string(&'a self, s: String) -> Result<RResult<FileLockEntry<'a>, String>> {
|
2017-05-30 18:25:00 +00:00
|
|
|
import_task(s.as_str())
|
2018-10-30 17:40:51 +00:00
|
|
|
.context(err_msg("Import error"))
|
|
|
|
.map_err(Error::from)
|
2017-05-30 18:25:00 +00:00
|
|
|
.map(|t| t.uuid().clone())
|
2017-09-02 12:30:27 +00:00
|
|
|
.and_then(|uuid| self.get_task_from_uuid(uuid))
|
2017-05-30 18:25:00 +00:00
|
|
|
.and_then(|o| match o {
|
|
|
|
None => Ok(Err(s)),
|
|
|
|
Some(t) => Ok(Ok(t)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a task from an UUID.
|
|
|
|
///
|
|
|
|
/// If there is no task with this UUID, this returns `Ok(None)`.
|
2017-09-02 12:30:27 +00:00
|
|
|
fn get_task_from_uuid(&'a self, uuid: Uuid) -> Result<Option<FileLockEntry<'a>>> {
|
2019-02-28 16:28:32 +00:00
|
|
|
crate::module_path::new_id(format!("taskwarrior/{}", uuid)).and_then(|store_id| self.get(store_id))
|
2017-05-30 18:25:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Same as Task::get_from_import() but uses Store::retrieve() rather than Store::get(), to
|
|
|
|
/// implicitely create the task if it does not exist.
|
2017-09-02 12:30:27 +00:00
|
|
|
fn retrieve_task_from_import<R: BufRead>(&'a self, mut r: R) -> Result<FileLockEntry<'a>> {
|
2017-05-30 18:25:00 +00:00
|
|
|
let mut line = String::new();
|
2018-10-30 17:40:51 +00:00
|
|
|
r.read_line(&mut line).context(EM::UTF8Error)?;
|
2017-09-02 12:30:27 +00:00
|
|
|
self.retrieve_task_from_string(line)
|
2017-05-30 18:25:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieve a task from a String. The String is expected to contain the JSON-representation of
|
|
|
|
/// the Task to retrieve from the store (only the UUID really matters in this case)
|
2017-09-02 12:30:27 +00:00
|
|
|
fn retrieve_task_from_string(&'a self, s: String) -> Result<FileLockEntry<'a>> {
|
|
|
|
self.get_task_from_string(s)
|
2017-05-30 18:25:00 +00:00
|
|
|
.and_then(|opt| match opt {
|
|
|
|
Ok(task) => Ok(task),
|
|
|
|
Err(string) => import_task(string.as_str())
|
2018-10-30 17:40:51 +00:00
|
|
|
.context(err_msg("Import error"))
|
|
|
|
.map_err(Error::from)
|
2017-09-02 12:30:27 +00:00
|
|
|
.and_then(|t| self.new_from_twtask(t)),
|
2017-05-30 18:25:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-09-02 12:30:27 +00:00
|
|
|
fn delete_tasks_by_imports<R: BufRead>(&self, r: R) -> Result<()> {
|
2017-05-30 18:25:00 +00:00
|
|
|
use task_hookrs::status::TaskStatus;
|
|
|
|
|
|
|
|
for (counter, res_ttask) in import_tasks(r).into_iter().enumerate() {
|
2018-10-30 17:40:51 +00:00
|
|
|
let ttask = res_ttask.context(err_msg("Import error"))?;
|
|
|
|
|
|
|
|
if counter % 2 == 1 {
|
|
|
|
// Only every second task is needed, the first one is the
|
|
|
|
// task before the change, and the second one after
|
|
|
|
// the change. The (maybe modified) second one is
|
|
|
|
// expected by taskwarrior.
|
2019-02-18 12:22:27 +00:00
|
|
|
//
|
2018-10-30 17:40:51 +00:00
|
|
|
// Taskwarrior does not have the concept of deleted tasks, but only modified
|
|
|
|
// ones.
|
|
|
|
//
|
|
|
|
// Here we check if the status of a task is deleted and if yes, we delete it
|
|
|
|
// from the store.
|
|
|
|
if *ttask.status() == TaskStatus::Deleted {
|
|
|
|
let _ = self.delete_task_by_uuid(*ttask.uuid())?;
|
|
|
|
info!("Deleted task {}", *ttask.uuid());
|
|
|
|
}
|
2017-05-30 18:25:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-09-02 12:30:27 +00:00
|
|
|
fn delete_task_by_uuid(&self, uuid: Uuid) -> Result<()> {
|
2019-02-28 16:28:32 +00:00
|
|
|
crate::module_path::new_id(format!("taskwarrior/{}", uuid)).and_then(|id| self.delete(id))
|
2017-05-30 18:25:00 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 18:59:39 +00:00
|
|
|
fn all_tasks(&self) -> Result<TaskIdIterator> {
|
2018-12-30 16:40:11 +00:00
|
|
|
self.entries().map(|i| TaskIdIterator::new(i))
|
2017-05-30 18:25:00 +00:00
|
|
|
}
|
|
|
|
|
2017-09-02 12:30:27 +00:00
|
|
|
fn new_from_twtask(&'a self, task: TTask) -> Result<FileLockEntry<'a>> {
|
2017-05-30 19:00:29 +00:00
|
|
|
use toml_query::read::TomlValueReadExt;
|
|
|
|
use toml_query::set::TomlValueSetExt;
|
|
|
|
|
2017-09-02 10:23:29 +00:00
|
|
|
let uuid = task.uuid();
|
2019-02-28 16:28:32 +00:00
|
|
|
crate::module_path::new_id(format!("taskwarrior/{}", uuid)).and_then(|id| {
|
2019-04-09 17:19:40 +00:00
|
|
|
self.retrieve(id).and_then(|mut fle| {
|
|
|
|
{
|
|
|
|
let hdr = fle.get_header_mut();
|
|
|
|
if hdr.read("todo")?.is_none() {
|
2019-05-29 10:34:47 +00:00
|
|
|
hdr.set("todo", Value::Table(Map::new()))?;
|
2019-04-09 17:19:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hdr.set("todo.uuid", Value::String(format!("{}",uuid)))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If none of the errors above have returned the function, everything is fine
|
|
|
|
Ok(fle)
|
2017-05-30 18:25:00 +00:00
|
|
|
})
|
2019-04-09 17:19:40 +00:00
|
|
|
})
|
2017-05-30 18:25:00 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|