sors 0.0.1

A standalone organization system to organize and track tasks
Documentation
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//! Holding data which are serialized and stored to disk.

use uuid::Uuid;
use serde::{Serialize, Deserialize};
use super::tasks::*;
use super::clock::*;
use super::error::*;
use std::io::Write;
use std::io::Read;
use std::fs::File;
use std::collections::HashMap;
use std::rc::Rc;
use std::path::Path;
use snafu::ResultExt;
use chrono::prelude::*;
use super::statics::*;

/// Holding data which are serialized and stored to disk.
/// 
/// # Example
/// 
/// ```
/// use todoapp3::doc::Doc;
/// use todoapp3::tasks::Task;
/// use todoapp3::TaskMod;
/// use todoapp3::tasks::Progress;
/// use std::rc::Rc;
/// 
/// // Initialize the doc.
/// let mut doc = Doc::new();
/// 
/// // The doc now contains one single root task.  Lets edit its title and
/// // body text.
/// doc.modify_task(&doc.root.clone(), |task| {
///     task
///         .set_title("Title of the root task")
///         .set_body("Some text");
/// });
/// 
/// // Now lets access the roots title.
/// assert_eq!(doc.get_root().title, "Title of the root task");
/// 
/// // Add lets generate a new task and set some title as well.
/// let mut child1 = Rc::new(Task::new());
/// child1.set_title("I'm the child");
/// 
/// // New lets add this text under the root.
/// let root_ref = doc.root.clone();
/// doc.add_subtask(child1, &root_ref);
/// 
/// // Now lets read the title of doc's first child.
/// {
///     // Get the new root
///     let root = doc.get_root();
///     // Get the child.  `children` is a Vec of IDs which are
///     // used to get the task.
///     let child = doc.get(&root.children[0]);
///     // Read the title
///     assert_eq!("I'm the child", child.title);
/// 
/// }
/// 
/// // Now lets add a body to the child
/// {
///     // Get the root
///     let root = doc.get_root();
///     // Get the child's id
///     let child_id = root.children[0];
///     // Modify the body
///     doc.modify_task(&child_id, |child| {
///         child.set_body("This is the child's body");
///     });
///     // Read the body
///     assert_eq!("This is the child's body", doc.get(&child_id).body);
/// }
/// 
/// 
/// // Now lets work on the child
/// {
///     // Get the root
///     let root = doc.get_root();
///     // Get the child's id
///     let child_id = root.children[0];
/// 
///     // Let's make it a task and assign TODO to it
///     doc.modify_task(&child_id, |child| {
///         child.set_progress(Progress::Todo);
///     });
/// 
///     // Start working and start tracking the time.
///     doc.clock_new().expect("Create a new clock");
///     
///     // Lets point the current clock to the child task.
///     doc.clock_assign(child_id).expect("Assign clock");
/// 
///     // Do some work.  And when done, mark it as done.
///     doc.modify_task(&child_id, |child| {
///         child.set_progress(Progress::Done);
///     });
/// 
///     // And finally clock out.
///     doc.clock_out().expect("Clocking out");
/// }
/// 
/// ```
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Doc {
    pub map: HashMap<Uuid, Rc<Task>>,

    #[serde(default)]
    pub clocks: HashMap<Uuid, Rc<Clock>>,
    pub current_clock: Option<Uuid>,
    pub root: Uuid
}

impl Doc {
    /// Create a new, empty document.
    /// 
    /// It is initialized with one empty root task which contains
    /// a random UUID.
    /// 
    /// # Example
    /// 
    /// ```
    /// use todoapp3::doc::Doc;
    /// let doc = Doc::new();
    /// ```
    pub fn new() -> Doc {
        let mut map = HashMap::new();
        let root = Task::new();
        let root_id = root.id.clone();
        map.insert(root_id.clone(), Rc::new(root));
        Doc {
            map: map,
            clocks: HashMap::default(),
            current_clock: None,
            root: root_id
        }
    }

    /// Write the content to into the specified file.
    pub fn save(&self, path: impl AsRef<Path>) -> Result<()> {
        Ok(serde_json::to_writer(
            File::create(path).context(IO)?, self)
            .context(SerdeSerializationError)?)
    }

    /// Load the document of hte given path and return a new doc.
    /// 
    /// # Error
    /// Produces an error if there are IO issues or if the file format
    /// couldn't be parsed.
    pub fn load(path: impl AsRef<Path>) -> Result<Doc> {
        Ok(
            serde_json::from_reader(
                File::open(path).context(IO)?
            ).context(SerdeSerializationError)?
        )
    }

    /// Load task which contains the given id.
    /// 
    /// # Panic
    /// Panics if no task does exist.
    pub fn get(&self, id: &Uuid) -> Rc<Task> {
        self.map.get(id).unwrap().clone()
    }

    /// Get the root task.
    pub fn get_root(&self) -> Rc<Task> {
        self.get(&self.root)
    }

    /// Adds or replaces the given task.
    /// 
    /// The task is identified by its id.
    pub fn upsert(&mut self, task: Rc<Task>) {
        self.map.insert(task.id.clone(), task);
    }

    /// Modify the task with a function or closure
    /// 
    /// # Panic
    /// Panics if no id for the task exists.
    pub fn modify_task<F>(&mut self, id: &Uuid, func: F)
            where F: Fn(&mut Rc<Task>) {
        let mut task = self.get(id);
        Rc::make_mut(&mut task);
        func(&mut task);
        self.upsert(task);
    }

    /// Add a new task as child of the given parent id.
    /// 
    /// # Panic
    /// Panics if the id of the parent task doesn't exist.
    pub fn add_subtask(&mut self, task: Rc<Task>, parent_ref: &Uuid) {
        self.modify_task(parent_ref, |parent| { parent.add_child(task.id.clone()); });
        self.upsert(task);
    }

    /// Return the parent of the given task.
    /// 
    /// It will be None, if not found.
    pub fn find_parent(&self, task_ref: &Uuid) -> Option<Uuid> {
        self.map.values().find(|task| task.children.iter().any(|child_id| child_id == task_ref)).map(|task| task.id.clone())
    }

    /// Checks if the first given task is a child or the second task or if it's
    /// the task itself.
    pub fn is_in_hierarchy_of(&self, child_task: &Uuid, parent_task: &Uuid) -> bool {
        let mut tmp_task = child_task.clone();
        let mut counter = 0;
        loop {
            // In case of a loop (which hopefully doesn't happen), break after
            // 200 iterations.
            if counter == 200 {
                return false;
            }
            counter += 1;
            if tmp_task == *parent_task {
                return true;
            }
            if let Some(new_parent) = self.find_parent(&tmp_task) {
                tmp_task = new_parent.clone();
            } else {
                return false;
            }
        }
    }

    /// Get all tasks, from the given one to the root.
    pub fn path(&self, task_ref: &Uuid) -> Vec<Uuid> {
        let mut res = Vec::new();
        let mut task_ref_opt = Some(task_ref.clone());
        loop {
            if let Some(task_ref) = task_ref_opt {
                res.push(task_ref.clone());
                task_ref_opt = self.find_parent(&task_ref);
            } else {
                break;
            }
        }
        res
    }

    /// Return a String which contains a html code which represents the givent task.
    /// 
    /// # Panic
    /// Panics if the task id is not found.
    pub fn to_html(&self, task_ref: &Uuid) -> String {
        let mut html = String::new();
        let task = self.get(task_ref);
        html.push_str("<!doctype html><html><head><link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css\" integrity=\"sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T\" crossorigin=\"anonymous\"></head><body><div class=\"container\">");

        let mut breadcrumb_item_opn = Some(task_ref.clone());
        let mut breadcrumb_data = Vec::new();
        loop {
            if let Some(breadcrumb_item) = breadcrumb_item_opn {
                breadcrumb_data.push(breadcrumb_item.clone());
                breadcrumb_item_opn = self.find_parent(&breadcrumb_item);
            } else {
                break;
            }
        }
        breadcrumb_data.iter().rev().zip(1..).for_each(|(breadcrumb_ref, i)| {
            let task = self.get(breadcrumb_ref);
            if i > 1 {
                html.push_str(" -> ");
            }
            html.push_str(&format!("<a href=\"{}.html\">{}</a>", breadcrumb_ref, task.title));
        });

        let (done, all_subtasks) = self.progress_summary(task_ref);
        html.push_str(&format!("[{}/{}]", done, all_subtasks));

        html.push_str(&markdown::to_html(&task.body));
        html.push_str("<hr/>");
        html.push_str("<ul>");
        for child in task.children.iter() {
            let child_task = self.get(child);
            html.push_str("<li><a href=\"");
            html.push_str(&child.to_string());
            html.push_str(".html\">");
            html.push_str(&if let Some(ref progress) = child_task.progress { 
                progress.to_string()
            } else {
                String::new()
            });
            html.push_str(" ");
            html.push_str(&child_task.title);
            html.push_str("</a></li>");
        }
        html.push_str("</ul>");
        html.push_str("</div></body></html>");
        html
    }

    /// Summary how many children are done vs how many have any progress state.
    /// 
    /// It counts the children which have a progress assigned which indicates that
    /// the task is not done in the first tuple entry and the count of children
    /// which contain any progress field.  Actually, this is the current progress
    /// state of the task: todo/all.
    pub fn progress_summary(&self, task_ref: &Uuid) -> (i32, i32) {
        self.get(task_ref)
            .children.iter()
            .filter_map(|child| self.get(child).progress)
            .fold((0, 0), |(acc_done, acc_sum), progress| (
                acc_done + if progress.done() { 1 } else { 0 },
                acc_sum + 1
            ))
    }

    /// Get the clock which is under the name.
    /// 
    /// # Error
    /// Returns an error if a clock wasn't found under the name.
    pub fn clock(&self, clock_ref: &Uuid) -> Result<Rc<Clock>> {
        self.clocks.get(clock_ref).map(|item| item.clone()).ok_or(Error::ClockNotFound {})
    }

    /// Insert or replace the clock.
    pub fn upsert_clock(&mut self, clock: Rc<Clock>) {
        self.clocks.insert(clock.id.clone(), clock);
    }

    /// Stops clocking time.
    /// 
    /// # Error
    /// If the internal state is incorrect and the current_clock
    /// references to a clock which doesn't exist, it will return
    /// an error.
    pub fn clock_out(&mut self) -> Result<bool> {
        if let Some(ref clock_ref) = self.current_clock {
            let mut clock = self.clock(clock_ref)?;
            clock.set_end(Local::now());
            self.upsert_clock(clock);
            self.current_clock = None;
            Ok(true)
        } else {
            Ok(false)
        }
    }

    /// Generate a new clock which starts at the time it was called.
    /// 
    /// # Error
    /// Return an error on an internal error if the clock out doesn't
    /// work.
    pub fn clock_new(&mut self) -> Result<Rc<Clock>> {
        self.clock_out()?;
        let clock = Rc::new(Clock {
            id: Uuid::new_v4(),
            start: Local::now(),
            end: None,
            comment: None,
            task_id: None
        });
        self.upsert_clock(clock.clone());
        self.current_clock = Some(clock.id.clone());
        Ok(clock)
    }

    /// Assign the given task to the active clock.
    /// 
    /// # Error
    /// It will return an error if the internal state is wrong and the current
    /// clock id cannot be found.
    pub fn clock_assign(&mut self, task_ref: Uuid) -> Result<()> {
        if let Some(ref clock_ref) = self.current_clock {
            let mut clock = self.clock(clock_ref)?;
            clock.set_task_id(task_ref);
            self.upsert_clock(clock);
        }
        Ok(())
    }

    /// Set the comment of the active clock.
    /// 
    /// # Error
    /// It will return an error if the internal state is wrong and the current
    /// clock id cannot be found.
    pub fn clock_comment(&mut self, comment: impl ToString) -> Result<()> {
        if let Some(ref clock_ref) = self.current_clock {
            let mut clock = self.clock(clock_ref)?;
            clock.set_comment(comment.to_string());
            self.upsert_clock(clock);
        }
        Ok(())
    }

    /// Get the clocks assigned to the given task.
    pub fn task_clock(&self, task_ref: &Uuid) -> Vec<Rc<Clock>> {
        self.clocks.values()
            .filter(|clock| clock.task_id == Some(*task_ref))
            .map(|clock| clock.clone()).collect()
    }
    
    /// Get the clocks for the given date.
    pub fn day_clock(&self, date: Date<Local>, main_task: impl Into<Option<Uuid>>) -> Vec<Rc<Clock>> {
        let main_task = main_task.into();
        self.clocks.values()
            .filter(|clock| clock.start.date() == date)
            .filter(|clock|
                if let Some(clock_task) = clock.task_id {
                    if let Some(main_task) = main_task {
                        self.is_in_hierarchy_of(&clock_task, &main_task)
                    } else { true }
                } else { true })
            .map(|clock| clock.clone()).collect()
    }

    /// Get the clocks of the given date.
    pub fn range_clock(&self, start: Date<Local>, end: Date<Local>, main_task: impl Into<Option<Uuid>>) -> Vec<Rc<Clock>> {
        let main_task = main_task.into();
        self.clocks.values()
            .filter(|clock| clock.start.date() >= start && clock.start.date() <= end)
            .filter(|clock|
                if let Some(clock_task) = clock.task_id {
                    if let Some(main_task) = main_task {
                        self.is_in_hierarchy_of(&clock_task, &main_task)
                    } else { true }
                } else { true })
            .cloned().collect()
    }
}




pub fn rec_print(doc: &mut Doc, task_id: &Uuid, level: usize, max_depth: usize) {
    if level >= max_depth {
        return;
    }
    let task = doc.get(task_id);
    for _ in 0..level {
        print!(" ");
    }
    print!("* ");
    println!("{} {}", task.id, task.title);
    for child_id in task.children.iter() {
        rec_print(doc, child_id, level + 1, max_depth);
    }
}

pub fn dump_html_rec(doc: &Doc, dir: &Path, task_ref: &Uuid) -> Result<()> {
    let task = doc.get(task_ref);
    for child in task.children.iter() {
        dump_html_rec(doc, dir, child)?;
    }
    let task_html = doc.to_html(task_ref);
    let filename = dir.join(format!("{}.html", task_ref));
    println!("{}", filename.to_str().unwrap_or("N/A"));
    let mut html_file = File::create(filename).context(IO)?;
    html_file.write_all(task_html.as_bytes()).context(IO)?;
    Ok(())
}

pub fn dump_html(doc: &Doc, dir: &Path, task_ref: &Uuid) -> Result<()> {
    std::fs::create_dir_all(dir).context(IO)?;
    dump_html_rec(doc, dir, task_ref)?;
    let filename = dir.join(format!("index.html"));
    let mut index_file = File::create(filename).context(IO)?;
    index_file.write_all(b"<!doctype html><html><head></head><body><a href=\"").context(IO)?;
    index_file.write_all(task_ref.to_string().as_bytes()).context(IO)?;
    index_file.write_all(b".html\">Index</a></body></html>").context(IO)?;
    Ok(())
}

pub fn vim_edit_task(mut task: Rc<Task>) -> Rc<Task> {
    {   
        let mut out = File::create(&*TASK_FILE).expect("Could not create .task file");
        out.write_all(task.title.as_bytes()).expect("Couldn't write title to .task file");
        out.write_all("\n\n".as_bytes()).expect("Couldn't write newlines to .task file");
        out.write_all(task.body.as_bytes()).expect("Couldn't write body to .task file");
    }
    subprocess::Exec::cmd("vi").arg(&*TASK_FILE).join().unwrap();
    let mut content = String::new();
    {
        let mut input = File::open(&*TASK_FILE).expect("Could not open .task file");
        input.read_to_string(&mut content).expect("Couldn't read .task file");
    }
    let mut lines = content.lines();
    let title = lines.next().expect("Couldn't extract title");
    let body = lines.fold(String::new(), |mut acc: String, item| { acc.push_str(&item); acc.push('\n'); acc});
    task.set_title(title).set_body(body.trim());
    task
}