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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use crate::{CollectionName, Guid, ServerTimestamp};
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct CollectionRequest {
    pub collection: CollectionName,
    pub full: bool,
    pub ids: Option<Vec<Guid>>,

    pub limit: Option<RequestLimit>,
    pub older: Option<ServerTimestamp>,
    pub newer: Option<ServerTimestamp>,
}

impl CollectionRequest {
    #[inline]
    pub fn new(collection: CollectionName) -> CollectionRequest {
        CollectionRequest {
            collection,
            ..Default::default()
        }
    }

    #[inline]
    pub fn ids<V>(mut self, v: V) -> CollectionRequest
    where
        V: IntoIterator,
        V::Item: Into<Guid>,
    {
        self.ids = Some(v.into_iter().map(|id| id.into()).collect());
        self
    }

    #[inline]
    pub fn full(mut self) -> CollectionRequest {
        self.full = true;
        self
    }

    #[inline]
    pub fn older_than(mut self, ts: ServerTimestamp) -> CollectionRequest {
        self.older = Some(ts);
        self
    }

    #[inline]
    pub fn newer_than(mut self, ts: ServerTimestamp) -> CollectionRequest {
        self.newer = Some(ts);
        self
    }

    #[inline]
    pub fn limit(mut self, num: usize, order: RequestOrder) -> CollectionRequest {
        self.limit = Some(RequestLimit { num, order });
        self
    }
}

// This is just used interally - consumers just provide the content, not request params.
#[cfg(feature = "sync-client")]
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub(crate) struct CollectionPost {
    pub collection: CollectionName,
    pub commit: bool,
    pub batch: Option<String>,
}

#[cfg(feature = "sync-client")]
impl CollectionPost {
    #[inline]
    pub fn new(collection: CollectionName) -> Self {
        Self {
            collection,
            ..Default::default()
        }
    }

    #[inline]
    pub fn batch(mut self, batch: Option<String>) -> Self {
        self.batch = batch;
        self
    }

    #[inline]
    pub fn commit(mut self, v: bool) -> Self {
        self.commit = v;
        self
    }
}

// Asking for the order of records only makes sense if you are limiting them
// in some way - consumers don't care about the order otherwise as everything
// is processed as a set.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum RequestOrder {
    Oldest,
    Newest,
    Index,
}

impl RequestOrder {
    #[inline]
    pub fn as_str(self) -> &'static str {
        match self {
            RequestOrder::Oldest => "oldest",
            RequestOrder::Newest => "newest",
            RequestOrder::Index => "index",
        }
    }
}

impl std::fmt::Display for RequestOrder {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

// If you specify a numerical limit you must provide the order so backfilling
// is possible (ie, so you know which ones you got!)
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct RequestLimit {
    pub(crate) num: usize,
    pub(crate) order: RequestOrder,
}