liserk_shared/
message_type.rs1use std::fmt::Display;
2
3use serde::{Deserialize, Deserializer, Serialize};
4use tracing::debug;
5
6#[derive(Debug, Serialize)]
7#[repr(u8)]
8pub enum MessageType {
9 Setup,
10 Authentification,
11 Insert,
12 InsertOpe,
13 InsertResponse,
14 Query,
15 QueryResponse,
16 SingleValueResponse,
17 Count,
18 Update,
19 UpdateResponse,
20 Delete,
21 DeleteResult,
22 DeleteForUsecase,
23 Drop,
24 DropResult,
25 EndOfCommunication,
26 CloseCommunication,
27}
28
29impl Display for MessageType {
30 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31 match self {
32 MessageType::Setup => write!(f, "Setup communcication"),
33 MessageType::Authentification => write!(f, "Authentification"),
34 MessageType::Insert => write!(f, "Insert"),
35 MessageType::InsertOpe => write!(f, "InsertOpe"),
36 MessageType::InsertResponse => write!(f, "InsertResponse"),
37 MessageType::Query => write!(f, "Query"),
38 MessageType::QueryResponse => write!(f, "QueryResponse"),
39 MessageType::SingleValueResponse => write!(f, "SingleValueResponse"),
40 MessageType::Count => todo!(),
41 MessageType::Update => write!(f, "Update"),
42 MessageType::UpdateResponse => write!(f, "UpdateResponse"),
43 MessageType::Delete => write!(f, "Delete"),
44 MessageType::DeleteResult => write!(f, "DeleteResult"),
45 MessageType::DeleteForUsecase => write!(f, "DeleteForUsecase"),
46 MessageType::Drop => write!(f, "Drop"),
47 MessageType::DropResult => write!(f, "DropResult"),
48 MessageType::EndOfCommunication => write!(f, "EndOfCommunication"),
49 MessageType::CloseCommunication => write!(f, "CloseCommunication"),
50 }
51 }
52}
53
54impl<'de> Deserialize<'de> for MessageType {
55 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
56 where
57 D: Deserializer<'de>,
58 {
59 let s = String::deserialize(deserializer)?;
60 debug!("parsing message type: {}", s);
61
62 if s == "Setup" {
63 return Ok(MessageType::Setup);
64 }
65
66 if s == "Authentification" {
67 return Ok(MessageType::Authentification);
68 }
69
70 if s == "Insert" {
71 return Ok(MessageType::Insert);
72 }
73
74 if s == "InsertOpe" {
75 return Ok(MessageType::InsertOpe);
76 }
77
78 if s == "InsertResponse" {
79 return Ok(MessageType::InsertResponse);
80 }
81
82 if s == "Query" {
83 return Ok(MessageType::Query);
84 }
85
86 if s == "QueryResponse" {
87 return Ok(MessageType::QueryResponse);
88 }
89
90 if s == "SingleValueResponse" {
91 return Ok(MessageType::SingleValueResponse);
92 }
93
94 if s == "Count" {
95 return Ok(MessageType::Count);
96 }
97
98 if s == "Update" {
99 return Ok(MessageType::Update);
100 }
101
102 if s == "UpdateResponse" {
103 return Ok(MessageType::UpdateResponse);
104 }
105
106 if s == "Delete" {
107 return Ok(MessageType::Delete);
108 }
109
110 if s == "DeleteResult" {
111 return Ok(MessageType::DeleteResult);
112 }
113
114 if s == "DeleteForUsecase" {
115 return Ok(MessageType::DeleteForUsecase);
116 }
117
118 if s == "Drop" {
119 return Ok(MessageType::Drop);
120 }
121
122 if s == "DeleteResult" {
123 return Ok(MessageType::DropResult);
124 }
125
126 if s == "EndOfCommunication" {
127 return Ok(MessageType::EndOfCommunication);
128 }
129
130 if s == "CloseCommunication" {
131 return Ok(MessageType::CloseCommunication);
132 }
133 panic!("panic deserialize message type");
134 }
135}
136
137#[derive(Debug, Default, thiserror::Error)]
138#[error("fail to parse MessageType")]
139pub struct MessageTypeError {}
140
141impl TryFrom<u8> for MessageType {
142 type Error = MessageTypeError;
143
144 fn try_from(v: u8) -> Result<Self, Self::Error> {
145 match v {
146 0 => Ok(MessageType::Setup),
147 1 => Ok(MessageType::Authentification),
148 2 => Ok(MessageType::Insert),
149 3 => Ok(MessageType::InsertResponse),
150 4 => Ok(MessageType::Query),
151 5 => Ok(MessageType::QueryResponse),
152 6 => Ok(MessageType::SingleValueResponse),
153 7 => Ok(MessageType::Count),
154 8 => Ok(MessageType::Update),
155 9 => Ok(MessageType::UpdateResponse),
156 10 => Ok(MessageType::Delete),
157 11 => Ok(MessageType::DeleteResult),
158 12 => Ok(MessageType::DeleteForUsecase),
159 13 => Ok(MessageType::Drop),
160 14 => Ok(MessageType::DropResult),
161 15 => Ok(MessageType::EndOfCommunication),
162 16 => Ok(MessageType::CloseCommunication),
163 17 => Ok(MessageType::InsertOpe),
164 _ => Err(MessageTypeError::default()),
165 }
166 }
167}