use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, Debug)]
pub struct Request {
pub secret: Option<String>,
pub command: Command,
}
#[derive(Deserialize, Serialize, Debug)]
pub enum Command {
Add {
url: String,
dir: String,
},
Status,
Shutdown,
Pause {
id: usize,
},
Resume {
id: usize,
},
}
#[derive(Serialize, Deserialize, Debug)]
pub enum Response {
Ok(String),
StatusList(Vec<JobStatus>),
Err(String),
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct JobStatus {
pub id: usize,
pub filename: String,
pub progress_percent: u64,
pub state: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn command_serialize_deserialize_roundtrip() {
let cmd = Command::Add {
url: "https://example.com/file".into(),
dir: "./".into(),
};
let json = serde_json::to_string(&cmd).expect("serialize");
let parsed: Command = serde_json::from_str(&json).expect("deserialize");
match parsed {
Command::Add { url, dir } => {
assert_eq!(url, "https://example.com/file");
assert_eq!(dir, "./");
}
_ => panic!("Unexpected variant"),
}
}
#[test]
fn response_statuslist_serialization() {
let job = JobStatus {
id: 1,
filename: "foo.bin".into(),
progress_percent: 50,
state: "Half".into(),
};
let resp = Response::StatusList(vec![job.clone()]);
let json = serde_json::to_string(&resp).expect("serialize");
let parsed: Response = serde_json::from_str(&json).expect("deserialize");
match parsed {
Response::StatusList(list) => {
assert_eq!(list.len(), 1);
assert_eq!(list[0].id, 1);
assert_eq!(list[0].filename, "foo.bin");
}
_ => panic!("Unexpected variant"),
}
}
}