task_tracker_cli/task/
manager.rs

1/// Task Manager for managing multiple tasks
2
3use crate::task::Task;
4use crate::models::Serializable;
5use tracing::debug;
6use json::JsonValue;
7
8/// Helper struct for generating unique task IDs
9#[derive(Debug)]
10pub struct IdGenerator {
11    current_id: u32,
12}
13
14impl IdGenerator {
15    pub fn new(start_value: Option<u32>) -> Self {
16        IdGenerator {
17            current_id: start_value.unwrap_or(0),
18        }
19    }
20
21    pub fn generate(&mut self) -> u32 {
22        self.current_id += 1;
23        debug!("Generated new task ID: {}", self.current_id);
24        self.current_id
25    }
26
27    pub fn current(&self) -> u32 {
28        self.current_id
29    }
30}
31
32/// Manages a collection of tasks
33#[derive(Debug)]
34pub struct TaskManager {
35    pub tasks: Vec<Task>,
36    id_generator: IdGenerator,
37}
38
39impl TaskManager {
40    /// Creates a new empty TaskManager
41    pub fn new() -> Self {
42        debug!("Creating new TaskManager");
43        TaskManager {
44            tasks: Vec::new(),
45            id_generator: IdGenerator::new(None),
46        }
47    }
48
49    /// Adds a new task with the given description
50    pub fn add_task(&mut self, desc: String) {
51        let task = Task::new(self.id_generator.generate(), desc);
52        self.tasks.push(task);
53    }
54
55    /// Retrieves a task by ID (immutable)
56    pub fn get_task(&self, id: u32) -> Option<&Task> {
57        self.tasks.iter().find(|&task| task.id == id)
58    }
59
60    /// Retrieves a task by ID (mutable)
61    pub fn get_task_mut(&mut self, id: u32) -> Option<&mut Task> {
62        self.tasks.iter_mut().find(|task| task.id == id)
63    }
64
65    /// Removes a task by ID
66    pub fn remove_task(&mut self, id: u32) {
67        debug!("Removing task {}", id);
68        self.tasks.retain(|task| task.id != id);
69    }
70
71    /// Converts all tasks to a JSON string and returns current ID
72    pub fn to_json_string(&self) -> (String, u32) {
73        let json_array = self
74            .tasks
75            .iter()
76            .map(|t| t.to_json())
77            .collect::<Vec<_>>();
78        (
79            JsonValue::Array(json_array).to_string(),
80            self.id_generator.current(),
81        )
82    }
83
84    /// Creates a TaskManager from a JSON string with the given current ID
85    pub fn from_json_string(json_str: &str, current_id: u32) -> Option<Self> {
86        let parsed = json::parse(json_str).ok()?;
87        let mut tasks = Vec::new();
88        for item in parsed.members() {
89            if let Some(task) = Task::from_json(item) {
90                tasks.push(task);
91            }
92        }
93        debug!("Loaded {} tasks from JSON", tasks.len());
94        Some(TaskManager {
95            tasks,
96            id_generator: IdGenerator::new(Some(current_id)),
97        })
98    }
99}
100
101impl Default for TaskManager {
102    fn default() -> Self {
103        Self::new()
104    }
105}