akv/command/
hash.rs

1use bincode::{Decode, Encode};
2use serde::{Deserialize, Serialize};
3
4use crate::{MAGIC_NUMBER, VERSION};
5
6use super::CmdType;
7
8#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
9pub struct HashSet {
10    pub db: String,
11    pub key: String,
12    pub field: String,
13    pub value: String,
14    pub expire: Option<u64>,
15}
16
17#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
18pub struct ResultHashSet {
19    pub ok: bool,
20    pub msg: String,
21}
22
23impl ResultHashSet {
24    pub fn new(ok: bool, msg: String) -> Self {
25        Self { ok, msg }
26    }
27
28    pub fn to_result(&self, seq: u32) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
29        let data = serde_json::to_vec(self)?;
30        let data_length = data.len() as u32;
31
32        let mut result = vec![];
33        result.extend(&MAGIC_NUMBER); // 魔数
34        result.extend(&VERSION.to_be_bytes()); // 版本
35        result.push(CmdType::HashSet as u8);
36        result.extend(&seq.to_be_bytes()); // 序列号
37        result.extend(&data_length.to_be_bytes()); // 数据长度
38        result.extend(data); // 数据
39
40        Ok(result)
41    }
42}
43
44#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
45pub struct HashGet {
46    pub db: String,
47    pub key: String,
48    pub field: String,
49}
50
51#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
52pub struct ResultHashGet {
53    pub value: Option<String>,
54    pub expire: Option<u64>,
55}
56
57impl ResultHashGet {
58    pub fn new(value: Option<String>, expire: Option<u64>) -> Self {
59        Self { value, expire }
60    }
61
62    pub fn to_result(&self, seq: u32) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
63        let data = serde_json::to_vec(self)?;
64        let data_length = data.len() as u32;
65
66        let mut result = vec![];
67        result.extend(&MAGIC_NUMBER); // 魔数
68        result.extend(&VERSION.to_be_bytes()); // 版本
69        result.push(CmdType::HashGet as u8);
70        result.extend(&seq.to_be_bytes()); // 序列号
71        result.extend(&data_length.to_be_bytes()); // 数据长度
72        result.extend(data); // 数据
73
74        Ok(result)
75    }
76}
77
78#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
79pub struct HashDel {
80    pub db: String,
81    pub key: String,
82    pub field: String,
83}
84
85#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
86pub struct ResultHashDel {
87    pub value: Option<String>,
88    pub expire: Option<u64>,
89}
90
91impl ResultHashDel {
92    pub fn new(value: Option<String>, expire: Option<u64>) -> Self {
93        Self { value, expire }
94    }
95
96    pub fn to_result(&self, seq: u32) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
97        let data = serde_json::to_vec(self)?;
98        let data_length = data.len() as u32;
99
100        let mut result = vec![];
101        result.extend(&MAGIC_NUMBER); // 魔数
102        result.extend(&VERSION.to_be_bytes()); // 版本
103        result.push(CmdType::HashDel as u8);
104        result.extend(&seq.to_be_bytes()); // 序列号
105        result.extend(&data_length.to_be_bytes()); // 数据长度
106        result.extend(data); // 数据
107
108        Ok(result)
109    }
110}
111
112#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
113pub struct HashExists {
114    pub db: String,
115    pub key: String,
116    pub field: String,
117}
118
119#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
120pub struct ResultHashExists {
121    pub exists: bool,
122}
123
124impl ResultHashExists {
125    pub fn new(exists: bool) -> Self {
126        Self { exists }
127    }
128
129    pub fn to_result(&self, seq: u32) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
130        let data = serde_json::to_vec(self)?;
131        let data_length = data.len() as u32;
132
133        let mut result = vec![];
134        result.extend(&MAGIC_NUMBER); // 魔数
135        result.extend(&VERSION.to_be_bytes()); // 版本
136        result.push(CmdType::HashExists as u8);
137        result.extend(&seq.to_be_bytes()); // 序列号
138        result.extend(&data_length.to_be_bytes()); // 数据长度
139        result.extend(data); // 数据
140
141        Ok(result)
142    }
143}
144
145#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
146pub struct HashLen {
147    pub db: String,
148    pub key: String,
149}
150
151#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
152pub struct ResultHashLen {
153    pub len: u32,
154}
155
156impl ResultHashLen {
157    pub fn new(len: u32) -> Self {
158        Self { len }
159    }
160
161    pub fn to_result(&self, seq: u32) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
162        let data = serde_json::to_vec(self)?;
163        let data_length = data.len() as u32;
164
165        let mut result = vec![];
166        result.extend(&MAGIC_NUMBER); // 魔数
167        result.extend(&VERSION.to_be_bytes()); // 版本
168        result.push(CmdType::HashLen as u8);
169        result.extend(&seq.to_be_bytes()); // 序列号
170        result.extend(&data_length.to_be_bytes()); // 数据长度
171        result.extend(data); // 数据
172
173        Ok(result)
174    }
175}
176
177#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
178pub struct HashFields {
179    pub db: String,
180    pub key: String,
181}
182
183#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
184pub struct ResultHashFields {
185    pub fields: Vec<String>,
186    pub total: u32,
187}
188
189impl ResultHashFields {
190    pub fn new(fields: Vec<String>, total: u32) -> Self {
191        Self { fields, total }
192    }
193
194    pub fn to_result(&self, seq: u32) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
195        let data = serde_json::to_vec(self)?;
196        let data_length = data.len() as u32;
197
198        let mut result = vec![];
199        result.extend(&MAGIC_NUMBER); // 魔数
200        result.extend(&VERSION.to_be_bytes()); // 版本
201        result.push(CmdType::HashFields as u8);
202        result.extend(&seq.to_be_bytes()); // 序列号
203        result.extend(&data_length.to_be_bytes()); // 数据长度
204        result.extend(data); // 数据
205
206        Ok(result)
207    }
208}