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); result.extend(&VERSION.to_be_bytes()); result.push(CmdType::SetString as u8);
35 result.extend(&data_length.to_be_bytes()); result.extend(data); 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); result.extend(&VERSION.to_be_bytes()); result.push(CmdType::GetString as u8);
67 result.extend(&data_length.to_be_bytes()); result.extend(data); 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); result.extend(&VERSION.to_be_bytes()); result.push(CmdType::DelString as u8);
99 result.extend(&data_length.to_be_bytes()); result.extend(data); Ok(result)
103 }
104}