1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use serde::{Deserialize, Serialize};
use serde_json::Result;
use std::collections::HashMap;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Command {
    Get {
        key: String,
    },
    GetDel {
        key: String,
    },
    Set {
        key: String,
        value: String,
    },
    Delete {
        keys: Vec<String>,
    },
    LPush {
        key: String,
        values: Vec<String>,
    },
    RPush {
        key: String,
        values: Vec<String>,
    },
    LRange {
        key: String,
        start: Option<usize>,
        end: Option<usize>,
    },
    LLen {
        key: String,
    },
    LPop {
        key: String,
    },
    RPop {
        key: String,
    },
    Exists {
        key: String,
    },
    Incr {
        key: String,
    },
    IncrBy {
        key: String,
        increment: i64,
    },
    Decr {
        key: String,
    },
    DecrBy {
        key: String,
        decrement: i64,
    },
    SAdd {
        key: String,
        values: Vec<String>,
    },
    SCard {
        key: String,
    },
    SInter {
        key: String,
        others: Vec<String>,
    },
    SDiff {
        key: String,
        others: Vec<String>,
    },
    ZAdd {
        key: String,
        values: HashMap<String, i64>,
    },
    ZCard {
        key: String,
    },
}

impl Command {
    pub fn from_json(json_str: &str) -> Result<Command> {
        serde_json::from_str(json_str)
    }

    pub(crate) fn to_json(&self) -> Result<String> {
        serde_json::to_string(self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_get_command_from_json() {
        let json_str = r#"{"Get":{"key":"mykey"}}"#;
        let cmd = Command::from_json(json_str).unwrap();
        match cmd {
            Command::Get { key } => assert_eq!(key, "mykey"),
            _ => panic!("Expected Command::Get"),
        }
    }

    #[test]
    fn test_set_command_to_json() {
        let cmd = Command::Set {
            key: "mykey".to_string(),
            value: "myvalue".to_string(),
        };
        let json_str = cmd.to_json().unwrap();
        assert_eq!(json_str, r#"{"Set":{"key":"mykey","value":"myvalue"}}"#);
    }

    #[test]
    fn test_zadd_command_from_json() {
        let json_str = r#"{"ZAdd":{"key":"myset","values":{"key_1":1,"key_2":2}}}"#;
        let cmd = Command::from_json(json_str).unwrap();
        match cmd {
            Command::ZAdd { key, values } => {
                assert_eq!(key, "myset");
                assert_eq!(values.get("key_1"), Some(&1));
                assert_eq!(values.get("key_2"), Some(&2));
            }
            _ => panic!("Expected Command::ZAdd"),
        }
    }

    #[test]
    fn test_lrange_command_to_json() {
        let cmd = Command::LRange {
            key: "mylist".to_string(),
            start: Some(0),
            end: Some(10),
        };
        let json_str = cmd.to_json().unwrap();
        assert_eq!(
            json_str,
            r#"{"LRange":{"key":"mylist","start":0,"end":10}}"#
        );
    }
}