2022-08-25 10:44:22 +10:00
|
|
|
use crate::tasks;
|
|
|
|
|
use crate::error;
|
2022-08-30 19:47:23 +10:00
|
|
|
use crate::format;
|
2022-08-25 10:44:22 +10:00
|
|
|
use crate::tasks::Id;
|
|
|
|
|
|
2022-08-25 16:40:56 +10:00
|
|
|
use std::fmt::Write;
|
2022-09-07 20:26:20 +10:00
|
|
|
use std::collections::BTreeMap;
|
2022-08-25 10:44:22 +10:00
|
|
|
use serde_with::{serde_as, DisplayFromStr};
|
|
|
|
|
|
|
|
|
|
#[serde_as]
|
|
|
|
|
#[derive(serde::Serialize, serde::Deserialize)]
|
|
|
|
|
pub struct Index {
|
2022-09-07 20:26:20 +10:00
|
|
|
#[serde_as(as = "BTreeMap<DisplayFromStr, _>")]
|
|
|
|
|
map : BTreeMap<String, Vec<Id>>
|
2022-08-25 10:44:22 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Index {
|
|
|
|
|
pub fn create(tasks : &Vec<tasks::Task>) -> Index {
|
2022-09-07 20:26:20 +10:00
|
|
|
let mut map : BTreeMap<String, Vec<Id>> = BTreeMap::new();
|
2022-08-25 10:44:22 +10:00
|
|
|
for task in tasks {
|
|
|
|
|
match map.get_mut(&task.data.name) {
|
|
|
|
|
Some(ids) => {
|
|
|
|
|
ids.push(task.data.id);
|
|
|
|
|
},
|
|
|
|
|
None => {
|
|
|
|
|
map.insert(task.data.name.clone(), vec![task.data.id]);
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Self {
|
|
|
|
|
map
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn insert(&mut self, name : String, id : Id) {
|
|
|
|
|
match self.map.get_mut(&name) {
|
|
|
|
|
Some(ids) => {
|
|
|
|
|
ids.push(id);
|
|
|
|
|
},
|
|
|
|
|
None => {
|
|
|
|
|
self.map.insert(name, vec![id]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn remove(&mut self, name : String, id : Id) {
|
|
|
|
|
if let Some(mut ids) = self.map.remove(&name) {
|
|
|
|
|
if let Some(index) = ids.iter().position(|i| i == &id) {
|
|
|
|
|
ids.swap_remove(index);
|
|
|
|
|
|
|
|
|
|
if !ids.is_empty() {
|
|
|
|
|
self.map.insert(name, ids);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn lookup(&self, name_or_id : &String) -> Result<Id, error::Error> {
|
|
|
|
|
match name_or_id.parse::<Id>() {
|
|
|
|
|
Ok(id) => Ok(id),
|
|
|
|
|
Err(_) => {
|
|
|
|
|
let name = name_or_id;
|
|
|
|
|
match self.map.get(name) {
|
|
|
|
|
Some(ids) => {
|
|
|
|
|
if ids.len() == 1 {
|
|
|
|
|
Ok(ids[0])
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
let coloured_ids : Vec<_> =
|
2022-08-25 16:40:56 +10:00
|
|
|
ids.iter()
|
2022-08-30 19:47:23 +10:00
|
|
|
.map(|i| format::id(*i))
|
2022-08-25 10:44:22 +10:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
let mut display_ids = String::new();
|
|
|
|
|
|
|
|
|
|
for id in coloured_ids {
|
2022-08-25 16:40:56 +10:00
|
|
|
write!(&mut display_ids, "{}, ", id).unwrap();
|
2022-08-25 10:44:22 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !display_ids.is_empty() {
|
|
|
|
|
display_ids.pop();
|
|
|
|
|
display_ids.pop();
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-30 19:47:23 +10:00
|
|
|
Err(error::Error::Generic(format!("Multiple tasks (Ids: [{}]) by that name exist", display_ids)))
|
2022-08-25 10:44:22 +10:00
|
|
|
}
|
|
|
|
|
},
|
2022-08-30 19:47:23 +10:00
|
|
|
None => Err(error::Error::Generic(format!("A note by the name {} does not exist", format::task(name)))),
|
2022-08-25 10:44:22 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|