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
#![allow(clippy::used_underscore_binding)]
use crate::{DatabaseRecord, Error, Record};
use arangors_lite::document::response::DocumentResponse;
use serde::{Deserialize, Serialize};
use std::convert::TryInto;
#[derive(Serialize, Deserialize)]
pub struct DatabaseRecordDto<T> {
#[serde(rename = "_key")]
#[serde(skip_serializing_if = "Option::is_none")]
key: Option<String>,
#[serde(flatten)]
pub record: T,
}
impl<T: Record> DatabaseRecordDto<T> {
#[inline]
#[must_use]
#[allow(clippy::missing_const_for_fn)]
pub fn new(record: T, key: Option<String>) -> Self {
Self { key, record }
}
}
impl<T: Record> TryInto<DatabaseRecord<T>> for DocumentResponse<DatabaseRecord<T>> {
type Error = Error;
fn try_into(self) -> Result<DatabaseRecord<T>, Self::Error> {
match self {
Self::Silent => Err(Error::InternalError {
message: Some(String::from("Received unexpected silent document response")),
}),
Self::Response { new, header, .. } => match new {
Some(value) => Ok(value),
None => Err(Error::InternalError {
message: Some(format!(
"Expected `ArangoDB` to return the new {} document",
header._id
)),
}),
},
}
}
}
impl<T: Record> TryInto<DatabaseRecord<T>> for DocumentResponse<DatabaseRecordDto<T>> {
type Error = Error;
fn try_into(self) -> Result<DatabaseRecord<T>, Self::Error> {
match self {
Self::Silent => Err(Error::InternalError {
message: Some(String::from("Received unexpected silent document response")),
}),
Self::Response { header, new, .. } => {
let record = match new {
Some(doc) => doc.record,
None => {
return Err(Error::InternalError {
message: Some(format!(
"Expected `ArangoDB` to return the new {} document",
header._id
)),
});
}
};
Ok(DatabaseRecord {
key: header._key.clone(),
id: header._id.clone(),
rev: header._rev,
record,
})
}
}
}
}