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
use std::fmt;
use std::{fmt::Debug, ops::Deref};
use log::trace;
use serde::{
de::{self, DeserializeOwned, Deserializer},
Deserialize,
};
use serde_json::value::Value;
use crate::{ArangoError, ClientError};
use super::aql::QueryStats;
pub(crate) fn serialize_response<T>(text: &str) -> Result<T, ClientError>
where
T: DeserializeOwned + Debug,
{
let response: Response<T> = serde_json::from_str(text)?;
Ok(Into::<Result<T, ArangoError>>::into(response)?)
}
#[derive(Debug)]
pub enum Response<T> {
Ok(Success<T>),
Err(ArangoError),
}
impl<T> Into<Result<T, ArangoError>> for Response<T> {
fn into(self) -> Result<T, ArangoError> {
match self {
Response::Ok(success) => Ok(success.result),
Response::Err(err) => Err(err),
}
}
}
impl<'de, T> Deserialize<'de> for Response<T>
where
T: Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let map = serde_json::Map::deserialize(deserializer)?;
trace!("Deserialize normal Response: {:?}", map);
let error = map
.get("error")
.ok_or_else(|| de::Error::missing_field("error"))
.map(Deserialize::deserialize)?
.map_err(de::Error::custom)?;
let rest = Value::Object(map);
if error {
ArangoError::deserialize(rest)
.map(Response::Err)
.map_err(de::Error::custom)
} else {
Success::<T>::deserialize(rest)
.map(Response::Ok)
.map_err(de::Error::custom)
}
}
}
#[derive(Deserialize, Debug)]
pub struct ArangoResult<T> {
#[serde(rename = "result")]
result: T,
}
impl<T> ArangoResult<T> {
pub fn unwrap(self) -> T {
self.result
}
}
impl<T> Deref for ArangoResult<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.result
}
}
#[derive(Deserialize, Debug)]
pub struct Success<T> {
pub error: bool,
pub code: u16,
#[serde(flatten)]
pub result: T,
}
impl<T: fmt::Display> fmt::Display for Success<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(format!("Response {} (Status: {})", &self.result, &self.code).as_str())
}
}
#[derive(Deserialize, Debug)]
pub struct Cursor<T> {
pub count: Option<usize>,
pub cached: bool,
#[serde(rename = "hasMore")]
pub more: bool,
pub result: Vec<T>,
pub id: Option<String>,
pub extra: Option<Extra>,
}
#[derive(Deserialize, Debug)]
pub struct Extra {
stats: Option<QueryStats>,
warnings: Option<Vec<Value>>,
}