akv/command/
mod.rs

1pub mod string;
2
3use bincode::{Decode, Encode};
4use serde::{Deserialize, Serialize};
5
6use crate::{MAGIC_NUMBER, VERSION};
7
8#[derive(Debug, Clone, PartialEq)]
9pub enum CmdType {
10    Ping = 0x01,
11    Authenticate = 0x02,
12    Keys = 0x03,
13    Exists = 0x04,
14    Expire = 0x05,
15    SetString = 0x11,
16    GetString = 0x12,
17    DelString = 0x13,
18}
19
20#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
21pub struct Ping {
22    pub timestamp: u64,
23}
24
25#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
26pub struct Pong {
27    pub timestamp: u64,
28}
29
30impl Pong {
31    pub fn new(timestamp: u64) -> Self {
32        Self { timestamp }
33    }
34
35    pub fn default() -> Self {
36        Self { timestamp: 0 }
37    }
38
39    pub fn to_result(&self) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
40        let data = serde_json::to_vec(self)?;
41        let data_length = data.len() as u32;
42
43        let mut result = vec![];
44        result.extend(&MAGIC_NUMBER); // 魔数
45        result.push(VERSION); // 版本
46        result.push(CmdType::Ping as u8); // Ping 命令结果
47        result.extend(&data_length.to_be_bytes()); // 长度
48        result.extend(data); // Pong 数据
49        Ok(result)
50    }
51}
52
53#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
54pub struct Authenticate {
55    pub secret: String,
56}
57
58#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
59pub struct ResultAuthenticate {
60    pub ok: bool,
61    pub msg: String,
62}
63
64impl ResultAuthenticate {
65    pub fn new(ok: bool, msg: String) -> Self {
66        Self { ok, msg }
67    }
68
69    pub fn to_result(&self) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
70        let data = serde_json::to_vec(self)?;
71        let data_length = data.len() as u32;
72
73        let mut result = vec![];
74        result.extend(&MAGIC_NUMBER); // 魔数
75        result.push(VERSION); // 版本
76        result.push(CmdType::Authenticate as u8); // Authenticate 命令结果
77        result.extend(&data_length.to_be_bytes()); // 长度
78        result.extend(data); // Authenticate 数据
79        Ok(result)
80    }
81}
82
83#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
84pub struct Keys {
85    pub db: String,
86    pub page: usize,
87    pub size: usize,
88}
89
90#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
91pub struct ResultKeys {
92    pub keys: Vec<String>,
93    pub total: u32,
94}
95
96impl ResultKeys {
97    pub fn new(keys: Vec<String>, total: u32) -> Self {
98        Self { keys, total }
99    }
100
101    pub fn to_result(&self) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
102        let data = serde_json::to_vec(self)?;
103        let data_length = data.len() as u32;
104
105        let mut result = vec![];
106        result.extend(&MAGIC_NUMBER); // 魔数
107        result.push(VERSION); // 版本
108        result.push(CmdType::Keys as u8); // Keys 命令结果
109        result.extend(&data_length.to_be_bytes()); // 长度
110        result.extend(data); // Keys 数据
111        Ok(result)
112    }
113}
114
115#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
116pub struct Exists {
117    pub db: String,
118    pub key: String,
119}
120
121#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
122pub struct ResultExists {
123    pub exists: bool,
124}
125
126impl ResultExists {
127    pub fn new(exists: bool) -> Self {
128        Self { exists }
129    }
130
131    pub fn to_result(&self) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
132        let data = serde_json::to_vec(self)?;
133        let data_length = data.len() as u32;
134
135        let mut result = vec![];
136        result.extend(&MAGIC_NUMBER); // 魔数
137        result.extend(&VERSION.to_be_bytes()); // 版本
138        result.push(CmdType::Exists as u8);
139        result.extend(&data_length.to_be_bytes()); // 数据长度
140        result.extend(data); // 数据
141
142        Ok(result)
143    }
144}
145
146#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
147pub struct Expire {
148    pub db: String,
149    pub key: String,
150    pub expire: u64,
151}
152
153#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
154pub struct ResultExpire {
155    pub ok: bool,
156}
157
158impl ResultExpire {
159    pub fn new(ok: bool) -> Self {
160        Self { ok }
161    }
162
163    pub fn to_result(&self) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
164        let data = serde_json::to_vec(self)?;
165        let data_length = data.len() as u32;
166
167        let mut result = vec![];
168        result.extend(&MAGIC_NUMBER); // 魔数
169        result.extend(&VERSION.to_be_bytes()); // 版本
170        result.push(CmdType::Expire as u8);
171        result.extend(&data_length.to_be_bytes()); // 数据长度
172        result.extend(data); // 数据
173
174        Ok(result)
175    }
176}