summaryrefslogtreecommitdiffstats
path: root/libimagtodo/src/task.rs
blob: ba9c7e9e792861e0ac74f0d6db043417b2ac60b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use std::collections::BTreeMap;
use toml::Value;

use task_hookrs::task::Task as TTask;

use libimagstore::store::{FileLockEntry, Store};
use libimagstore::storeid::IntoStoreId;
use module_path::ModuleEntryPath;

use error::{TodoError, TodoErrorKind};
use result::Result;

/// Task struct containing a `FileLockEntry`
#[derive(Debug)]
pub struct Task<'a> {
    pub flentry : FileLockEntry<'a>,
}

impl<'a> Task<'a> {
    /// Concstructs a new `Task` with a `FileLockEntry`
    pub fn new(fle : FileLockEntry<'a>) -> Task<'a> {
        Task {
            flentry : fle
        }
    }
}

/// A trait to get a `libimagtodo::task::Task` out of the implementing object.
/// This Task struct is merely a wrapper for a `FileLockEntry`, therefore the function name
/// `into_filelockentry`.
pub trait IntoTask<'a> {
    /// # Usage
    /// ```ignore
    /// use std::io::stdin;
    ///
    /// use task_hookrs::task::Task;
    /// use task_hookrs::import::import;
    /// use libimagstore::store::{Store, FileLockEntry};
    ///
    /// if let Ok(task_hookrs_task) = import(stdin()) {
    ///     // Store is given at runtime
    ///     let task = task_hookrs_task.into_filelockentry(store);
    ///     println!("Task with uuid: {}", task.flentry.get_header().get("todo.uuid"));
    /// }
    /// ```
    fn into_filelockentry(self, store : &'a Store) -> Result<Task<'a>>;
}
impl<'a> IntoTask<'a> for TTask {
    fn into_filelockentry(self, store : &'a Store) -> Result<Task<'a>> {
        let uuid = self.uuid();
        let store_id = ModuleEntryPath::new(format!("taskwarrior/{}", uuid)).into_storeid();
        match store.retrieve(store_id) {
            Err(e) => {
                return Err(TodoError::new(TodoErrorKind::StoreError, Some(Box::new(e))))
            },
            Ok(mut fle) => {
                {
                    let mut header = fle.get_header_mut();
                    match header.read("todo") {
                        Ok(None) => {
                            match header.set("todo", Value::Table(BTreeMap::new())) {
                                Ok(_) => { },
                                Err(e) => {
                                    return Err(TodoError::new(TodoErrorKind::StoreError, Some(Box::new(e))))
                                }
                            }
                        }
                        Ok(Some(_)) => { }
                        Err(e) => {
                            return Err(TodoError::new(TodoErrorKind::StoreError, Some(Box::new(e))))
                        }
                    }
                    match header.set("todo.uuid", Value::String(format!("{}",uuid))) {
                        Ok(_) => { },
                        Err(e) => {
                            return Err(TodoError::new(TodoErrorKind::StoreError, Some(Box::new(e))))
                        }
                    }
                }
                // If none of the errors above have returned the function, everything is fine
                Ok(Task { flentry : fle } )
            }
        }
    }
}