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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright (c) 2021 ruarango developers
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

//! Document Output Structs

use getset::Getters;
use serde::{Deserialize, Serialize};
use std::fmt;
#[cfg(test)]
use {
    crate::{error::RuarangoErr::InvalidMock, utils::mocks::Mock},
    anyhow::Result,
};

/// Document metadata output
#[derive(Clone, Debug, Deserialize, Getters, Serialize)]
#[getset(get = "pub")]
pub struct DocMeta<N, O> {
    /// Contains the document key
    #[serde(rename = "_key")]
    key: String,
    /// Contains the document identifier of the newly created document
    #[serde(rename = "_id")]
    id: String,
    /// Contains the document revision
    #[serde(rename = "_rev")]
    rev: String,
    /// Contains the old document revision, for some `overwrite`s
    #[serde(rename = "_oldRev", skip_serializing_if = "Option::is_none")]
    old_rev: Option<String>,
    /// Contains the new document, if `return_new` was enabled
    #[serde(rename = "new", skip_serializing_if = "Option::is_none")]
    new_doc: Option<N>,
    /// Contains the old document, if `return_old` was enabled, and the
    /// [`overwrite`](crate::doc::input::OverwriteMode) mode supports it.
    #[serde(rename = "old", skip_serializing_if = "Option::is_none")]
    old_doc: Option<O>,
}

#[cfg(test)]
impl Default for DocMeta<(), ()> {
    fn default() -> Self {
        Self {
            key: "abc".to_string(),
            id: "def".to_string(),
            rev: "ghi".to_string(),
            old_rev: None,
            new_doc: None,
            old_doc: None,
        }
    }
}

#[cfg(test)]
impl Default for DocMeta<OutputDoc, ()> {
    fn default() -> Self {
        Self {
            key: "abc".to_string(),
            id: "def".to_string(),
            rev: "ghi".to_string(),
            old_rev: None,
            new_doc: Some(OutputDoc::default()),
            old_doc: None,
        }
    }
}

#[cfg(test)]
impl Default for DocMeta<OutputDoc, OutputDoc> {
    fn default() -> Self {
        Self {
            key: "abc".to_string(),
            id: "def".to_string(),
            rev: "ghi".to_string(),
            old_rev: Some("ghi".to_string()),
            new_doc: Some(OutputDoc::default()),
            old_doc: Some(OutputDoc::default()),
        }
    }
}

#[cfg(test)]
#[derive(Clone, Debug, Deserialize, Getters, Serialize)]
#[getset(get = "pub(crate)")]
pub(crate) struct OutputDoc {
    #[serde(rename = "_key")]
    key: String,
    #[serde(rename = "_id")]
    id: String,
    #[serde(rename = "_rev")]
    rev: String,
    test: String,
}

#[cfg(test)]
impl Default for OutputDoc {
    fn default() -> Self {
        Self {
            key: "abc".to_string(),
            id: "def".to_string(),
            rev: "ghi".to_string(),
            test: "test".to_string(),
        }
    }
}

#[cfg(test)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum CreateMockKind {
    FirstCreate,
    SecondCreate,
    NewDoc,
    NewOldDoc,
}

#[cfg(test)]
impl Mock<CreateMockKind> for DocMeta<(), ()> {
    fn try_mock(name: CreateMockKind) -> Result<Self> {
        match name {
            CreateMockKind::FirstCreate => Ok(DocMeta::<(), ()> {
                key: "test_key".to_string(),
                ..Default::default()
            }),
            CreateMockKind::SecondCreate => Ok(DocMeta::<(), ()> {
                key: "test_key".to_string(),
                old_rev: Some("ghi".to_string()),
                ..Default::default()
            }),
            _ => Err(InvalidMock.into()),
        }
    }
}

#[cfg(test)]
impl Mock<CreateMockKind> for DocMeta<OutputDoc, ()> {
    fn try_mock(name: CreateMockKind) -> Result<Self> {
        match name {
            CreateMockKind::NewDoc => Ok(Self::default()),
            _ => Err(InvalidMock.into()),
        }
    }
}

#[cfg(test)]
impl Mock<CreateMockKind> for DocMeta<OutputDoc, OutputDoc> {
    fn try_mock(name: CreateMockKind) -> Result<Self> {
        match name {
            CreateMockKind::NewOldDoc => Ok(DocMeta::<OutputDoc, OutputDoc> {
                key: "test_key".to_string(),
                ..Default::default()
            }),
            _ => Err(InvalidMock.into()),
        }
    }
}

#[cfg(test)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum ReadMockKind {
    Found,
}

#[cfg(test)]
impl Mock<ReadMockKind> for OutputDoc {
    fn try_mock(name: ReadMockKind) -> Result<Self>
    where
        Self: Sized,
    {
        Ok(match name {
            ReadMockKind::Found => OutputDoc::default(),
        })
    }
}

/// Output on a precondition failure for some endpoints
#[derive(Clone, Debug, Deserialize, Eq, Getters, PartialEq, Serialize)]
#[getset(get = "pub")]
pub struct DocErr {
    /// Is this an error?
    error: bool,
    /// The error code
    code: u16,
    /// The ArangoDB code
    #[serde(rename = "errorNum")]
    error_num: usize,
    /// The error message
    #[serde(rename = "errorMessage", skip_serializing_if = "Option::is_none")]
    error_message: Option<String>,
    /// Contains the document key
    #[serde(rename = "_key", skip_serializing_if = "Option::is_none")]
    key: Option<String>,
    /// Contains the document identifier of the newly created document
    #[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
    id: Option<String>,
    /// Contains the document revision
    #[serde(rename = "_rev", skip_serializing_if = "Option::is_none")]
    rev: Option<String>,
}

impl fmt::Display for DocErr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "error: {}", self.error)?;
        write!(f, ", code: {}", self.code)?;
        write!(f, ", error_num: {}", self.error_num)?;
        if let Some(error_message) = &self.error_message {
            write!(f, ", error_message: {}", error_message)?;
        }
        if let Some(key) = &self.key {
            write!(f, ", key: {}", key)?;
        }
        if let Some(id) = &self.id {
            write!(f, ", id: {}", id)?;
        }
        if let Some(rev) = &self.rev {
            write!(f, ", rev: {}", rev)?;
        }
        Ok(())
    }
}