akv/command/
string.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 SetString {
10    pub db: String,
11    pub key: String,
12    pub value: String,
13    pub expire: Option<u64>,
14}
15
16#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
17pub struct ResultSetString {
18    pub ok: bool,
19    pub msg: String,
20}
21
22impl ResultSetString {
23    pub fn new(ok: bool, msg: String) -> Self {
24        Self { ok, msg }
25    }
26
27    pub fn to_result(&self) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
28        let data = serde_json::to_vec(self)?;
29        let data_length = data.len() as u32;
30
31        let mut result = vec![];
32        result.extend(&MAGIC_NUMBER); // 魔数
33        result.extend(&VERSION.to_be_bytes()); // 版本
34        result.push(CmdType::SetString as u8);
35        result.extend(&data_length.to_be_bytes()); // 数据长度
36        result.extend(data); // 数据
37
38        Ok(result)
39    }
40}
41
42#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
43pub struct GetString {
44    pub db: String,
45    pub key: String,
46}
47
48#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
49pub struct ResultGetString {
50    pub value: Option<String>,
51    pub expire: Option<u64>,
52}
53
54impl ResultGetString {
55    pub fn new(value: Option<String>, expire: Option<u64>) -> Self {
56        Self { value, expire }
57    }
58
59    pub fn to_result(&self) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
60        let data = serde_json::to_vec(self)?;
61        let data_length = data.len() as u32;
62
63        let mut result = vec![];
64        result.extend(&MAGIC_NUMBER); // 魔数
65        result.extend(&VERSION.to_be_bytes()); // 版本
66        result.push(CmdType::GetString as u8);
67        result.extend(&data_length.to_be_bytes()); // 数据长度
68        result.extend(data); // 数据
69
70        Ok(result)
71    }
72}
73
74#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
75pub struct DelString {
76    pub db: String,
77    pub key: String,
78}
79
80#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
81pub struct ResultDelString {
82    pub value: Option<String>,
83    pub expire: Option<u64>,
84}
85
86impl ResultDelString {
87    pub fn new(value: Option<String>, expire: Option<u64>) -> Self {
88        Self { value, expire }
89    }
90
91    pub fn to_result(&self) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
92        let data = serde_json::to_vec(self)?;
93        let data_length = data.len() as u32;
94
95        let mut result = vec![];
96        result.extend(&MAGIC_NUMBER); // 魔数
97        result.extend(&VERSION.to_be_bytes()); // 版本
98        result.push(CmdType::DelString as u8);
99        result.extend(&data_length.to_be_bytes()); // 数据长度
100        result.extend(data); // 数据
101
102        Ok(result)
103    }
104}