task_tracker_cli/task/
mod.rs

1/// Task module containing Task struct and TaskManager
2
3pub mod manager;
4
5pub use manager::TaskManager;
6
7use chrono::{DateTime, Local};
8use json::{JsonValue, object};
9use crate::models::{TaskStatus, Serializable, Identifiable};
10use tracing::debug;
11
12/// Represents a single task
13#[derive(Debug, Clone)]
14pub struct Task {
15    pub id: u32,
16    pub description: String,
17    pub status: TaskStatus,
18    pub created_at: DateTime<Local>,
19    pub updated_at: DateTime<Local>,
20}
21
22impl Task {
23    /// Creates a new task with the given ID and description
24    pub fn new(id: u32, desc: String) -> Self {
25        debug!("Creating new task: id={}, desc=\"{}\"", id, desc);
26        Task {
27            id,
28            description: desc,
29            status: TaskStatus::ToDo,
30            created_at: Local::now(),
31            updated_at: Local::now(),
32        }
33    }
34
35    /// Updates the task's timestamp
36    fn update_timestamp(&mut self) {
37        self.updated_at = Local::now();
38    }
39
40    /// Updates the task description
41    pub fn update(&mut self, desc: String) {
42        debug!("Updating task {}: \"{}\" → \"{}\"", self.id, self.description, desc);
43        self.description = desc;
44        self.update_timestamp();
45    }
46
47    /// Marks the task as in progress
48    pub fn mark_in_progress(&mut self) {
49        debug!("Marking task {} as In Progress", self.id);
50        self.status = TaskStatus::InProgress;
51        self.update_timestamp();
52    }
53
54    /// Marks the task as done
55    pub fn mark_done(&mut self) {
56        debug!("Marking task {} as Done", self.id);
57        self.status = TaskStatus::Done;
58        self.update_timestamp();
59    }
60}
61
62impl Serializable for Task {
63    fn to_json(&self) -> JsonValue {
64        object! {
65            "id" => self.id,
66            "description" => self.description.clone(),
67            "status" => self.status.to_string(),
68            "created_at" => self.created_at.to_rfc3339(),
69            "updated_at" => self.updated_at.to_rfc3339(),
70        }
71    }
72
73    fn from_json(json: &JsonValue) -> Option<Self> {
74        Some(Task {
75            id: json["id"].as_u32()?,
76            description: json["description"].as_str()?.to_string(),
77            status: TaskStatus::from_str(json["status"].as_str()?)?,
78            created_at: DateTime::parse_from_rfc3339(json["created_at"].as_str()?)
79                .ok()?
80                .with_timezone(&Local),
81            updated_at: DateTime::parse_from_rfc3339(json["updated_at"].as_str()?)
82                .ok()?
83                .with_timezone(&Local),
84        })
85    }
86}
87
88impl Identifiable for Task {
89    fn get_id(&self) -> u32 {
90        self.id
91    }
92}