1use std::fmt;
9
10#[derive(Debug, Clone, PartialEq)]
14pub enum JsonValue {
15 Null,
16 Bool(bool),
17 Number(f64),
18 String(String),
19 Array(Vec<JsonValue>),
20 Object(Vec<(String, JsonValue)>),
21}
22
23impl JsonValue {
24 pub fn null() -> Self {
25 JsonValue::Null
26 }
27
28 pub fn bool(b: bool) -> Self {
29 JsonValue::Bool(b)
30 }
31
32 pub fn number(n: impl Into<f64>) -> Self {
33 JsonValue::Number(n.into())
34 }
35
36 pub fn string(s: impl Into<String>) -> Self {
37 JsonValue::String(s.into())
38 }
39
40 pub fn object<I, K>(entries: I) -> Self
41 where
42 I: IntoIterator<Item = (K, JsonValue)>,
43 K: Into<String>,
44 {
45 JsonValue::Object(entries.into_iter().map(|(k, v)| (k.into(), v)).collect())
46 }
47
48 pub fn array<I>(items: I) -> Self
49 where
50 I: IntoIterator<Item = JsonValue>,
51 {
52 JsonValue::Array(items.into_iter().collect())
53 }
54
55 pub fn as_object(&self) -> Option<&[(String, JsonValue)]> {
56 match self {
57 JsonValue::Object(entries) => Some(entries.as_slice()),
58 _ => None,
59 }
60 }
61
62 pub fn to_json_string(&self) -> String {
63 let mut out = String::new();
64 write_json(self, &mut out);
65 out
66 }
67}
68
69fn write_json(value: &JsonValue, out: &mut String) {
70 match value {
71 JsonValue::Null => out.push_str("null"),
72 JsonValue::Bool(b) => out.push_str(if *b { "true" } else { "false" }),
73 JsonValue::Number(n) => {
74 if n.fract() == 0.0 && n.is_finite() {
75 out.push_str(&format!("{}", *n as i64));
76 } else {
77 out.push_str(&format!("{n}"));
78 }
79 }
80 JsonValue::String(s) => {
81 out.push('"');
82 for c in s.chars() {
83 match c {
84 '"' => out.push_str("\\\""),
85 '\\' => out.push_str("\\\\"),
86 '\n' => out.push_str("\\n"),
87 '\r' => out.push_str("\\r"),
88 '\t' => out.push_str("\\t"),
89 c if (c as u32) < 0x20 => {
90 out.push_str(&format!("\\u{:04x}", c as u32));
91 }
92 c => out.push(c),
93 }
94 }
95 out.push('"');
96 }
97 JsonValue::Array(items) => {
98 out.push('[');
99 for (i, item) in items.iter().enumerate() {
100 if i > 0 {
101 out.push(',');
102 }
103 write_json(item, out);
104 }
105 out.push(']');
106 }
107 JsonValue::Object(entries) => {
108 out.push('{');
109 for (i, (k, v)) in entries.iter().enumerate() {
110 if i > 0 {
111 out.push(',');
112 }
113 write_json(&JsonValue::String(k.clone()), out);
114 out.push(':');
115 write_json(v, out);
116 }
117 out.push('}');
118 }
119 }
120}
121
122#[derive(Debug, Clone, PartialEq)]
125pub enum ValueOut {
126 Null,
127 Bool(bool),
128 Integer(i64),
129 Float(f64),
130 String(String),
131}
132
133impl fmt::Display for ValueOut {
134 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
135 match self {
136 ValueOut::Null => f.write_str("null"),
137 ValueOut::Bool(b) => write!(f, "{b}"),
138 ValueOut::Integer(n) => write!(f, "{n}"),
139 ValueOut::Float(n) => write!(f, "{n}"),
140 ValueOut::String(s) => write!(f, "{s}"),
141 }
142 }
143}
144
145#[derive(Debug, Clone)]
148pub struct QueryResult {
149 pub statement: String,
150 pub affected: u64,
151 pub columns: Vec<String>,
152 pub rows: Vec<Vec<(String, ValueOut)>>,
153}
154
155pub type Row = Vec<(String, ValueOut)>;
156
157#[derive(Debug, Clone, Default)]
158pub struct ListOptions<'a> {
159 pub filter: Option<&'a str>,
160 pub order_by: Option<&'a str>,
161 pub limit: Option<u64>,
162}
163
164impl<'a> ListOptions<'a> {
165 pub fn new() -> Self {
166 Self::default()
167 }
168
169 pub fn filter(mut self, filter: &'a str) -> Self {
170 self.filter = Some(filter);
171 self
172 }
173
174 pub fn order_by(mut self, order_by: &'a str) -> Self {
175 self.order_by = Some(order_by);
176 self
177 }
178
179 pub fn limit(mut self, limit: u64) -> Self {
180 self.limit = Some(limit);
181 self
182 }
183}
184
185#[derive(Debug, Clone)]
186pub struct ListResult {
187 pub items: Vec<Row>,
188 pub affected: u64,
189}
190
191#[derive(Debug, Clone, PartialEq, Eq)]
192pub struct DeleteResult {
193 pub affected: u64,
194 pub deleted: bool,
195}
196
197#[derive(Debug, Clone, PartialEq, Eq)]
198pub struct ExistsResult {
199 pub exists: bool,
200}
201
202#[derive(Debug, Clone, PartialEq)]
203pub struct DocumentItem {
204 pub rid: String,
205 pub fields: Row,
206}
207
208#[derive(Debug, Clone, PartialEq)]
209pub struct KvItem {
210 pub collection: String,
211 pub key: String,
212 pub value: ValueOut,
213}
214
215#[derive(Debug, Clone, PartialEq)]
216pub struct KvWatchEvent {
217 pub key: String,
218 pub op: String,
219 pub before: serde_json::Value,
220 pub after: serde_json::Value,
221 pub lsn: u64,
222 pub committed_at: u64,
223 pub dropped_event_count: u64,
224}
225
226impl QueryResult {
227 pub fn from_envelope(value: serde_json::Value) -> Self {
230 let Some(obj) = value.as_object() else {
231 return Self {
232 statement: String::new(),
233 affected: 0,
234 columns: Vec::new(),
235 rows: Vec::new(),
236 };
237 };
238 let result_obj = obj.get("result").and_then(|v| v.as_object()).unwrap_or(obj);
239 let statement = obj
240 .get("statement")
241 .or_else(|| obj.get("statement_type"))
242 .and_then(|v| v.as_str())
243 .unwrap_or("")
244 .to_string();
245 let affected = obj
246 .get("affected")
247 .or_else(|| obj.get("affected_rows"))
248 .and_then(|v| v.as_u64())
249 .unwrap_or(0);
250 let columns: Vec<String> = result_obj
251 .get("columns")
252 .and_then(|v| v.as_array())
253 .map(|cols| {
254 cols.iter()
255 .filter_map(|col| col.as_str().map(ToOwned::to_owned))
256 .collect::<Vec<_>>()
257 })
258 .unwrap_or_default();
259 let row_values = result_obj
260 .get("records")
261 .or_else(|| result_obj.get("rows"))
262 .and_then(|v| v.as_array());
263 let rows = row_values
264 .map(|records| {
265 records
266 .iter()
267 .map(|record| parse_record(record, &columns))
268 .collect()
269 })
270 .unwrap_or_default();
271 Self {
272 statement,
273 affected,
274 columns,
275 rows,
276 }
277 }
278}
279
280fn parse_record(record: &serde_json::Value, columns: &[String]) -> Vec<(String, ValueOut)> {
281 let Some(record_obj) = record.as_object() else {
282 return Vec::new();
283 };
284 let values = record_obj
285 .get("values")
286 .and_then(|v| v.as_object())
287 .unwrap_or(record_obj);
288 if columns.is_empty() {
289 return values
290 .iter()
291 .map(|(key, value)| (key.clone(), json_to_value_out(value)))
292 .collect();
293 }
294 columns
295 .iter()
296 .map(|column| {
297 (
298 column.clone(),
299 values
300 .get(column)
301 .map(json_to_value_out)
302 .unwrap_or(ValueOut::Null),
303 )
304 })
305 .collect()
306}
307
308fn json_to_value_out(value: &serde_json::Value) -> ValueOut {
309 match value {
310 serde_json::Value::Null => ValueOut::Null,
311 serde_json::Value::Bool(value) => ValueOut::Bool(*value),
312 serde_json::Value::Number(value) => {
313 if let Some(n) = value.as_i64() {
314 ValueOut::Integer(n)
315 } else if let Some(n) = value.as_f64() {
316 ValueOut::Float(n)
317 } else {
318 ValueOut::String(value.to_string())
319 }
320 }
321 serde_json::Value::String(value) => ValueOut::String(value.clone()),
322 serde_json::Value::Array(_) | serde_json::Value::Object(_) => {
323 ValueOut::String(value.to_string())
324 }
325 }
326}
327
328#[derive(Debug, Clone)]
329pub struct InsertResult {
330 pub affected: u64,
331 pub rid: Option<String>,
333 pub id: Option<String>,
335}
336
337#[derive(Debug, Clone, PartialEq, Eq)]
338pub struct BulkInsertResult {
339 pub affected: u64,
340 pub rids: Vec<String>,
342 pub ids: Vec<String>,
344}