vault_tasks_core/lib.rs
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
use color_eyre::{eyre::bail, Result};
use serde::Deserialize;
use std::{collections::HashSet, fmt::Display, path::PathBuf};
use vault_data::VaultData;
use filter::{filter, Filter};
use tracing::error;
use vault_parser::VaultParser;
pub mod filter;
pub mod parser;
pub mod sorter;
pub mod task;
pub mod vault_data;
mod vault_parser;
#[derive(Clone, Debug, Deserialize, Default)]
pub struct TasksConfig {
#[serde(default)]
pub parse_dot_files: bool,
#[serde(default)]
pub file_tags_propagation: bool,
#[serde(default)]
pub ignored: Vec<PathBuf>,
#[serde(default)]
pub indent_length: usize,
#[serde(default)]
pub use_american_format: bool,
#[serde(default)]
pub show_relative_due_dates: bool,
#[serde(default)]
pub vault_path: PathBuf,
#[serde(default)]
pub explorer_default_search_string: String,
#[serde(default)]
pub filter_default_search_string: String,
#[serde(default)]
pub task_state_marker: char,
}
pub struct TaskManager {
pub tasks: VaultData,
pub tags: HashSet<String>,
pub current_filter: Option<Filter>,
}
impl Default for TaskManager {
fn default() -> Self {
Self {
tasks: VaultData::Directory("Empty Vault".to_owned(), vec![]),
tags: HashSet::new(),
current_filter: None,
}
}
}
impl TaskManager {
/// 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<()> {
let vault_parser = VaultParser::new(config.clone());
let tasks = vault_parser.scan_vault()?;
Self::rewrite_vault_tasks(config, &tasks)
.unwrap_or_else(|e| error!("Failed to fix tasks: {e}"));
let mut tags = HashSet::new();
Self::collect_tags(&tasks, &mut tags);
self.tasks = tasks;
self.tags = tags;
Ok(())
}
/// Explores the vault and fills a `&mut HashSet<String>` with every tags found.
pub fn collect_tags(tasks: &VaultData, tags: &mut HashSet<String>) {
match tasks {
VaultData::Directory(_, children) | VaultData::Header(_, _, children) => {
children.iter().for_each(|c| Self::collect_tags(c, tags));
}
VaultData::Task(task) => {
task.tags.clone().unwrap_or_default().iter().for_each(|t| {
tags.insert(t.clone());
});
task.subtasks
.iter()
.for_each(|task| Self::collect_tags(&VaultData::Task(task.clone()), tags));
}
}
}
/// Follows a path and returns every `VaultData` that are on the target layer, discarding every children.
///
/// # Errors
///
/// This function will return an error if the path can't be resolved.
pub fn get_path_layer_entries(&self, path: &[String]) -> Result<Vec<VaultData>> {
Ok(self
.get_explorer_entries(path)?
.iter()
.map(|vd| match vd {
VaultData::Directory(name, _) => VaultData::Directory(name.clone(), vec![]),
VaultData::Header(level, name, _) => {
VaultData::Header(*level, name.clone(), vec![])
}
VaultData::Task(t) => {
let mut t = t.clone();
t.subtasks = vec![];
VaultData::Task(t)
}
})
.collect::<Vec<VaultData>>())
}
/// Recursively calls `Task.fix_task_attributes` on every task from the vault.
fn rewrite_vault_tasks(config: &TasksConfig, tasks: &VaultData) -> Result<()> {
fn explore_tasks_rec(
config: &TasksConfig,
filename: &mut PathBuf,
file_entry: &VaultData,
) -> Result<()> {
match file_entry {
VaultData::Header(_, _, children) => {
children
.iter()
.try_for_each(|c| explore_tasks_rec(config, filename, c))?;
}
VaultData::Task(task) => {
task.fix_task_attributes(config, filename)?;
task.subtasks
.iter()
.try_for_each(|t| t.fix_task_attributes(config, filename))?;
}
VaultData::Directory(dir_name, children) => {
let mut filename = filename.clone();
filename.push(dir_name);
children
.iter()
.try_for_each(|c| explore_tasks_rec(config, &mut filename.clone(), c))?;
}
}
Ok(())
}
explore_tasks_rec(config, &mut PathBuf::new(), tasks)
}
/// Follows the `selected_header_path` to retrieve the correct `VaultData`.
/// Then returns every `VaultData` objects on the same layer.
///
/// # Errors
/// Will return an error if the vault is empty or the first layer is not a `VaultData::Directory`
pub fn get_explorer_entries(&self, selected_header_path: &[String]) -> Result<Vec<VaultData>> {
fn aux(
file_entry: Vec<VaultData>,
selected_header_path: &[String],
path_index: usize,
) -> Result<Vec<VaultData>> {
if path_index == selected_header_path.len() {
Ok(file_entry)
} else {
for entry in file_entry {
match entry {
VaultData::Directory(name, children)
| VaultData::Header(_, name, children) => {
if name == selected_header_path[path_index] {
return aux(children, selected_header_path, path_index + 1);
}
}
VaultData::Task(task) => {
if task.name == selected_header_path[path_index] {
return aux(
task.subtasks
.iter()
.map(|t| VaultData::Task(t.clone()))
.collect(),
selected_header_path,
path_index + 1,
);
}
}
}
}
bail!("Couldn't find corresponding entry");
}
}
let filtered_tasks = if let Some(task_filter) = &self.current_filter {
filter(&self.tasks, task_filter)
} else {
Some(self.tasks.clone())
};
match filtered_tasks {
Some(VaultData::Directory(_, entries)) => aux(entries, selected_header_path, 0),
None => bail!("Empty Vault"),
_ => {
error!("First layer of VaultData was not a Directory");
bail!("First layer of VaultData was not a Directory")
}
}
}
/// Follows the `selected_header_path` to retrieve the correct `VaultData`.
/// Returns a vector of `VaultData` with the items to display in TUI, preserving the recursive nature.
/// `task_preview_offset`: add offset to return a task instead of one of its subtasks
///
/// # Errors
/// Will return an error if
/// - vault is empty or the first layer is not a `VaultData::Directory`
/// - the path can't be resolved in the vault data
pub fn get_vault_data_from_path(
&self,
selected_header_path: &[String],
task_preview_offset: usize,
) -> Result<Vec<VaultData>> {
fn aux(
file_entry: VaultData,
selected_header_path: &[String],
path_index: usize,
task_preview_offset: usize,
) -> Result<Vec<VaultData>> {
if path_index == selected_header_path.len() {
Ok(vec![file_entry])
} else {
match file_entry {
VaultData::Directory(name, children) | VaultData::Header(_, name, children) => {
if name == selected_header_path[path_index] {
let mut res = vec![];
for child in children {
if let Ok(mut found) = aux(
child,
selected_header_path,
path_index + 1,
task_preview_offset,
) {
res.append(&mut found);
}
}
Ok(res)
} else {
bail!("Couldn't find corresponding entry");
}
}
VaultData::Task(task) => {
if task.name == selected_header_path[path_index] {
let mut res = vec![];
if path_index + task_preview_offset == selected_header_path.len() {
res.push(VaultData::Task(task));
} else {
for child in task.subtasks {
if let Ok(mut found) = aux(
VaultData::Task(child),
selected_header_path,
path_index + 1,
task_preview_offset,
) {
res.append(&mut found);
}
}
}
Ok(res)
} else {
bail!("Couldn't find corresponding entry");
}
}
}
}
}
let filtered_tasks = if let Some(task_filter) = &self.current_filter {
filter(&self.tasks, task_filter)
} else {
Some(self.tasks.clone())
};
match filtered_tasks {
Some(VaultData::Directory(_, entries)) => {
for entry in entries {
if let Ok(res) = aux(entry, selected_header_path, 0, task_preview_offset) {
return Ok(res);
}
}
error!("Vault was not empty but the entry was not found");
bail!("Vault was not empty but the entry was not found");
}
None => bail!("Empty Vault"),
_ => {
error!("First layer of VaultData was not a Directory");
bail!("Empty Vault")
}
}
}
/// Whether the path resolves to something that can be entered or not.
/// Directories, Headers and Tasks with subtasks can be entered.
pub fn can_enter(&self, selected_header_path: &[String]) -> bool {
fn aux(file_entry: VaultData, selected_header_path: &[String], path_index: usize) -> bool {
if path_index == selected_header_path.len() {
true
} else {
match file_entry {
VaultData::Directory(name, children) | VaultData::Header(_, name, children) => {
if name == selected_header_path[path_index] {
return children
.iter()
.any(|c| aux(c.clone(), selected_header_path, path_index + 1));
}
false
}
VaultData::Task(task) => {
if task.name == selected_header_path[path_index] {
return task.subtasks.iter().any(|t| {
aux(
VaultData::Task(t.clone()),
selected_header_path,
path_index + 1,
)
});
}
false
}
}
}
}
let filtered_tasks = if let Some(task_filter) = &self.current_filter {
filter(&self.tasks, task_filter)
} else {
return false;
};
let Some(VaultData::Directory(_, entries)) = filtered_tasks else {
return false;
};
entries
.iter()
.any(|e| aux(e.clone(), selected_header_path, 0))
}
}
impl Display for TaskManager {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.tasks)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use std::collections::HashSet;
use super::TaskManager;
use crate::{task::Task, vault_data::VaultData};
#[test]
fn test_get_vault_data() {
let expected_tasks = vec![
VaultData::Task(Task {
name: "test".to_string(),
line_number: 8,
description: Some("test\ndesc".to_string()),
..Default::default()
}),
VaultData::Task(Task {
name: "test".to_string(),
line_number: 8,
description: Some("test\ndesc".to_string()),
..Default::default()
}),
VaultData::Task(Task {
name: "test".to_string(),
line_number: 8,
description: Some("test\ndesc".to_string()),
..Default::default()
}),
];
let expected_header = VaultData::Header(3, "3".to_string(), expected_tasks.clone());
let input = VaultData::Directory(
"test".to_owned(),
vec![VaultData::Header(
0,
"Test".to_string(),
vec![
VaultData::Header(
1,
"1".to_string(),
vec![VaultData::Header(
2,
"2".to_string(),
vec![expected_header.clone()],
)],
),
VaultData::Header(
1,
"1.2".to_string(),
vec![
VaultData::Header(3, "3".to_string(), vec![]),
VaultData::Header(
2,
"4".to_string(),
vec![VaultData::Task(Task {
name: "test".to_string(),
line_number: 8,
description: Some("test\ndesc".to_string()),
..Default::default()
})],
),
],
),
],
)],
);
let task_mgr = TaskManager {
tasks: input,
tags: HashSet::new(),
..Default::default()
};
let path = vec![String::from("Test"), String::from("1"), String::from("2")];
let res = task_mgr.get_vault_data_from_path(&path, 0).unwrap();
assert_eq!(vec![expected_header], res);
let path = vec![
String::from("Test"),
String::from("1"),
String::from("2"),
String::from("3"),
];
let res = task_mgr.get_vault_data_from_path(&path, 0).unwrap();
assert_eq!(expected_tasks, res);
}
}