option to edit, delete, view, etc with name

This commit is contained in:
aaron-jack-manning
2022-08-21 17:14:26 +10:00
parent cd0dce4168
commit 1a136a8966
3 changed files with 51 additions and 13 deletions

View File

@@ -1,5 +1,6 @@
use crate::error;
use crate::tasks;
use crate::colour;
use crate::tasks::Id;
use std::fs;
@@ -128,4 +129,39 @@ impl State {
}
}
}
pub fn name_or_id_to_id(&self, name : &String) -> Result<Id, error::Error> {
match name.parse::<Id>() {
Ok(id) => Ok(id),
Err(_) => {
match self.data.index.get(name) {
Some(ids) => {
if ids.len() == 1 {
Ok(ids[0])
}
else {
let coloured_ids : Vec<_> =
ids.into_iter()
.map(|i| colour::id(&i.to_string()))
.collect();
let mut display_ids = String::new();
for id in coloured_ids {
display_ids.push_str(&format!("{}, ", id));
}
if !display_ids.is_empty() {
display_ids.pop();
display_ids.pop();
}
Err(error::Error::Generic(format!("Multiple notes (Ids: [{}]) by that name exist", display_ids)))
}
},
None => Err(error::Error::Generic(format!("A note by the name {} does not exist", colour::task_name(&name)))),
}
}
}
}
}