vault-tasks-core 2.0.1

vault-tasks-core
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
use color_eyre::{Result, eyre::bail};

use std::{collections::HashSet, fmt::Display};
use vault_data::Vaults;

use filter::Filter;
use tracing::{error, warn};
use vault_parser::VaultParser;

use crate::{
    config::TasksConfig,
    vault_data::{FileEntryNode, VaultNode},
};

pub mod config;
pub mod date;
pub mod filter;
pub mod logging;
pub mod parser;
pub mod sorter;
pub mod task;
pub mod vault_data;
mod vault_parser;

// Re-export logging functions for easier access
pub use logging::init as init_logging;

pub struct TaskManager {
    pub tasks_refactored: Vaults,
    config: TasksConfig,
    pub tags: HashSet<String>,
    pub current_filter: Option<Filter>,
}
impl Default for TaskManager {
    fn default() -> Self {
        Self {
            tasks_refactored: Vaults { root: vec![] }, // TODO: will replace tasks eventually
            tags: HashSet::new(),
            current_filter: None,
            config: TasksConfig::default(),
        }
    }
}
// Helper enum to unify return types
#[derive(Clone, Debug)]
pub enum Found {
    Root(Vaults),
    Node(VaultNode),
    FileEntry(FileEntryNode),
}
impl TaskManager {
    const DROP_FILE_DEFAULT_PATH: &str = "task_drop_file.md";

    /// Loads a vault from a `Config` and returns a `TaskManager`.
    ///
    /// # Errors
    ///
    /// This function will return an error if the vault can't be loaded.
    pub fn load_from_config(config: &TasksConfig) -> Result<Self> {
        let mut res = Self::default();
        res.reload(config)?;
        Ok(res)
    }

    /// Reloads the `VaultData` from file system.
    ///
    /// # Errors
    ///
    /// This function will return an error if the vault can't be parsed, or if tasks can't be fixed (relative dates are replaced by fixed dates for example).
    pub fn reload(&mut self, config: &TasksConfig) -> Result<()> {
        self.config = config.clone();
        if self
            .config
            .core
            .vault_path
            .to_str()
            .is_some_and(str::is_empty)
        {
            bail!( "No vault path provided (use `--vault-path <PATH>`) and no default path set in config file".to_string(), );
        }
        if !self.config.core.vault_path.exists() && !cfg!(test) {
            bail!(
                "Vault path does not exist: {:?}",
                self.config.core.vault_path
            );
        }

        let vault_parser = VaultParser::new(config.clone());
        let tasks = vault_parser.scan_vault()?;

        self.tasks_refactored = tasks;

        Self::rewrite_vault_tasks(config, &self.tasks_refactored)
            .unwrap_or_else(|e| error!("Failed to fix tasks: {e}"));

        let mut tags = HashSet::new();
        Self::collect_tags(&self.tasks_refactored, &mut tags);
        self.tags = tags;
        Ok(())
    }
    /// Explores every `NewFileEntry` from the vault and applies the given function `f` on it.
    pub fn map_file_entries(
        tasks: &Vaults,
        f: &mut impl FnMut(&FileEntryNode) -> FileEntryNode,
    ) -> Vaults {
        fn explore_nodes(
            node: &VaultNode,
            f: &mut impl FnMut(&FileEntryNode) -> FileEntryNode,
        ) -> VaultNode {
            match node.clone() {
                VaultNode::Vault {
                    name,
                    path,
                    content,
                } => VaultNode::Vault {
                    name,
                    path,
                    content: content.iter().map(|v| explore_nodes(v, f)).collect(),
                },
                VaultNode::Directory {
                    name,
                    path,
                    content,
                } => VaultNode::Directory {
                    name,
                    path,
                    content: content.iter().map(|v| explore_nodes(v, f)).collect(),
                },
                VaultNode::File {
                    name,
                    path,
                    content,
                } => VaultNode::File {
                    name,
                    path,
                    content: content.iter().map(f).collect(),
                },
            }
        }
        let new_root = tasks.root.iter().map(|n| explore_nodes(n, f)).collect();
        Vaults { root: new_root }
    }

    /// Explores the vault and fills a `&mut HashSet<String>` with every tags found.
    pub fn collect_tags(tasks: &Vaults, tags: &mut HashSet<String>) {
        fn gather_tags_from_file_entry(file_entry: &FileEntryNode, tags: &mut HashSet<String>) {
            match file_entry {
                FileEntryNode::Task(task) => {
                    task.tags.clone().unwrap_or_default().iter().for_each(|t| {
                        tags.insert(t.clone());
                    });
                    task.subtasks.iter().for_each(|subtask| {
                        gather_tags_from_file_entry(&FileEntryNode::Task(subtask.clone()), tags);
                    });
                }
                FileEntryNode::Header { content, .. } => {
                    for c in content {
                        gather_tags_from_file_entry(c, tags);
                    }
                }
            }
        }
        Self::map_file_entries(tasks, &mut |file_entry: &FileEntryNode| {
            gather_tags_from_file_entry(file_entry, tags);
            file_entry.clone()
        });
    }

    /// Follows the `path` to retrieve the correct `VaultData`.
    ///
    /// # Errors
    /// Will return an error if the vault is empty or the path cannot be resolved
    pub fn resolve_path(&self, path: &[String]) -> Result<Found> {
        fn aux_node(node: &VaultNode, path: &[String], path_index: usize) -> Option<Found> {
            if path_index == path.len() {
                return Some(Found::Node(node.clone()));
            }

            match node {
                VaultNode::Vault { name, content, .. }
                | VaultNode::Directory { name, content, .. } => {
                    if *name == path[path_index] {
                        // Check if we're at the end of the path
                        if path_index + 1 == path.len() {
                            return Some(Found::Node(node.clone()));
                        }
                        // Otherwise, continue recursing into children
                        return content
                            .iter()
                            .find_map(|child| aux_node(child, path, path_index + 1));
                    }
                }
                VaultNode::File { name, content, .. } => {
                    if *name == path[path_index] {
                        // If we're at the end of the path, return file entries as nodes
                        if path_index + 1 == path.len() {
                            // Wrap each file entry in a temporary file node for conversion
                            return Some(Found::Node(node.clone()));
                        }
                        // Navigate into file entries
                        return content
                            .iter()
                            .find_map(|entry| aux_file_entry(entry, path, path_index + 1));
                    }
                }
            }
            None
        }

        fn aux_file_entry(
            file_entry: &FileEntryNode,
            path: &[String],
            path_index: usize,
        ) -> Option<Found> {
            if path_index == path.len() {
                return Some(Found::FileEntry(file_entry.clone()));
            }

            match file_entry {
                FileEntryNode::Header { name, content, .. } => {
                    if *name == path[path_index] {
                        // Check if we're at the end of the path
                        if path_index + 1 == path.len() {
                            return Some(Found::FileEntry(file_entry.clone()));
                        }
                        // Otherwise, continue recursing into children
                        return content
                            .iter()
                            .find_map(|child| aux_file_entry(child, path, path_index + 1));
                    }
                }
                FileEntryNode::Task(task) => {
                    if task.name == path[path_index] {
                        // Check if we're at the end of the path
                        if path_index + 1 == path.len() {
                            return Some(Found::FileEntry(file_entry.clone()));
                        }
                        // Otherwise, continue recursing into subtasks
                        let subtask_entries: Vec<FileEntryNode> = task
                            .subtasks
                            .iter()
                            .map(|t| FileEntryNode::Task(t.clone()))
                            .collect();
                        return subtask_entries
                            .iter()
                            .find_map(|st| aux_file_entry(st, path, path_index + 1));
                    }
                }
            }
            None
        }
        // apply filter
        let Some(filtered_tasks) = filter::filter(&self.tasks_refactored, &self.current_filter)
        else {
            bail!("No tasks found after applying filter");
        };
        // If path is empty, return all root nodes
        if path.is_empty() {
            return Ok(Found::Root(filtered_tasks.clone()));
        }

        // Try to find entries in each vault root node
        for node in &filtered_tasks.root {
            if let Some(found) = aux_node(node, path, 0) {
                return Ok(found); // path only resolves to one node
            }
        }

        bail!("Couldn't find entries at path: {:?}", path)
    }

    /// Recursively calls `Task.fix_task_attributes` on every task from the vault.
    fn rewrite_vault_tasks(config: &TasksConfig, tasks: &Vaults) -> Result<()> {
        fn explore_node(node: &VaultNode, config: &TasksConfig) -> Result<()> {
            match node {
                VaultNode::Vault { content, .. } | VaultNode::Directory { content, .. } => {
                    for c in content {
                        explore_node(c, config)?;
                    }
                }
                VaultNode::File {
                    name: _, content, ..
                } => {
                    for file_entry in content {
                        match file_entry {
                            FileEntryNode::Header { content, .. } => {
                                for c in content {
                                    explore_file_entry(c, config)?;
                                }
                            }
                            FileEntryNode::Task(_) => {
                                explore_file_entry(file_entry, config)?;
                            }
                        }
                    }
                }
            }
            Ok(())
        }
        fn explore_file_entry(file_entry: &FileEntryNode, config: &TasksConfig) -> Result<()> {
            match file_entry {
                FileEntryNode::Header { content, .. } => {
                    for c in content {
                        explore_file_entry(c, config)?;
                    }
                }
                FileEntryNode::Task(task) => {
                    task.fix_task_attributes(config)?;
                    for t in &task.subtasks {
                        t.fix_task_attributes(config)?;
                    }
                }
            }
            Ok(())
        }

        tasks
            .root
            .iter()
            .try_for_each(|node| explore_node(node, config))?;
        Ok(())
    }

    /// Whether the path resolves to something that can be entered or not.
    /// Directories, Headers and Tasks with subtasks can be entered.
    #[must_use]
    pub fn can_enter(&mut self, selected_header_path: &[String]) -> bool {
        fn aux_node(node: &VaultNode, selected_header_path: &[String], path_index: usize) -> bool {
            if path_index == selected_header_path.len() {
                true
            } else {
                match node {
                    VaultNode::Vault { name, content, .. }
                    | VaultNode::Directory { name, content, .. } => {
                        if *name == selected_header_path[path_index] {
                            return content
                                .iter()
                                .any(|c| aux_node(c, selected_header_path, path_index + 1));
                        }
                    }
                    VaultNode::File { name, content, .. } => {
                        if *name == selected_header_path[path_index] {
                            return content
                                .iter()
                                .any(|c| aux_file_entry(c, selected_header_path, path_index + 1));
                        }
                    }
                }
                false
            }
        }

        fn aux_file_entry(
            file_entry: &FileEntryNode,
            selected_header_path: &[String],
            path_index: usize,
        ) -> bool {
            if path_index == selected_header_path.len() {
                true
            } else {
                match file_entry {
                    FileEntryNode::Header { name, content, .. } => {
                        if *name == selected_header_path[path_index] {
                            return content
                                .iter()
                                .any(|c| aux_file_entry(c, selected_header_path, path_index + 1));
                        }
                        false
                    }
                    FileEntryNode::Task(task) => {
                        if task.name == selected_header_path[path_index] {
                            return task.subtasks.iter().any(|t| {
                                aux_file_entry(
                                    &FileEntryNode::Task(t.clone()),
                                    selected_header_path,
                                    path_index + 1,
                                )
                            });
                        }
                        false
                    }
                }
            }
        }

        self.tasks_refactored
            .root
            .iter()
            .any(|node| aux_node(node, selected_header_path, 0))
    }
    /// Adds a new task to the vault from a raw task input string.
    /// Task will be added to the drop file configured in the `TasksConfig` or to a default drop file if none is configured.
    /// # Errors
    /// Will return an error if the task cannot be parsed or if it cannot be written to the vault.
    pub fn add_task(&mut self, mut task_input: &str, filename_opt: Option<String>) -> Result<()> {
        let path = filename_opt
            .map(|filename| self.config.core.vault_path.join(filename))
            .or_else(|| self.config.core.tasks_drop_file.clone())
            .unwrap_or_else(|| {
                warn!(
                    "No drop file configured, using default drop filename: {}",
                    Self::DROP_FILE_DEFAULT_PATH
                );
                self.config
                    .core
                    .vault_path
                    .join(Self::DROP_FILE_DEFAULT_PATH)
            });
        match parser::task::parse_task(&mut task_input, &path, &self.config) {
            Ok(task) => {
                task.fix_task_attributes(&self.config)?;
                self.reload(&self.config.clone())
            }
            Err(e) => bail!("Failed to parse task input: {e}"),
        }
    }
}
impl Display for TaskManager {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.tasks_refactored)?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashSet;

    use super::TaskManager;

    use crate::{
        Found,
        task::Task,
        vault_data::{FileEntryNode, VaultNode, Vaults},
    };

    #[test]
    fn test_get_vault_data() {
        use std::path::Path;

        // Create tasks that will be in a header
        let task1 = Task {
            name: "test task 1".to_string(),
            line_number: Some(8),
            description: Some("test\ndesc".to_string()),
            ..Default::default()
        };
        let task2 = Task {
            name: "test task 2".to_string(),
            line_number: Some(9),
            description: Some("another desc".to_string()),
            ..Default::default()
        };
        let task3 = Task {
            name: "test task 3".to_string(),
            line_number: Some(10),
            ..Default::default()
        };

        // Build a vault structure: Vault -> Directory -> File -> Headers -> Tasks
        let file_content = vec![
            FileEntryNode::Header {
                name: "1".to_string(),
                heading_level: 1,
                content: vec![FileEntryNode::Header {
                    name: "2".to_string(),
                    heading_level: 2,
                    content: vec![],
                }],
            },
            FileEntryNode::Header {
                name: "1.2".to_string(),
                heading_level: 1,
                content: vec![
                    FileEntryNode::Header {
                        name: "3".to_string(),
                        heading_level: 3,
                        content: vec![],
                    },
                    FileEntryNode::Header {
                        name: "4".to_string(),
                        heading_level: 2,
                        content: vec![
                            FileEntryNode::Task(task1.clone()),
                            FileEntryNode::Task(task2.clone()),
                            FileEntryNode::Task(task3.clone()),
                        ],
                    },
                ],
            },
        ];

        let test_file = VaultNode::File {
            name: "Test".to_string(),
            path: Path::new("test/Test.md").into(),
            content: file_content,
        };

        let test_directory = VaultNode::Directory {
            name: "test".to_string(),
            path: Path::new("test").into(),
            content: vec![test_file],
        };

        let vault_data = Vaults {
            root: vec![test_directory],
        };

        let task_mgr = TaskManager {
            tasks_refactored: vault_data,
            tags: HashSet::new(),
            ..Default::default()
        };

        // Test path to empty header "2" - should return the header FileEntry
        let path = vec![
            String::from("test"),
            String::from("Test"),
            String::from("1"),
            String::from("2"),
        ];
        let found = task_mgr.resolve_path(&path).unwrap();
        let expected_header = FileEntryNode::Header {
            name: "2".to_string(),
            heading_level: 2,
            content: vec![],
        };
        match found {
            Found::FileEntry(entry) => assert_eq!(expected_header, entry),
            _ => panic!("Expected FileEntry, got {found:?}"),
        }

        // Test path to header "4" with tasks
        let path = vec![
            String::from("test"),
            String::from("Test"),
            String::from("1.2"),
            String::from("4"),
        ];
        let found = task_mgr.resolve_path(&path).unwrap();
        let expected_header_with_tasks = FileEntryNode::Header {
            name: "4".to_string(),
            heading_level: 2,
            content: vec![
                FileEntryNode::Task(task1),
                FileEntryNode::Task(task2),
                FileEntryNode::Task(task3),
            ],
        };
        match found {
            Found::FileEntry(entry) => assert_eq!(expected_header_with_tasks, entry),
            _ => panic!("Expected FileEntry, got {found:?}"),
        }
    }
}