ckeylock_core/
response.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Serialize, Deserialize)]
4pub enum ResponseStatus {
5 Success,
6 Error,
7 NotFound,
8 Unauthorized,
9}
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct Response {
13 message: String,
14 data: Option<ResponseData>,
15 reqid: Vec<u8>,
16}
17
18impl Response {
19 pub fn new(data: Option<ResponseData>, message: &str, reqid: Vec<u8>) -> Self {
20 Self {
21 message: message.to_string(),
22 data,
23 reqid,
24 }
25 }
26 pub fn data(&self) -> Option<&ResponseData> {
27 self.data.as_ref()
28 }
29 pub fn to_string(&self) -> String {
30 serde_json::to_string(self).unwrap()
31 }
32 pub fn reqid(&self) -> Vec<u8> {
33 self.reqid.clone()
34 }
35}
36
37#[derive(Debug, Clone, Serialize, Deserialize)]
38pub struct ErrorResponse {
39 pub message: String,
40 pub reqid: Vec<u8>,
41}
42impl ErrorResponse {
43 pub fn to_string(&self) -> String {
44 serde_json::to_string(self).unwrap()
45 }
46}
47
48#[derive(Debug, Clone, Serialize, Deserialize)]
49pub enum ResponseData {
50 SetResponse { key: Vec<u8> },
51 GetResponse { value: Option<Vec<u8>> },
52 DeleteResponse { key: Option<Vec<u8>> },
53 ListResponse { keys: Vec<Vec<u8>> },
54 ExistsResponse { exists: bool },
55 CountResponse { count: usize },
56 ClearResponse,
57}