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, seq: u32) -> 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(&seq.to_be_bytes()); // 序列号
36        result.extend(&data_length.to_be_bytes()); // 数据长度
37        result.extend(data); // 数据
38
39        Ok(result)
40    }
41}
42
43#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
44pub struct GetString {
45    pub db: String,
46    pub key: String,
47}
48
49#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
50pub struct ResultGetString {
51    pub value: Option<String>,
52    pub expire: Option<u64>,
53}
54
55impl ResultGetString {
56    pub fn new(value: Option<String>, expire: Option<u64>) -> Self {
57        Self { value, expire }
58    }
59
60    pub fn to_result(&self, seq: u32) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
61        let data = serde_json::to_vec(self)?;
62        let data_length = data.len() as u32;
63
64        let mut result = vec![];
65        result.extend(&MAGIC_NUMBER); // 魔数
66        result.extend(&VERSION.to_be_bytes()); // 版本
67        result.push(CmdType::GetString as u8);
68        result.extend(&seq.to_be_bytes()); // 序列号
69        result.extend(&data_length.to_be_bytes()); // 数据长度
70        result.extend(data); // 数据
71
72        Ok(result)
73    }
74}
75
76#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
77pub struct DelString {
78    pub db: String,
79    pub key: String,
80}
81
82#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
83pub struct ResultDelString {
84    pub value: Option<String>,
85    pub expire: Option<u64>,
86}
87
88impl ResultDelString {
89    pub fn new(value: Option<String>, expire: Option<u64>) -> Self {
90        Self { value, expire }
91    }
92
93    pub fn to_result(&self, seq: u32) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
94        let data = serde_json::to_vec(self)?;
95        let data_length = data.len() as u32;
96
97        let mut result = vec![];
98        result.extend(&MAGIC_NUMBER); // 魔数
99        result.extend(&VERSION.to_be_bytes()); // 版本
100        result.push(CmdType::DelString as u8);
101        result.extend(&seq.to_be_bytes()); // 序列号
102        result.extend(&data_length.to_be_bytes()); // 数据长度
103        result.extend(data); // 数据
104
105        Ok(result)
106    }
107}