tsk/server/
protocol.rs

1use crate::task::{Task, TaskStatus};
2use serde::{Deserialize, Serialize};
3use std::path::PathBuf;
4
5/// Request messages that can be sent to the TSK server
6#[derive(Debug, Serialize, Deserialize)]
7#[serde(tag = "type")]
8pub enum Request {
9    /// Add a new task to the queue
10    AddTask { repo_path: PathBuf, task: Box<Task> },
11    /// List all tasks
12    ListTasks,
13    /// Get the status of a specific task
14    GetStatus { task_id: String },
15    /// Shutdown the server
16    Shutdown,
17}
18
19/// Response messages from the TSK server
20#[derive(Debug, Serialize, Deserialize)]
21#[serde(tag = "type")]
22pub enum Response {
23    /// Successful operation
24    Success { message: String },
25    /// Error occurred
26    Error { message: String },
27    /// List of tasks
28    TaskList { tasks: Vec<Task> },
29    /// Status of a specific task
30    TaskStatus { status: TaskStatus },
31}
32
33impl Request {
34    /// Parse a request from a JSON string
35    #[allow(dead_code)]
36    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
37        serde_json::from_str(json)
38    }
39
40    /// Serialize the request to JSON
41    #[allow(dead_code)]
42    pub fn to_json(&self) -> Result<String, serde_json::Error> {
43        serde_json::to_string(self)
44    }
45}
46
47impl Response {
48    /// Parse a response from a JSON string
49    #[allow(dead_code)]
50    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
51        serde_json::from_str(json)
52    }
53
54    /// Serialize the response to JSON
55    #[allow(dead_code)]
56    pub fn to_json(&self) -> Result<String, serde_json::Error> {
57        serde_json::to_string(self)
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn test_request_serialization() {
67        let request = Request::ListTasks;
68        let json = request.to_json().unwrap();
69        let parsed: Request = Request::from_json(&json).unwrap();
70
71        match parsed {
72            Request::ListTasks => (),
73            _ => panic!("Unexpected request type"),
74        }
75    }
76
77    #[test]
78    fn test_response_serialization() {
79        let response = Response::Success {
80            message: "Test message".to_string(),
81        };
82        let json = response.to_json().unwrap();
83        let parsed: Response = Response::from_json(&json).unwrap();
84
85        match parsed {
86            Response::Success { message } => {
87                assert_eq!(message, "Test message");
88            }
89            _ => panic!("Unexpected response type"),
90        }
91    }
92}