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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// 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 Replace Input Structs

use crate::{
    error::RuarangoErr::Unreachable,
    model::{
        add_qp, AddHeaders, BuildUrl,
        QueryParam::{IgnoreRevs, ReturnNew, ReturnOld, Silent, WaitForSync},
    },
    Connection,
};
use anyhow::{Context, Result};
use derive_builder::Builder;
use getset::Getters;
use reqwest::{
    header::{HeaderMap, HeaderName, HeaderValue},
    Url,
};
use serde::{Deserialize, Serialize};

/// Document replace configuration
#[derive(Builder, Clone, Debug, Default, Deserialize, Getters, Serialize)]
#[getset(get = "pub(crate)")]
pub struct Config<T> {
    /// The collection to replace the document in
    #[builder(setter(into))]
    collection: String,
    /// The _key of the document to replace
    #[builder(setter(into))]
    key: String,
    /// The patch document
    document: T,
    /// Wait until the delete operation has been synced to disk.
    #[builder(setter(strip_option), default)]
    wait_for_sync: Option<bool>,
    /// By default, or if this is set to true, the `_rev` attribute in
    /// the given document is ignored. If this is set to false, then
    /// the `_rev` attribute given in the body document is taken as a
    /// precondition. The document is only replaced if the current revision
    /// is the one specified.
    #[builder(setter(strip_option), default)]
    ignore_revs: Option<bool>,
    /// Additionally return the complete new document under the attribute `new`
    /// in the result.
    #[builder(setter(strip_option), default)]
    return_new: Option<bool>,
    /// Additionally return the complete old document under the attribute `old`
    /// in the result.
    #[builder(setter(strip_option), default)]
    return_old: Option<bool>,
    /// If set to true, an empty object will be returned as response. No meta-data
    /// will be returned for the replaced document. This option can be used to
    /// save some network traffic.
    #[builder(setter(strip_option), default)]
    silent: Option<bool>,
    /// You can conditionally replace a document based on a target `rev` by
    /// using the `if_match` option
    #[builder(setter(into, strip_option), default)]
    if_match: Option<String>,
}

impl<T> Config<T> {
    fn build_suffix(&self, base: &str) -> String {
        let mut url = format!("{}/{}/{}", base, self.collection, self.key);
        let mut has_qp = false;

        add_qp(*self.wait_for_sync(), &mut url, &mut has_qp, WaitForSync);

        if self.silent().is_some() {
            add_qp(*self.silent(), &mut url, &mut has_qp, Silent);
        } else {
            add_qp(*self.return_new(), &mut url, &mut has_qp, ReturnNew);
            add_qp(*self.return_old(), &mut url, &mut has_qp, ReturnOld);
        }

        add_qp(*self.ignore_revs(), &mut url, &mut has_qp, IgnoreRevs);

        url
    }
}

impl<T> BuildUrl for Config<T> {
    fn build_url(&self, base: &str, conn: &Connection) -> Result<Url> {
        let suffix = &self.build_suffix(base);
        conn.db_url()
            .join(suffix)
            .with_context(|| format!("Unable to build '{}' url", suffix))
    }
}

impl<T> AddHeaders for Config<T> {
    fn has_header(&self) -> bool {
        self.if_match.is_some()
    }

    fn add_headers(&self) -> Result<Option<HeaderMap>> {
        let mut headers = None;
        if self.has_header() {
            let mut headers_map = HeaderMap::new();
            if let Some(rev) = self.if_match() {
                let _ = headers_map.append(
                    HeaderName::from_static("if-match"),
                    HeaderValue::from_str(rev)?,
                );
                headers = Some(headers_map);
            } else {
                return Err(Unreachable {
                    msg: "'if_match' should be true!".to_string(),
                }
                .into());
            }
        }
        Ok(headers)
    }
}

#[cfg(test)]
mod test {
    use super::{Config, ConfigBuilder};
    use crate::model::{
        doc::BASE_DOC_SUFFIX, AddHeaders, RETURN_NEW_QP, RETURN_OLD_QP, SILENT_QP, TEST_COLL,
        TEST_KEY, WAIT_FOR_SYNC_QP,
    };
    use anyhow::Result;
    use const_format::concatcp;

    const BASIC_ACTUAL: &str = concatcp!(BASE_DOC_SUFFIX, "/", TEST_COLL, "/", TEST_KEY);
    const WAIT_FOR_SYNC_ACTUAL: &str = concatcp!(BASIC_ACTUAL, "?", WAIT_FOR_SYNC_QP);
    const SILENT_ACTUAL: &str = concatcp!(BASIC_ACTUAL, "?", SILENT_QP);
    const RETURN_NEW_ACTUAL: &str = concatcp!(BASIC_ACTUAL, "?", RETURN_NEW_QP);
    const RETURN_OLD_ACTUAL: &str = concatcp!(BASIC_ACTUAL, "?", RETURN_OLD_QP);
    const WAIT_RETURN_OLD_ACTUAL: &str =
        concatcp!(BASIC_ACTUAL, "?", WAIT_FOR_SYNC_QP, "&", RETURN_OLD_QP);
    const WAIT_SILENT_ACTUAL: &str = concatcp!(BASIC_ACTUAL, "?", WAIT_FOR_SYNC_QP, "&", SILENT_QP);
    const WAIT_RETURN_NEW_ACTUAL: &str =
        concatcp!(BASIC_ACTUAL, "?", WAIT_FOR_SYNC_QP, "&", RETURN_NEW_QP);
    const WAIT_RETURNS_ACTUAL: &str = concatcp!(
        BASIC_ACTUAL,
        "?",
        WAIT_FOR_SYNC_QP,
        "&",
        RETURN_NEW_QP,
        "&",
        RETURN_OLD_QP
    );

    fn check_url<T>(config: &Config<T>, actual: &str) {
        assert_eq!(actual, config.build_suffix(BASE_DOC_SUFFIX));
    }

    #[test]
    fn replace_url() -> Result<()> {
        let config = ConfigBuilder::default()
            .collection(TEST_COLL)
            .key(TEST_KEY)
            .document("test")
            .build()?;
        check_url(&config, BASIC_ACTUAL);
        Ok(())
    }

    #[test]
    fn replace_wait_for_sync_url() -> Result<()> {
        let config = ConfigBuilder::default()
            .collection(TEST_COLL)
            .key(TEST_KEY)
            .document("test")
            .wait_for_sync(true)
            .build()?;
        check_url(&config, WAIT_FOR_SYNC_ACTUAL);
        Ok(())
    }

    #[test]
    fn replace_silent_url() -> Result<()> {
        let config = ConfigBuilder::default()
            .collection(TEST_COLL)
            .key(TEST_KEY)
            .document("test")
            .silent(true)
            .build()?;
        check_url(&config, SILENT_ACTUAL);
        Ok(())
    }

    #[test]
    fn replace_silent_forces_no_return_url() -> Result<()> {
        let config = ConfigBuilder::default()
            .collection(TEST_COLL)
            .key(TEST_KEY)
            .document("test")
            .silent(true)
            .return_old(true)
            .return_new(true)
            .build()?;
        check_url(&config, SILENT_ACTUAL);
        Ok(())
    }

    #[test]
    fn replace_return_old_url() -> Result<()> {
        let config = ConfigBuilder::default()
            .collection(TEST_COLL)
            .key(TEST_KEY)
            .document("test)")
            .return_old(true)
            .build()?;
        check_url(&config, RETURN_OLD_ACTUAL);
        Ok(())
    }

    #[test]
    fn replace_return_new_url() -> Result<()> {
        let config = ConfigBuilder::default()
            .collection(TEST_COLL)
            .key(TEST_KEY)
            .document("test)")
            .return_new(true)
            .build()?;
        check_url(&config, RETURN_NEW_ACTUAL);
        Ok(())
    }

    #[test]
    fn replace_wait_silent() -> Result<()> {
        let config = ConfigBuilder::default()
            .collection(TEST_COLL)
            .key(TEST_KEY)
            .document("test")
            .wait_for_sync(true)
            .silent(true)
            .build()?;
        check_url(&config, WAIT_SILENT_ACTUAL);
        Ok(())
    }

    #[test]
    fn replace_wait_return_old() -> Result<()> {
        let config = ConfigBuilder::default()
            .collection(TEST_COLL)
            .key(TEST_KEY)
            .document("test")
            .wait_for_sync(true)
            .return_old(true)
            .build()?;
        check_url(&config, WAIT_RETURN_OLD_ACTUAL);
        Ok(())
    }

    #[test]
    fn replace_wait_return_new() -> Result<()> {
        let config = ConfigBuilder::default()
            .collection(TEST_COLL)
            .key(TEST_KEY)
            .document("test")
            .wait_for_sync(true)
            .return_new(true)
            .build()?;
        check_url(&config, WAIT_RETURN_NEW_ACTUAL);
        Ok(())
    }

    #[test]
    fn replace_wait_returns() -> Result<()> {
        let config = ConfigBuilder::default()
            .collection(TEST_COLL)
            .key(TEST_KEY)
            .document("test")
            .wait_for_sync(true)
            .return_old(true)
            .return_new(true)
            .build()?;
        check_url(&config, WAIT_RETURNS_ACTUAL);
        Ok(())
    }

    #[test]
    fn has_header() -> Result<()> {
        let config = ConfigBuilder::default()
            .collection(TEST_COLL)
            .key(TEST_KEY)
            .document("test")
            .if_match("_rev")
            .build()?;
        let headers_opt = config.add_headers()?;
        assert!(headers_opt.is_some());
        assert_eq!(headers_opt.unwrap().keys_len(), 1);
        Ok(())
    }

    #[test]
    fn has_no_header() -> Result<()> {
        let config = ConfigBuilder::default()
            .collection(TEST_COLL)
            .key(TEST_KEY)
            .document("test")
            .build()?;
        let headers_opt = config.add_headers()?;
        assert!(headers_opt.is_none());
        Ok(())
    }
}