From d034fc34773599b194958da234fa2c6ed1be530e Mon Sep 17 00:00:00 2001 From: nimaaskarian Date: Sat, 27 Jan 2024 00:44:30 +0330 Subject: [PATCH] Removed unnecessary path in dependency methods --- src/todo_list.rs | 5 ++- src/todo_list/todo.rs | 80 ++++++++++++++++++------------------------- src/tui/app.rs | 19 +++++----- 3 files changed, 44 insertions(+), 60 deletions(-) diff --git a/src/todo_list.rs b/src/todo_list.rs index e999f66..d221cf1 100644 --- a/src/todo_list.rs +++ b/src/todo_list.rs @@ -317,12 +317,11 @@ impl TodoList { } #[inline] - fn remove_dependencies(&mut self, path: &PathBuf) -> io::Result<()> { + fn remove_dependencies(&mut self) { let mut todos = [&mut self.undone.todos, &mut self.done.todos]; for todo in todos.iter_mut().flat_map(|v| v.iter_mut()) { - todo.remove_dependency(path); + todo.remove_dependency(); } - Ok(()) } #[inline] diff --git a/src/todo_list/todo.rs b/src/todo_list/todo.rs index ce521eb..557d5a6 100644 --- a/src/todo_list/todo.rs +++ b/src/todo_list/todo.rs @@ -31,7 +31,7 @@ pub struct Todo { dependency_name: String, dependency_type: DependencyType, done:bool, - removed_files: Vec, + removed_names: Vec, daily: bool, date_str: String, } @@ -137,7 +137,7 @@ impl TryFrom<&str> for Todo { dependency_type, date_str, daily, - removed_files: Vec::new(), + removed_names: Vec::new(), dependency_name, dependencies, message, @@ -160,7 +160,7 @@ impl Todo { dependency_type: DependencyType::None, date_str: String::new(), daily: false, - removed_files: Vec::new(), + removed_names: Vec::new(), dependency_name: String::new(), dependencies: TodoList::new(), message, @@ -183,20 +183,10 @@ impl Todo { format!("{name}.todo") } - // pub fn dependency_path(&self) -> Option { - // note_path(&self.dependency_name, self.dependency_dir()).unwrap() - // } - - pub fn remove_note(&mut self, path: &PathBuf) { - // if let Some(path) = self.dependency_path() { - // self.removed_files.push(path); - // } - // if !self.dependency_name.is_empty() { + pub fn remove_note(&mut self) { if self.dependency_type == DependencyType::Note { - self.remove_dependency(path); - // self.removed_files.push(path.join(name)); + self.remove_dependency(); } - self.dependency_type = DependencyType::None; } #[inline] @@ -215,13 +205,13 @@ impl Todo { } #[inline] - pub fn add_dependency(&mut self, path: &PathBuf) -> Result<(), TodoError>{ - if self.has_dependency() { + pub fn add_todo_dependency(&mut self) -> Result<(), TodoError>{ + if self.has_todo_dependency() { return Err(TodoError::AlreadyExists) } - if let Some(path) = self.dependency_path(path) { - let _ = self.remove_note(&path); - } + // if let Some(path) = self.dependency_path(path) { + // let _ = self.remove_note(&path); + // } self.dependency_name = Self::todolist_name(&self.hash()); self.dependency_type = DependencyType::TodoList; @@ -245,14 +235,14 @@ impl Todo { pub fn remove_dependent_files(&mut self, path: &PathBuf) -> io::Result<()>{ self.dependencies.handle_dependent_files(path); - for path in &self.removed_files { - let _ = remove_file(path); + for name in &self.removed_names { + let _ = remove_file(path.join(name)); } - self.removed_files = Vec::new(); + self.removed_names = Vec::new(); Ok(()) } - pub fn has_dependency(&self) -> bool { + pub fn has_todo_dependency(&self) -> bool { self.dependency_type == DependencyType::TodoList } @@ -309,30 +299,28 @@ impl Todo { } } - pub fn remove_dependency(&mut self, path: &PathBuf) { - if let Some(path) = self.dependency_path(path) { - self.removed_files.push(path); + pub fn remove_dependency(&mut self) { + if self.dependency_type != DependencyType::None { + self.removed_names.push(self.dependency_name.clone()); } self.dependency_type = DependencyType::None; - self.dependencies.remove_dependencies(path); + self.dependencies.remove_dependencies(); self.dependency_name = String::new(); self.note = String::new(); - // self.dependencies = TodoList::new(); } - pub fn set_note(&mut self, note:String, path: &PathBuf) -> io::Result<()>{ - // let _ = self.remove_dependency(&path); + pub fn set_note(&mut self, note:String) -> io::Result<()>{ self.dependency_name = sha1(¬e); self.dependency_type = DependencyType::Note; self.note = note; Ok(()) } - pub fn edit_note(&mut self, path: &PathBuf)-> io::Result<()>{ + pub fn edit_note(&mut self)-> io::Result<()>{ let note = open_temp_editor(self.note.clone())?; - self.set_note(note, path)?; + self.set_note(note)?; Ok(()) } @@ -433,7 +421,7 @@ mod tests { fn test_todo_into_string() { let mut todo = Todo::default("Test".to_string(), 1); let path = PathBuf::new(); - todo.set_note("Note".to_string(), &path); + todo.set_note("Note".to_string()); let expected = "[1]>2c924e3088204ee77ba681f72be3444357932fca Test"; let result: String = (&todo).into(); @@ -449,7 +437,7 @@ mod tests { dependency_type: DependencyType::Note, date_str: String::new(), daily: false, - removed_files: Vec::new(), + removed_names: Vec::new(), dependency_name: String::from("2c924e3088204ee77ba681f72be3444357932fca"), message: "Test".to_string(), priority: 1, @@ -501,7 +489,7 @@ mod tests { let mut todo = Todo::default("Test".to_string(), 1); let expected = "900a80c94f076b4ee7006a9747667ccf6878a72b.todo"; let pathbuf = PathBuf::from("tests/TODO_LIST"); - todo.add_dependency(&pathbuf).expect("Error setting dependency"); + todo.add_todo_dependency().expect("Error setting dependency"); let result = &todo.dependency_name; assert_eq!(result, expected); @@ -511,10 +499,10 @@ mod tests { fn test_remove_note() { let mut todo = Todo::default("Test".to_string(), 1); let path = PathBuf::new(); - todo.set_note("Note".to_string(), &path).expect("Error setting note"); + todo.set_note("Note".to_string()).expect("Error setting note"); let pathbuf = PathBuf::from("test/TODO_LIST"); - todo.remove_note(&pathbuf); + todo.remove_note(); assert_eq!(todo.dependency_type, DependencyType::None); assert!(todo.note_empty()); @@ -526,20 +514,20 @@ mod tests { let pathbuf = PathBuf::from("test/TODO_LIST"); - todo.add_dependency(&pathbuf); + todo.add_todo_dependency(); - assert!(todo.has_dependency()); + assert!(todo.has_todo_dependency()); } #[test] fn test_remove_dependency() { let mut todo = Todo::default("Test".to_string(), 1); let pathbuf = PathBuf::from("test/TODO_LIST"); - todo.add_dependency(&pathbuf); + todo.add_todo_dependency(); - todo.remove_dependency(&pathbuf); + todo.remove_dependency(); - assert!(!todo.has_dependency()); + assert!(!todo.has_todo_dependency()); } #[test] @@ -563,7 +551,7 @@ mod tests { dependency_type: DependencyType::TodoList, date_str: String::new(), daily: false, - removed_files: Vec::new(), + removed_names: Vec::new(), dependency_name: "1BE348656D84993A6DF0DB0DECF2E95EF2CF461c.todo".to_string(), message: "Read for exams".to_string(), priority: 1, @@ -582,7 +570,7 @@ mod tests { dependency_type: DependencyType::None, date_str: "2023-09-05".to_string(), daily: true, - removed_files: Vec::new(), + removed_names: Vec::new(), dependency_name: String::new(), message: "this one should be daily".to_string(), priority: 2, @@ -599,7 +587,7 @@ mod tests { dependency_type: DependencyType::None, date_str: String::new(), daily: true, - removed_files: Vec::new(), + removed_names: Vec::new(), dependency_name: String::new(), message: "this one should be daily".to_string(), priority: 2, diff --git a/src/tui/app.rs b/src/tui/app.rs index 4d5538b..45a54ed 100644 --- a/src/tui/app.rs +++ b/src/tui/app.rs @@ -280,7 +280,7 @@ impl<'a>App<'a>{ #[inline] pub fn traverse_down(&mut self) { match self.todo() { - Some(todo) if todo.has_dependency() => { + Some(todo) if todo.has_todo_dependency() => { self.prior_indexes.push(self.index); self.index = 0; } @@ -459,9 +459,9 @@ impl<'a>App<'a>{ #[inline] pub fn add_dependency_traverse_down(&mut self) { if let Some(todo) = self.todo() { - if !todo.has_dependency() && todo.note_empty() { + if !todo.has_todo_dependency() && todo.note_empty() { let todo_path = self.todo_path.clone(); - self.mut_todo().unwrap().add_dependency(&todo_path); + self.mut_todo().unwrap().add_todo_dependency(); } } self.traverse_down() @@ -510,25 +510,22 @@ impl<'a>App<'a>{ #[inline] pub fn edit_or_add_note(&mut self) { - let path = self.todo_path.clone(); if let Some(todo) = self.mut_todo() { - todo.edit_note(&path); + todo.edit_note(); } } #[inline] pub fn add_dependency(&mut self) { - let todo_path = self.todo_path.clone(); if let Some(todo) = self.mut_todo() { - todo.add_dependency(&todo_path); + todo.add_todo_dependency(); } } #[inline] pub fn remove_current_dependent(&mut self) { - let path = self.todo_path.clone(); if let Some(todo) = self.mut_todo() { - todo.remove_dependency(&path); + todo.remove_dependency(); } } @@ -765,7 +762,7 @@ impl<'a>App<'a>{ // }; let dependency_width = if let Some(todo) = todo { - let should_show_right = (todo.has_dependency() || !todo.get_note_content().is_empty()) && self.show_right; + let should_show_right = (todo.has_todo_dependency() || !todo.get_note_content().is_empty()) && self.show_right; 40 * (should_show_right as u16) } else { 0 @@ -820,7 +817,7 @@ impl<'a>App<'a>{ let note_widget = Paragraph::new(Text::styled(todo.get_note_content(), Style::default())).wrap(Wrap { trim: true }).block(default_block("Todo note")); frame.render_widget(note_widget, todos_layout[1]); } else - if todo.has_dependency() { + if todo.has_todo_dependency() { match create_todo_widget(&todo.dependencies.display(self.show_done), String::from("Todo dependencies")) { TodoWidget::List(widget) =>frame.render_widget(widget, todos_layout[1]), TodoWidget::Paragraph(widget) =>frame.render_widget(widget, todos_layout[1]),