name to id index and validation on names

This commit is contained in:
aaron-jack-manning
2022-08-21 16:43:42 +10:00
parent 4d1093b50b
commit cd0dce4168
8 changed files with 242 additions and 54 deletions

View File

@@ -5,6 +5,7 @@ use std::process;
use crate::tasks;
use crate::error;
use crate::state;
use crate::tasks::Id;
pub fn open_editor(path : &path::Path, editor : &str) -> Result<process::ExitStatus, error::Error> {
@@ -15,15 +16,15 @@ pub fn open_editor(path : &path::Path, editor : &str) -> Result<process::ExitSta
let mut child = command.spawn()?;
child.wait().map_err(|err| error::Error::from(err))
child.wait().map_err(error::Error::from)
}
pub fn edit_info(id : Id, vault_folder : path::PathBuf, editor : &str) -> Result<(), error::Error> {
let mut task = tasks::Task::load(id, vault_folder.clone(), false)?;
let mut task = tasks::Task::load(id, &vault_folder, false)?;
let temp_path = vault_folder.join("temp.md");
fs::write(&temp_path, &task.data.info.unwrap_or(String::new()).as_bytes())?;
fs::write(&temp_path, &task.data.info.unwrap_or_default().as_bytes())?;
let status = open_editor(&temp_path, editor)?;
@@ -49,9 +50,9 @@ pub fn edit_info(id : Id, vault_folder : path::PathBuf, editor : &str) -> Result
}
}
pub fn edit_raw(id : Id, vault_folder : path::PathBuf, editor : &str) -> Result<(), error::Error> {
pub fn edit_raw(id : Id, vault_folder : path::PathBuf, editor : &str, state : &mut state::State) -> Result<(), error::Error> {
let mut task = tasks::Task::load(id, vault_folder.clone(), false)?;
let mut task = tasks::Task::load(id, &vault_folder, false)?;
let temp_path = vault_folder.join("temp.toml");
@@ -66,19 +67,22 @@ pub fn edit_raw(id : Id, vault_folder : path::PathBuf, editor : &str) -> Result<
}
}
else {
let file_contents = fs::read_to_string(&temp_path)?;
let mut edited_task = tasks::Task::load_direct(temp_path.clone(), true)?;
if edited_task.data.id != task.data.id {
Err(error::Error::Generic(String::from("You cannot change the ID of a task in a direct edit")))
}
else if edited_task.data.name.chars().all(|c| c.is_numeric()) {
Err(error::Error::Generic(String::from("Name must not be purely numeric")))
}
else {
if edited_task.data.dependencies != task.data.dependencies {
// This is where the other dependencies graph needs to be updated.
}
// Name change means index needs to be updated.
if edited_task.data.name != task.data.name {
// This is where the hashmap from id to string needs to be updated.
state.index_remove(task.data.name.clone(), id);
state.index_insert(edited_task.data.name.clone(), id);
}
mem::swap(&mut edited_task.data, &mut task.data);