cleaner representation of index
This commit is contained in:
		
							
								
								
									
										19
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										19
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							@@ -234,6 +234,12 @@ dependencies = [
 | 
			
		||||
 "winapi 0.3.9",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "fixedbitset"
 | 
			
		||||
version = "0.4.2"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "fnv"
 | 
			
		||||
version = "1.0.7"
 | 
			
		||||
@@ -496,6 +502,18 @@ version = "2.1.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "petgraph"
 | 
			
		||||
version = "0.6.2"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "e6d5014253a1331579ce62aa67443b4a658c5e7dd03d4bc6d302b94474888143"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "fixedbitset",
 | 
			
		||||
 "indexmap",
 | 
			
		||||
 "serde",
 | 
			
		||||
 "serde_derive",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "proc-macro-error"
 | 
			
		||||
version = "1.0.4"
 | 
			
		||||
@@ -832,6 +850,7 @@ dependencies = [
 | 
			
		||||
 "colored",
 | 
			
		||||
 "comfy-table",
 | 
			
		||||
 "confy",
 | 
			
		||||
 "petgraph",
 | 
			
		||||
 "serde",
 | 
			
		||||
 "serde_with",
 | 
			
		||||
 "termsize",
 | 
			
		||||
 
 | 
			
		||||
@@ -13,6 +13,7 @@ clap = { version = "3.2.17", features = ["derive"] }
 | 
			
		||||
colored = "2.0.0"
 | 
			
		||||
comfy-table = "6.0.0"
 | 
			
		||||
confy = "0.4.0"
 | 
			
		||||
petgraph = { version = "0.6.2", features = ["serde_derive", "serde", "serde-1"] }
 | 
			
		||||
serde = { version = "1.0.143", features = ["derive"] }
 | 
			
		||||
serde_with = "2.0.0"
 | 
			
		||||
termsize = "0.1.6"
 | 
			
		||||
 
 | 
			
		||||
@@ -81,8 +81,8 @@ pub fn edit_raw(id : Id, vault_folder : path::PathBuf, editor : &str, state : &m
 | 
			
		||||
            }
 | 
			
		||||
            // Name change means index needs to be updated.
 | 
			
		||||
            if edited_task.data.name != task.data.name {
 | 
			
		||||
                state.index_remove(task.data.name.clone(), id);
 | 
			
		||||
                state.index_insert(edited_task.data.name.clone(), id);
 | 
			
		||||
                state.data.index.remove(task.data.name.clone(), id);
 | 
			
		||||
                state.data.index.insert(edited_task.data.name.clone(), id);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            mem::swap(&mut edited_task.data, &mut task.data);
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										94
									
								
								src/index.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										94
									
								
								src/index.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,94 @@
 | 
			
		||||
use crate::tasks;
 | 
			
		||||
use crate::error;
 | 
			
		||||
use crate::colour;
 | 
			
		||||
use crate::tasks::Id;
 | 
			
		||||
 | 
			
		||||
use std::collections::HashMap;
 | 
			
		||||
use serde_with::{serde_as, DisplayFromStr};
 | 
			
		||||
 | 
			
		||||
#[serde_as]
 | 
			
		||||
#[derive(serde::Serialize, serde::Deserialize)]
 | 
			
		||||
pub struct Index {
 | 
			
		||||
    #[serde_as(as = "HashMap<DisplayFromStr, _>")]
 | 
			
		||||
    map : HashMap<String, Vec<Id>>
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Index {
 | 
			
		||||
    pub fn create(tasks : &Vec<tasks::Task>) -> Index {
 | 
			
		||||
        let mut map : HashMap<String, Vec<Id>> = HashMap::with_capacity(tasks.len());
 | 
			
		||||
        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<_> =
 | 
			
		||||
                                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)))),
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										15
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								src/main.rs
									
									
									
									
									
								
							@@ -1,6 +1,7 @@
 | 
			
		||||
mod vcs;
 | 
			
		||||
mod edit;
 | 
			
		||||
mod vault;
 | 
			
		||||
mod index;
 | 
			
		||||
mod error;
 | 
			
		||||
mod tasks;
 | 
			
		||||
mod state;
 | 
			
		||||
@@ -252,21 +253,21 @@ fn program() -> Result<(), error::Error> {
 | 
			
		||||
                println!("Created task {} (ID: {})", colour::task_name(&task.data.name), colour::id(&task.data.id.to_string()));
 | 
			
		||||
            },
 | 
			
		||||
            Delete { id_or_name } => {
 | 
			
		||||
                let id = state.name_or_id_to_id(&id_or_name)?;
 | 
			
		||||
                let id = state.data.index.lookup(&id_or_name)?;
 | 
			
		||||
                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);
 | 
			
		||||
                state.data.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_or_name } => {
 | 
			
		||||
                let id = state.name_or_id_to_id(&id_or_name)?;
 | 
			
		||||
                let id = state.data.index.lookup(&id_or_name)?;
 | 
			
		||||
                let task = tasks::Task::load(id, vault_folder, true)?;
 | 
			
		||||
                task.display()?;
 | 
			
		||||
            },
 | 
			
		||||
            Edit { id_or_name, info } => {
 | 
			
		||||
                let id = state.name_or_id_to_id(&id_or_name)?;
 | 
			
		||||
                let id = state.data.index.lookup(&id_or_name)?;
 | 
			
		||||
                if info {
 | 
			
		||||
                    edit::edit_info(id, vault_folder.clone(), &config.editor)?;
 | 
			
		||||
                }
 | 
			
		||||
@@ -276,7 +277,7 @@ fn program() -> Result<(), error::Error> {
 | 
			
		||||
                println!("Updated task {}", colour::id(&id.to_string()));
 | 
			
		||||
            },
 | 
			
		||||
            Track { id_or_name, hours, minutes } => {
 | 
			
		||||
                let id = state.name_or_id_to_id(&id_or_name)?;
 | 
			
		||||
                let id = state.data.index.lookup(&id_or_name)?;
 | 
			
		||||
                let mut task = tasks::Task::load(id, vault_folder, false)?;
 | 
			
		||||
                let entry =  tasks::TimeEntry::new(hours, minutes);
 | 
			
		||||
                task.data.time_entries.push(entry);
 | 
			
		||||
@@ -294,14 +295,14 @@ fn program() -> Result<(), error::Error> {
 | 
			
		||||
                }
 | 
			
		||||
            },
 | 
			
		||||
            Discard { id_or_name } => {
 | 
			
		||||
                let id = state.name_or_id_to_id(&id_or_name)?;
 | 
			
		||||
                let id = state.data.index.lookup(&id_or_name)?;
 | 
			
		||||
                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_or_name } => {
 | 
			
		||||
                let id = state.name_or_id_to_id(&id_or_name)?;
 | 
			
		||||
                let id = state.data.index.lookup(&id_or_name)?;
 | 
			
		||||
                let mut task = tasks::Task::load(id, vault_folder, false)?;
 | 
			
		||||
                task.data.completed = Some(chrono::Local::now().naive_local());
 | 
			
		||||
                task.save()?;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										80
									
								
								src/state.rs
									
									
									
									
									
								
							
							
						
						
									
										80
									
								
								src/state.rs
									
									
									
									
									
								
							@@ -1,29 +1,23 @@
 | 
			
		||||
use crate::error;
 | 
			
		||||
use crate::tasks;
 | 
			
		||||
use crate::colour;
 | 
			
		||||
use crate::index;
 | 
			
		||||
use crate::tasks::Id;
 | 
			
		||||
 | 
			
		||||
use std::fs;
 | 
			
		||||
use std::path;
 | 
			
		||||
use std::io;
 | 
			
		||||
use std::io::{Write, Seek};
 | 
			
		||||
use std::collections::HashMap;
 | 
			
		||||
 | 
			
		||||
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>>,
 | 
			
		||||
    //#[serde_as(as = "HashMap<DisplayFromStr, _>")]
 | 
			
		||||
    //pub dependencies : HashMap<Id, Vec<Id>>,
 | 
			
		||||
    pub index : index::Index,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl State {
 | 
			
		||||
@@ -60,19 +54,8 @@ 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 index = index::Index::create(&tasks);
 | 
			
		||||
 | 
			
		||||
            let data = InternalState {
 | 
			
		||||
                next_id : u64::try_from(max_id + 1).unwrap(),
 | 
			
		||||
@@ -115,61 +98,4 @@ 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);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    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)))),
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -119,7 +119,7 @@ impl Task {
 | 
			
		||||
        file.seek(io::SeekFrom::Start(0))?;
 | 
			
		||||
        file.write_all(file_contents.as_bytes())?;
 | 
			
		||||
 | 
			
		||||
        state.index_insert(data.name.clone(), id);
 | 
			
		||||
        state.data.index.insert(data.name.clone(), id);
 | 
			
		||||
 | 
			
		||||
        Ok(Task {
 | 
			
		||||
            path,
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user