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
use std::time::Duration;

use serde::Serialize;
use typed_builder::TypedBuilder;

use crate::{
    bson::{doc, Bson, Document},
    coll::options::{
        FindOneAndDeleteOptions,
        FindOneAndReplaceOptions,
        FindOneAndUpdateOptions,
        Hint,
        ReturnDocument,
    },
    collation::Collation,
    concern::WriteConcern,
    operation::UpdateOrReplace,
    serde_util,
};

#[derive(Clone, Debug)]
pub(super) enum Modification<'a, T> {
    Delete,
    Update(UpdateOrReplace<'a, T>),
}

#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, TypedBuilder, Serialize)]
#[builder(field_defaults(setter(into)))]
#[serde(rename_all = "camelCase")]
pub(super) struct FindAndModifyOptions {
    #[builder(default)]
    pub(crate) sort: Option<Document>,

    #[builder(default)]
    pub(crate) new: Option<bool>,

    #[builder(default)]
    pub(crate) upsert: Option<bool>,

    #[builder(default)]
    pub(crate) bypass_document_validation: Option<bool>,

    #[builder(default)]
    pub(crate) write_concern: Option<WriteConcern>,

    #[builder(default)]
    pub(crate) array_filters: Option<Vec<Document>>,

    #[serde(
        serialize_with = "serde_util::serialize_duration_option_as_int_millis",
        rename = "maxTimeMS"
    )]
    #[builder(default)]
    pub(crate) max_time: Option<Duration>,

    #[serde(rename = "fields")]
    #[builder(default)]
    pub(crate) projection: Option<Document>,

    #[builder(default)]
    pub(crate) collation: Option<Collation>,

    #[builder(default)]
    pub(crate) hint: Option<Hint>,

    #[builder(default)]
    #[serde(rename = "let")]
    pub(crate) let_vars: Option<Document>,

    #[builder(default)]
    pub(crate) comment: Option<Bson>,
}

impl From<FindOneAndDeleteOptions> for FindAndModifyOptions {
    fn from(options: FindOneAndDeleteOptions) -> Self {
        Self {
            sort: options.sort,
            new: None,
            upsert: None,
            bypass_document_validation: None,
            write_concern: options.write_concern,
            array_filters: None,
            max_time: options.max_time,
            projection: options.projection,
            collation: options.collation,
            hint: options.hint,
            let_vars: options.let_vars,
            comment: options.comment,
        }
    }
}

impl From<FindOneAndUpdateOptions> for FindAndModifyOptions {
    fn from(options: FindOneAndUpdateOptions) -> Self {
        Self {
            sort: options.sort,
            new: return_document_to_bool(options.return_document),
            upsert: options.upsert,
            bypass_document_validation: options.bypass_document_validation,
            write_concern: options.write_concern,
            array_filters: options.array_filters,
            max_time: options.max_time,
            projection: options.projection,
            collation: options.collation,
            hint: options.hint,
            let_vars: options.let_vars,
            comment: options.comment,
        }
    }
}

impl From<FindOneAndReplaceOptions> for FindAndModifyOptions {
    fn from(options: FindOneAndReplaceOptions) -> Self {
        Self {
            sort: options.sort,
            new: return_document_to_bool(options.return_document),
            upsert: options.upsert,
            bypass_document_validation: options.bypass_document_validation,
            write_concern: options.write_concern,
            array_filters: None,
            max_time: options.max_time,
            projection: options.projection,
            collation: options.collation,
            hint: options.hint,
            let_vars: options.let_vars,
            comment: options.comment,
        }
    }
}

fn return_document_to_bool(return_document: Option<ReturnDocument>) -> Option<bool> {
    if let Some(return_document) = return_document {
        return match return_document {
            ReturnDocument::After => Some(true),
            ReturnDocument::Before => Some(false),
        };
    }
    None
}