orientdb_client/common/protocol/messages/
response.rs

1use crate::common::types::live::LiveResult;
2use crate::common::types::result::OResult;
3use std::collections::HashMap;
4use std::collections::VecDeque;
5
6#[derive(Debug)]
7pub struct Response {
8    pub header: Header,
9    pub payload: ResponseType,
10}
11
12pub trait Payload {
13    fn consume(payload: &mut ResponseType) -> Self;
14}
15
16impl Response {
17    pub fn new(header: Header, payload: ResponseType) -> Response {
18        Response { header, payload }
19    }
20    pub fn empty() -> Response {
21        Response {
22            header: Header {
23                status: Status::OK,
24                client_id: None,
25                session_id: -1,
26                token: None,
27                op: -1,
28            },
29            payload: ResponseType::Empty,
30        }
31    }
32
33    pub fn payload<T>(&mut self) -> T
34    where
35        T: Payload,
36    {
37        T::consume(&mut self.payload)
38    }
39}
40
41#[derive(Debug)]
42pub struct Open {
43    pub session_id: i32,
44    pub token: Option<Vec<u8>>,
45}
46
47impl Open {
48    pub fn new(session_id: i32, token: Option<Vec<u8>>) -> Open {
49        Open { session_id, token }
50    }
51}
52
53impl From<Open> for ResponseType {
54    fn from(input: Open) -> ResponseType {
55        ResponseType::Open(Some(input))
56    }
57}
58
59#[derive(Debug)]
60pub struct Connect {
61    pub session_id: i32,
62    pub token: Option<Vec<u8>>,
63}
64
65impl Connect {
66    pub fn new(session_id: i32, token: Option<Vec<u8>>) -> Connect {
67        Connect { session_id, token }
68    }
69}
70
71impl From<Connect> for ResponseType {
72    fn from(input: Connect) -> ResponseType {
73        ResponseType::Connect(Some(input))
74    }
75}
76
77#[derive(Debug)]
78pub struct LiveQueryResult {
79    pub monitor_id: i32,
80    pub ended: bool,
81    pub events: Vec<LiveResult>,
82}
83
84impl LiveQueryResult {
85    pub fn new(monitor_id: i32, ended: bool, events: Vec<LiveResult>) -> LiveQueryResult {
86        LiveQueryResult {
87            monitor_id,
88            ended,
89            events,
90        }
91    }
92}
93
94impl From<LiveQueryResult> for ResponseType {
95    fn from(input: LiveQueryResult) -> ResponseType {
96        ResponseType::LiveQueryResult(Some(input))
97    }
98}
99
100#[derive(Debug)]
101pub struct LiveQuery {
102    pub monitor_id: i32,
103}
104
105impl From<LiveQuery> for ResponseType {
106    fn from(input: LiveQuery) -> ResponseType {
107        ResponseType::LiveQuery(Some(input))
108    }
109}
110#[derive(Debug)]
111pub struct Query {
112    pub query_id: String,
113    pub tx_changes: bool,
114    pub execution_plan: Option<OResult>,
115    pub records: VecDeque<OResult>,
116    pub has_next: bool,
117    pub stats: HashMap<String, i64>,
118}
119
120impl Query {
121    pub fn new<T>(
122        query_id: T,
123        tx_changes: bool,
124        execution_plan: Option<OResult>,
125        records: VecDeque<OResult>,
126        has_next: bool,
127        stats: HashMap<String, i64>,
128    ) -> Query
129    where
130        T: Into<String>,
131    {
132        Query {
133            query_id: query_id.into(),
134            tx_changes,
135            execution_plan,
136            records,
137            has_next,
138            stats,
139        }
140    }
141}
142
143impl From<Query> for ResponseType {
144    fn from(input: Query) -> ResponseType {
145        ResponseType::Query(Some(input))
146    }
147}
148
149#[derive(Debug)]
150pub struct CreateDB {}
151
152impl From<CreateDB> for ResponseType {
153    fn from(input: CreateDB) -> ResponseType {
154        ResponseType::CreateDB(Some(input))
155    }
156}
157
158#[derive(Debug)]
159pub struct DropDB {}
160
161impl From<DropDB> for ResponseType {
162    fn from(input: DropDB) -> ResponseType {
163        ResponseType::DropDB(Some(input))
164    }
165}
166
167#[derive(Debug)]
168pub struct ExistDB {
169    pub exist: bool,
170}
171
172impl ExistDB {
173    pub fn new(exist: bool) -> Self {
174        ExistDB { exist }
175    }
176}
177
178impl From<ExistDB> for ResponseType {
179    fn from(input: ExistDB) -> ResponseType {
180        ResponseType::ExistDB(Some(input))
181    }
182}
183
184#[derive(Debug)]
185pub struct QueryClose {}
186
187impl From<QueryClose> for ResponseType {
188    fn from(input: QueryClose) -> ResponseType {
189        ResponseType::QueryClose(Some(input))
190    }
191}
192
193#[allow(clippy::large_enum_variant)]
194#[derive(Debug)]
195pub enum ResponseType {
196    Empty,
197    Open(Option<Open>),
198    Connect(Option<Connect>),
199    Query(Option<Query>),
200    CreateDB(Option<CreateDB>),
201    ExistDB(Option<ExistDB>),
202    DropDB(Option<DropDB>),
203    LiveQuery(Option<LiveQuery>),
204    LiveQueryResult(Option<LiveQueryResult>),
205    QueryClose(Option<QueryClose>),
206}
207
208#[derive(Debug, PartialEq)]
209pub enum Status {
210    OK,
211    ERROR,
212    PUSH,
213}
214
215impl From<i8> for Status {
216    fn from(status: i8) -> Self {
217        match status {
218            0 => Status::OK,
219            3 => Status::PUSH,
220            _ => Status::ERROR,
221        }
222    }
223}
224
225#[derive(Debug, PartialEq)]
226pub struct Header {
227    pub status: Status,
228    pub client_id: Option<i32>,
229    pub session_id: i32,
230    pub token: Option<Vec<u8>>,
231    pub op: i8,
232}
233
234macro_rules! impl_payload {
235    ($s:ident) => {
236        impl Payload for $s {
237            fn consume(payload: &mut ResponseType) -> $s {
238                match payload {
239                    ResponseType::$s(ref mut this) => this
240                        .take()
241                        .expect(&format!("Response does not contain {:?}", this)),
242                    _ => panic!(
243                        "Cannot get an {} response from {:?}",
244                        stringify!($s),
245                        payload
246                    ),
247                }
248            }
249        }
250    };
251}
252
253impl_payload!(Open);
254impl_payload!(Query);
255impl_payload!(QueryClose);
256impl_payload!(CreateDB);
257impl_payload!(DropDB);
258impl_payload!(ExistDB);
259impl_payload!(Connect);
260impl_payload!(LiveQuery);
261impl_payload!(LiveQueryResult);