1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
pub mod request;
pub mod response;
pub mod structs;
use std::io::Cursor;
pub use talk_loco_commands_derive::BsonData;
use loco_protocol::command::{self, CommandData};
use serde::{de::DeserializeOwned, Serialize, Deserialize};
pub trait BsonData: Serialize + DeserializeOwned {
fn method() -> &'static str;
}
pub struct BsonReqData<D>(D);
impl<D: BsonData> CommandData for BsonReqData<D> {
fn method(&self) -> &'static str {
D::method()
}
fn encode(&self) -> Result<Vec<u8>, command::Error> {
Ok(self.serialize()?)
}
fn decode(data: &Vec<u8>) -> Result<Self, command::Error> {
Ok(Self::deserialize(data)?)
}
}
impl<D: BsonData> BsonReqData<D> {
pub fn new(data: D) -> Self {
Self(data)
}
pub fn unwrap(self) -> D {
self.0
}
pub fn as_ref(&self) -> &D {
&self.0
}
pub fn as_mut(&mut self) -> &mut D {
&mut self.0
}
fn serialize(&self) -> Result<Vec<u8>, BsonError> {
let mut encoded = Vec::new();
let doc = bson::to_document(&self.0)?;
doc.to_writer(&mut encoded)?;
Ok(encoded)
}
fn deserialize(data: &Vec<u8>) -> Result<Self, BsonError> {
let doc = bson::Document::from_reader(&mut Cursor::new(data))?;
let decoded = bson::from_document::<D>(doc)?;
Ok(Self::new(decoded))
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BsonResData<D> {
status: i16,
#[serde(flatten)]
response: Option<D>
}
impl<D: BsonData> CommandData for BsonResData<D> {
fn method(&self) -> &'static str {
D::method()
}
fn encode(&self) -> Result<Vec<u8>, command::Error> {
Ok(self.serialize()?)
}
fn decode(data: &Vec<u8>) -> Result<Self, command::Error> {
Ok(Self::deserialize(data)?)
}
}
impl<D: BsonData> BsonResData<D> {
pub fn new(status: i16, response: Option<D>) -> Self {
Self {
status,
response
}
}
pub fn status(&self) -> i16 {
self.status
}
pub fn response(self) -> Option<D> {
self.response
}
pub fn as_ref(&self) -> Option<&D> {
self.response.as_ref()
}
pub fn as_mut(&mut self) -> Option<&mut D> {
self.response.as_mut()
}
fn serialize(&self) -> Result<Vec<u8>, BsonError> {
let mut encoded = Vec::new();
let doc = bson::to_document(&self)?;
doc.to_writer(&mut encoded)?;
Ok(encoded)
}
fn deserialize(data: &Vec<u8>) -> Result<Self, BsonError> {
let doc = bson::Document::from_reader(&mut Cursor::new(data))?;
let decoded = bson::from_document::<Self>(doc)?;
Ok(decoded)
}
}
pub(crate) enum BsonError {
Encode(bson::ser::Error),
Decode(bson::de::Error)
}
impl From<bson::ser::Error> for BsonError {
fn from(err: bson::ser::Error) -> Self {
Self::Encode(err)
}
}
impl From<bson::de::Error> for BsonError {
fn from(err: bson::de::Error) -> Self {
Self::Decode(err)
}
}
impl From<BsonError> for command::Error {
fn from(err: BsonError) -> Self {
match err {
BsonError::Encode(err) => Self::Encode(err.to_string()),
BsonError::Decode(err) => Self::Decode(err.to_string())
}
}
}