Skip to main content

kanade_shared/wire/
command.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Serialize, Deserialize, Debug, Clone)]
4pub struct Command {
5    pub id: String,
6    pub version: String,
7    pub request_id: String,
8    pub job_id: Option<String>,
9    pub shell: Shell,
10    pub script: String,
11    pub timeout_secs: u64,
12    pub jitter_secs: Option<u64>,
13}
14
15#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
16#[serde(rename_all = "lowercase")]
17pub enum Shell {
18    Powershell,
19    Cmd,
20}
21
22#[cfg(test)]
23mod tests {
24    use super::*;
25
26    fn sample_command() -> Command {
27        Command {
28            id: "echo-test".into(),
29            version: "1.0.0".into(),
30            request_id: "req-1".into(),
31            job_id: Some("dep-1".into()),
32            shell: Shell::Powershell,
33            script: "echo hi".into(),
34            timeout_secs: 30,
35            jitter_secs: Some(5),
36        }
37    }
38
39    #[test]
40    fn shell_serialises_lowercase() {
41        let json = serde_json::to_string(&Shell::Powershell).unwrap();
42        assert_eq!(json, "\"powershell\"");
43        let json = serde_json::to_string(&Shell::Cmd).unwrap();
44        assert_eq!(json, "\"cmd\"");
45    }
46
47    #[test]
48    fn command_round_trips_through_json() {
49        let orig = sample_command();
50        let json = serde_json::to_string(&orig).expect("encode");
51        let decoded: Command = serde_json::from_str(&json).expect("decode");
52        assert_eq!(decoded.id, orig.id);
53        assert_eq!(decoded.version, orig.version);
54        assert_eq!(decoded.request_id, orig.request_id);
55        assert_eq!(decoded.job_id, orig.job_id);
56        assert_eq!(decoded.shell, orig.shell);
57        assert_eq!(decoded.script, orig.script);
58        assert_eq!(decoded.timeout_secs, orig.timeout_secs);
59        assert_eq!(decoded.jitter_secs, orig.jitter_secs);
60    }
61
62    #[test]
63    fn command_accepts_missing_optional_fields() {
64        let json = r#"{
65          "id": "x",
66          "version": "1.0.0",
67          "request_id": "r",
68          "shell": "cmd",
69          "script": "echo",
70          "timeout_secs": 5
71        }"#;
72        let cmd: Command = serde_json::from_str(json).expect("decode");
73        assert!(cmd.job_id.is_none());
74        assert!(cmd.jitter_secs.is_none());
75        assert_eq!(cmd.shell, Shell::Cmd);
76    }
77}