| @@ -41,56 +41,58 @@ fn main() { |
| 41 | 41 | |
| 42 | 42 | let file_path = "tasks.txt"; |
| 43 | 43 | |
| 44 | | // Handle task completion |
| 45 | 44 | if let Some(ids) = matches.get_many::<String>("done") { |
| 46 | | let mut tasks = load_tasks(file_path); |
| 47 | | let mut completed_tasks = Vec::new(); |
| 48 | | for id in ids { |
| 49 | | if let Ok(index) = id.parse::<usize>() { |
| 50 | | if index > 0 && index <= tasks.len() { |
| 51 | | tasks[index - 1].completed = true; // Mark task as complete |
| 52 | | } else { |
| 53 | | completed_tasks.push(id.clone()); // Collect invalid task IDs |
| 54 | | } |
| 55 | | } |
| 56 | | } |
| 57 | | save_tasks(file_path, &tasks); |
| 45 | mark_done(file_path, ids); |
| 46 | } |
| 58 | 47 | |
| 59 | | // Alert for nonexistent tasks |
| 60 | | if !completed_tasks.is_empty() { |
| 61 | | for id in completed_tasks { |
| 62 | | println!("Task {} doesn't exist.", id); |
| 63 | | } |
| 64 | | } else { |
| 65 | | println!("Tasks marked as complete."); |
| 66 | | } |
| 48 | if let Some(new_tasks) = matches.get_many::<String>("task") { |
| 49 | add_tasks(file_path, new_tasks); |
| 50 | } |
| 67 | 51 | |
| 68 | | // Display the updated task list |
| 52 | // Display saved tasks if no new tasks or done tasks were provided |
| 53 | if !matches.contains_id("task") && !matches.contains_id("done") { |
| 69 | 54 | display_tasks(file_path); |
| 70 | 55 | } |
| 56 | } |
| 71 | 57 | |
| 72 | | // Handle new tasks |
| 73 | | if let Some(new_tasks) = matches.get_many::<String>("task") { |
| 74 | | let mut tasks = load_tasks(file_path); |
| 75 | | let next_id = tasks.len() + 1; // Determine the next task ID |
| 76 | | for task_desc in new_tasks { |
| 77 | | tasks.push(Task { |
| 78 | | id: next_id, |
| 79 | | description: task_desc.to_string(), |
| 80 | | completed: false, |
| 81 | | }); |
| 58 | fn mark_done<'a>(file_path: &str, ids: impl Iterator<Item = &'a String>) { |
| 59 | let mut tasks = load_tasks(file_path); |
| 60 | let mut missing = Vec::new(); |
| 61 | for id in ids { |
| 62 | if let Ok(index) = id.parse::<usize>() { |
| 63 | if index > 0 && index <= tasks.len() { |
| 64 | tasks[index - 1].completed = true; |
| 65 | } else { |
| 66 | missing.push(id.clone()); |
| 67 | } |
| 82 | 68 | } |
| 83 | | save_tasks(file_path, &tasks); |
| 84 | | println!("Tasks saved to {}", file_path); |
| 69 | } |
| 70 | save_tasks(file_path, &tasks); |
| 85 | 71 | |
| 86 | | // Display the updated task list |
| 87 | | display_tasks(file_path); |
| 72 | if missing.is_empty() { |
| 73 | println!("Tasks marked as complete."); |
| 74 | } |
| 75 | for id in missing { |
| 76 | println!("Task {} doesn't exist.", id); |
| 88 | 77 | } |
| 89 | 78 | |
| 90 | | // Display saved tasks if no new tasks or done tasks were provided |
| 91 | | if !matches.contains_id("task") && !matches.contains_id("done") { |
| 92 | | display_tasks(file_path); |
| 79 | display_tasks(file_path); |
| 80 | } |
| 81 | |
| 82 | fn add_tasks<'a>(file_path: &str, new_tasks: impl Iterator<Item = &'a String>) { |
| 83 | let mut tasks = load_tasks(file_path); |
| 84 | let next_id = tasks.len() + 1; |
| 85 | for task_desc in new_tasks { |
| 86 | tasks.push(Task { |
| 87 | id: next_id, |
| 88 | description: task_desc.to_string(), |
| 89 | completed: false, |
| 90 | }); |
| 93 | 91 | } |
| 92 | save_tasks(file_path, &tasks); |
| 93 | println!("Tasks saved to {}", file_path); |
| 94 | |
| 95 | display_tasks(file_path); |
| 94 | 96 | } |
| 95 | 97 | |
| 96 | 98 | fn load_tasks(file_path: &str) -> Vec<Task> { |