name to id index and validation on names
This commit is contained in:
20
src/edit.rs
20
src/edit.rs
@ -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);
|
||||
|
18
src/main.rs
18
src/main.rs
@ -1,4 +1,4 @@
|
||||
#![allow(dead_code, unused_variables)]
|
||||
//#![allow(dead_code, unused_variables)]
|
||||
|
||||
mod git;
|
||||
mod edit;
|
||||
@ -164,11 +164,15 @@ fn program() -> Result<(), error::Error> {
|
||||
println!("Created task {} (ID: {})", colour::task_name(&task.data.name), colour::id(&task.data.id.to_string()));
|
||||
},
|
||||
Delete { id } => {
|
||||
tasks::Task::delete_by_id(id, vault_folder)?;
|
||||
println!("Deleted task {}", colour::id(&id.to_string()));
|
||||
let task = tasks::Task::load(id, vault_folder, false)?;
|
||||
let name = task.data.name.clone();
|
||||
state.index_remove(task.data.name.clone(), task.data.id);
|
||||
task.delete()?;
|
||||
|
||||
println!("Deleted task {} (ID: {})", colour::task_name(&name), colour::id(&id.to_string()));
|
||||
},
|
||||
View { id } => {
|
||||
let task = tasks::Task::load(id, vault_folder.clone(), true)?;
|
||||
let task = tasks::Task::load(id, vault_folder, true)?;
|
||||
task.display()?;
|
||||
},
|
||||
Edit { id, info } => {
|
||||
@ -176,18 +180,18 @@ fn program() -> Result<(), error::Error> {
|
||||
edit::edit_info(id, vault_folder.clone(), "nvim")?;
|
||||
}
|
||||
else {
|
||||
edit::edit_raw(id, vault_folder.clone(), "nvim")?;
|
||||
edit::edit_raw(id, vault_folder.clone(), "nvim", &mut state)?;
|
||||
}
|
||||
println!("Updated task {}", colour::id(&id.to_string()));
|
||||
},
|
||||
Discard { id } => {
|
||||
let mut task = tasks::Task::load(id, vault_folder.clone(), false)?;
|
||||
let mut task = tasks::Task::load(id, vault_folder, false)?;
|
||||
task.data.discarded = true;
|
||||
task.save()?;
|
||||
println!("Discarded task {}", colour::id(&id.to_string()));
|
||||
},
|
||||
Complete { id } => {
|
||||
let mut task = tasks::Task::load(id, vault_folder.clone(), false)?;
|
||||
let mut task = tasks::Task::load(id, vault_folder, false)?;
|
||||
task.data.complete = true;
|
||||
task.save()?;
|
||||
println!("Marked task {} as complete", colour::id(&id.to_string()));
|
||||
|
51
src/state.rs
51
src/state.rs
@ -1,19 +1,26 @@
|
||||
use crate::error;
|
||||
use crate::tasks;
|
||||
use crate::tasks::Id;
|
||||
|
||||
use std::fs;
|
||||
use std::path;
|
||||
use std::io;
|
||||
use std::io::{Write, Seek};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::error;
|
||||
use crate::tasks::Id;
|
||||
use serde_with::{serde_as, DisplayFromStr};
|
||||
|
||||
pub struct State {
|
||||
file : fs::File,
|
||||
pub data : InternalState,
|
||||
}
|
||||
|
||||
#[serde_as]
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
pub struct InternalState {
|
||||
pub next_id : Id,
|
||||
#[serde_as(as = "HashMap<DisplayFromStr, _>")]
|
||||
pub index : HashMap<String, Vec<Id>>,
|
||||
}
|
||||
|
||||
impl State {
|
||||
@ -39,8 +46,8 @@ impl State {
|
||||
}
|
||||
else {
|
||||
|
||||
// Calculating the next ID if necessary.
|
||||
let mut max_id : i128 = -1;
|
||||
|
||||
for id in vault_location.join("notes").read_dir()?.filter_map(|p| p.ok()).map(|p| p.path()).filter(|p| p.extension().map(|s| s.to_str()) == Some(Some("toml"))).filter_map(|p| p.file_stem().map(|x| x.to_str().map(|y| y.to_string()))).flatten().filter_map(|p| p.parse::<Id>().ok()) {
|
||||
|
||||
if i128::try_from(id).unwrap() > max_id {
|
||||
@ -48,8 +55,23 @@ impl State {
|
||||
}
|
||||
}
|
||||
|
||||
// Calculating out the index.
|
||||
let tasks = tasks::Task::load_all(vault_location, true)?;
|
||||
let mut index : HashMap<String, Vec<Id>> = HashMap::with_capacity(tasks.len());
|
||||
for task in tasks {
|
||||
match index.get_mut(&task.data.name) {
|
||||
Some(ids) => {
|
||||
ids.push(task.data.id);
|
||||
},
|
||||
None => {
|
||||
index.insert(task.data.name.clone(), vec![task.data.id]);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
let data = InternalState {
|
||||
next_id : u64::try_from(max_id + 1).unwrap(),
|
||||
index,
|
||||
};
|
||||
|
||||
let mut file = fs::File::options()
|
||||
@ -83,4 +105,27 @@ impl State {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn index_insert(&mut self, name : String, id : Id) {
|
||||
match self.data.index.get_mut(&name) {
|
||||
Some(ids) => {
|
||||
ids.push(id);
|
||||
},
|
||||
None => {
|
||||
self.data.index.insert(name, vec![id]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn index_remove(&mut self, name : String, id : Id) {
|
||||
if let Some(mut ids) = self.data.index.remove(&name) {
|
||||
if let Some(index) = ids.iter().position(|i| i == &id) {
|
||||
ids.swap_remove(index);
|
||||
|
||||
if !ids.is_empty() {
|
||||
self.data.index.insert(name, ids);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
67
src/tasks.rs
67
src/tasks.rs
@ -75,6 +75,10 @@ pub struct InternalTask {
|
||||
impl Task {
|
||||
pub fn new(name : String, info : Option<String>, tags : Vec<String>, dependencies : Vec<Id>, priority : Option<Priority>, vault_folder : &path::Path, state : &mut state::State) -> Result<Self, error::Error> {
|
||||
|
||||
if name.chars().all(|c| c.is_numeric()) {
|
||||
return Err(error::Error::Generic(String::from("Name must not be purely numeric")));
|
||||
};
|
||||
|
||||
let id = state.data.next_id;
|
||||
state.data.next_id += 1;
|
||||
|
||||
@ -98,11 +102,12 @@ impl Task {
|
||||
discarded : false,
|
||||
};
|
||||
|
||||
|
||||
file.set_len(0)?;
|
||||
file.seek(io::SeekFrom::Start(0))?;
|
||||
file.write_all(toml::to_string(&data)?.as_bytes())?;
|
||||
|
||||
state.index_insert(data.name.clone(), id);
|
||||
|
||||
Ok(Task {
|
||||
path,
|
||||
file,
|
||||
@ -136,12 +141,30 @@ impl Task {
|
||||
|
||||
/// The read_only flag is so that the file will not be truncated, and therefore doesn't need to
|
||||
/// be saved when finished.
|
||||
pub fn load(id : Id, vault_folder : path::PathBuf, read_only : bool) -> Result<Self, error::Error> {
|
||||
let path = Task::check_exists(id, &vault_folder)?;
|
||||
pub fn load(id : Id, vault_folder : &path::Path, read_only : bool) -> Result<Self, error::Error> {
|
||||
let path = Task::check_exists(id, vault_folder)?;
|
||||
|
||||
Task::load_direct(path, read_only)
|
||||
}
|
||||
|
||||
pub fn load_all(vault_folder : &path::Path, read_only : bool) -> Result<Vec<Self>, error::Error> {
|
||||
let ids : Vec<Id> =
|
||||
fs::read_dir(vault_folder.join("notes"))
|
||||
.unwrap()
|
||||
.map(|entry| entry.unwrap().path())
|
||||
.filter(|p| p.is_file())
|
||||
.map(|p| p.file_stem().unwrap().to_str().unwrap().to_string())
|
||||
.filter_map(|n| n.parse::<Id>().ok())
|
||||
.collect();
|
||||
|
||||
let mut tasks = Vec::with_capacity(ids.len());
|
||||
for id in ids {
|
||||
tasks.push(Task::load(id, vault_folder, read_only)?);
|
||||
}
|
||||
|
||||
Ok(tasks)
|
||||
}
|
||||
|
||||
pub fn path(&self) -> &path::Path {
|
||||
&self.path
|
||||
}
|
||||
@ -158,7 +181,7 @@ impl Task {
|
||||
|
||||
pub fn save(self) -> Result<(), error::Error> {
|
||||
let Self {
|
||||
path,
|
||||
path : _,
|
||||
mut file,
|
||||
data,
|
||||
} = self;
|
||||
@ -174,7 +197,7 @@ impl Task {
|
||||
let Self {
|
||||
path,
|
||||
file,
|
||||
data,
|
||||
data : _,
|
||||
} = self;
|
||||
|
||||
mem::drop(file);
|
||||
@ -183,12 +206,6 @@ impl Task {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn delete_by_id(id : Id, vault_folder : &path::Path) -> Result<(), error::Error> {
|
||||
let path = Task::check_exists(id, vault_folder)?;
|
||||
fs::remove_file(&path)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn display(&self) -> Result<(), error::Error> {
|
||||
|
||||
fn line(len : usize) {
|
||||
@ -200,7 +217,7 @@ impl Task {
|
||||
|
||||
let id = &self.data.id.to_string();
|
||||
let discarded = if self.data.discarded { String::from(" (discarded)") } else { String::new() };
|
||||
let heading = format!("[{}] {} {}{}", if self.data.complete {"X"} else {" "}, colour::id(&id), colour::task_name(&self.data.name), colour::greyed_out(&discarded));
|
||||
let heading = format!("[{}] {} {}{}", if self.data.complete {"X"} else {" "}, colour::id(id), colour::task_name(&self.data.name), colour::greyed_out(&discarded));
|
||||
println!("{}", heading);
|
||||
|
||||
line(5 + self.data.name.chars().count() + id.chars().count() + discarded.chars().count());
|
||||
@ -212,11 +229,11 @@ impl Task {
|
||||
let mut max_line_width = 0;
|
||||
println!("Info:");
|
||||
|
||||
while info.ends_with("\n") {
|
||||
while info.ends_with('\n') {
|
||||
info.pop();
|
||||
}
|
||||
|
||||
let info_lines : Vec<&str> = info.split("\n").collect();
|
||||
let info_lines : Vec<&str> = info.split('\n').collect();
|
||||
for line in info_lines {
|
||||
max_line_width = usize::max(max_line_width, line.chars().count() + 4);
|
||||
println!(" {}", line);
|
||||
@ -240,7 +257,8 @@ fn format_hash_set<T : fmt::Display>(set : &HashSet<T>) -> Result<String, error:
|
||||
fmt::write(&mut output, format_args!("{}, ", value))?;
|
||||
}
|
||||
|
||||
if output.len() != 0 {
|
||||
// Remove the trailing comma and space.
|
||||
if !output.is_empty() {
|
||||
output.pop();
|
||||
output.pop();
|
||||
}
|
||||
@ -249,30 +267,15 @@ fn format_hash_set<T : fmt::Display>(set : &HashSet<T>) -> Result<String, error:
|
||||
}
|
||||
|
||||
pub fn list(vault_folder : &path::Path) -> Result<(), error::Error> {
|
||||
let ids : Vec<Id> =
|
||||
fs::read_dir(vault_folder.join("notes"))
|
||||
.unwrap()
|
||||
.map(|entry| entry.unwrap().path())
|
||||
.filter(|p| p.is_file())
|
||||
.map(|p| p.file_stem().unwrap().to_str().unwrap().to_string())
|
||||
.filter_map(|n| n.parse::<Id>().ok())
|
||||
.collect();
|
||||
|
||||
let mut table = comfy_table::Table::new();
|
||||
|
||||
table
|
||||
.load_preset(comfy_table::presets::UTF8_FULL)
|
||||
.apply_modifier(comfy_table::modifiers::UTF8_ROUND_CORNERS)
|
||||
.set_content_arrangement(comfy_table::ContentArrangement::Dynamic);
|
||||
|
||||
table.set_header(vec!["Id", "Name", "Tags", "Priority"]);
|
||||
|
||||
let mut tasks = Vec::with_capacity(ids.len());
|
||||
|
||||
for id in ids {
|
||||
tasks.push(Task::load(id, vault_folder.to_path_buf(), true)?);
|
||||
}
|
||||
|
||||
let mut tasks = Task::load_all(vault_folder, true)?;
|
||||
tasks.sort_by(|t1, t2| t2.data.priority.cmp(&t1.data.priority));
|
||||
|
||||
for task in tasks {
|
||||
@ -282,7 +285,7 @@ pub fn list(vault_folder : &path::Path) -> Result<(), error::Error> {
|
||||
task.data.id.to_string(),
|
||||
task.data.name,
|
||||
format_hash_set(&task.data.tags)?,
|
||||
task.data.priority.to_string()
|
||||
task.data.priority.to_string(),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
@ -10,8 +10,7 @@ pub fn new(name : String, path : path::PathBuf, config : &mut config::Config) ->
|
||||
|
||||
fn create_all_metadata(path : &path::Path) -> Result<(), error::Error> {
|
||||
fs::create_dir(path.join("notes"))?;
|
||||
let state = state::State::load(path)?;
|
||||
//state.save()?;
|
||||
let _ = state::State::load(path)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user