sync15/engine/
request.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4use crate::{CollectionName, Guid, ServerTimestamp};
5#[derive(Debug, Default, Clone, PartialEq, Eq)]
6pub struct CollectionRequest {
7    pub collection: CollectionName,
8    pub full: bool,
9    pub ids: Option<Vec<Guid>>,
10
11    pub limit: Option<RequestLimit>,
12    pub older: Option<ServerTimestamp>,
13    pub newer: Option<ServerTimestamp>,
14}
15
16impl CollectionRequest {
17    #[inline]
18    pub fn new(collection: CollectionName) -> CollectionRequest {
19        CollectionRequest {
20            collection,
21            ..Default::default()
22        }
23    }
24
25    #[inline]
26    pub fn ids<V>(mut self, v: V) -> CollectionRequest
27    where
28        V: IntoIterator,
29        V::Item: Into<Guid>,
30    {
31        self.ids = Some(v.into_iter().map(|id| id.into()).collect());
32        self
33    }
34
35    #[inline]
36    pub fn full(mut self) -> CollectionRequest {
37        self.full = true;
38        self
39    }
40
41    #[inline]
42    pub fn older_than(mut self, ts: ServerTimestamp) -> CollectionRequest {
43        self.older = Some(ts);
44        self
45    }
46
47    #[inline]
48    pub fn newer_than(mut self, ts: ServerTimestamp) -> CollectionRequest {
49        self.newer = Some(ts);
50        self
51    }
52
53    #[inline]
54    pub fn limit(mut self, num: usize, order: RequestOrder) -> CollectionRequest {
55        self.limit = Some(RequestLimit { num, order });
56        self
57    }
58}
59
60// This is just used interally - consumers just provide the content, not request params.
61#[cfg(feature = "sync-client")]
62#[derive(Debug, Default, Clone, PartialEq, Eq)]
63pub(crate) struct CollectionPost {
64    pub collection: CollectionName,
65    pub commit: bool,
66    pub batch: Option<String>,
67}
68
69#[cfg(feature = "sync-client")]
70impl CollectionPost {
71    #[inline]
72    pub fn new(collection: CollectionName) -> Self {
73        Self {
74            collection,
75            ..Default::default()
76        }
77    }
78
79    #[inline]
80    pub fn batch(mut self, batch: Option<String>) -> Self {
81        self.batch = batch;
82        self
83    }
84
85    #[inline]
86    pub fn commit(mut self, v: bool) -> Self {
87        self.commit = v;
88        self
89    }
90}
91
92// Asking for the order of records only makes sense if you are limiting them
93// in some way - consumers don't care about the order otherwise as everything
94// is processed as a set.
95#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
96pub enum RequestOrder {
97    Oldest,
98    Newest,
99    Index,
100}
101
102impl RequestOrder {
103    #[inline]
104    pub fn as_str(self) -> &'static str {
105        match self {
106            RequestOrder::Oldest => "oldest",
107            RequestOrder::Newest => "newest",
108            RequestOrder::Index => "index",
109        }
110    }
111}
112
113impl std::fmt::Display for RequestOrder {
114    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
115        f.write_str(self.as_str())
116    }
117}
118
119// If you specify a numerical limit you must provide the order so backfilling
120// is possible (ie, so you know which ones you got!)
121#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
122pub struct RequestLimit {
123    pub(crate) num: usize,
124    pub(crate) order: RequestOrder,
125}