mongodb_gridfs/
options.rs

1use bson::Document;
2use mongodb::options::{ReadConcern, ReadPreference, WriteConcern};
3use std::{sync::Arc, time::Duration};
4use typed_builder::TypedBuilder;
5
6// TODO: rethink the name of the trait
7// TODO: move the trait in another file
8pub trait ProgressUpdate {
9    fn update(&self, position: usize);
10}
11
12/// [Spec](https://github.com/mongodb/specifications/blob/master/source/gridfs/gridfs-spec.rst#file-upload)
13#[derive(Clone, Default, TypedBuilder)]
14pub struct GridFSUploadOptions {
15    /**
16     * The number of bytes per chunk of this file. Defaults to the
17     * chunkSizeBytes in the GridFSBucketOptions.
18     */
19    #[builder(default = None)]
20    pub(crate) chunk_size_bytes: Option<u32>,
21
22    /**
23     * User data for the 'metadata' field of the files collection document.
24     * If not provided the driver MUST omit the metadata field from the
25     * files collection document.
26     */
27    #[builder(default = None)]
28    pub(crate) metadata: Option<Document>,
29
30    /**
31     * DEPRECATED: A valid MIME type. If not provided the driver MUST omit the
32     * contentType field from the files collection document.
33     *
34     * Applications wishing to store a contentType should add a contentType field
35     * to the metadata document instead.
36     */
37    #[allow(dead_code)]
38    #[builder(default = None)]
39    content_type: Option<String>,
40
41    /**
42     * DEPRECATED: An array of aliases. If not provided the driver MUST omit the
43     * aliases field from the files collection document.
44     *
45     * Applications wishing to store aliases should add an aliases field to the
46     * metadata document instead.
47     */
48    #[allow(dead_code)]
49    #[builder(default = None)]
50    aliases: Option<Vec<String>>,
51
52    /**
53     * TODO: Documentation for progress_tick
54     */
55    // TODO: find a better name.
56    #[builder(default = None)]
57    pub(crate) progress_tick: Option<Arc<dyn ProgressUpdate + Send + Sync>>, // TODO: test process_tick
58}
59
60/// [Spec](https://github.com/mongodb/specifications/blob/master/source/gridfs/gridfs-spec.rst#configurable-gridfsbucket-class)
61#[derive(Clone, Debug, TypedBuilder)]
62pub struct GridFSBucketOptions {
63    /**
64     * The bucket name. Defaults to 'fs'.
65     */
66    #[builder(default = "fs".into())]
67    pub bucket_name: String,
68
69    /**
70     * The chunk size in bytes. Defaults to 255 KiB.
71     */
72    #[builder(default = 255 * 1024)]
73    pub chunk_size_bytes: u32,
74
75    /**
76     * The write concern. Defaults to the write concern of the database.
77     */
78    #[builder(default)]
79    pub write_concern: Option<WriteConcern>,
80
81    /**
82     * The read concern. Defaults to the read concern of the database.
83     */
84    #[builder(default)]
85    pub read_concern: Option<ReadConcern>,
86
87    /**
88     * The read preference. Defaults to the read preference of the database.
89     */
90    #[builder(default)]
91    pub read_preference: Option<ReadPreference>,
92
93    /**
94     * TRANSITIONAL: This option is provided for backwards compatibility.
95     * It MUST be supported while a driver supports MD5 and MUST be removed
96     * (or made into a no-op) when a driver removes MD5 support entirely.
97     * When true, the GridFS implementation will not compute MD5 checksums
98     * of uploaded files. Defaults to false.
99     */
100    #[builder(default = false)]
101    pub disable_md5: bool,
102}
103
104impl Default for GridFSBucketOptions {
105    fn default() -> Self {
106        GridFSBucketOptions {
107            bucket_name: "fs".into(),
108            chunk_size_bytes: 255 * 1024,
109            write_concern: None,
110            read_concern: None,
111            read_preference: None,
112            disable_md5: false,
113        }
114    }
115}
116
117/// [Spec](https://github.com/mongodb/specifications/blob/master/source/gridfs/gridfs-spec.rst#generic-find-on-files-collection)
118#[derive(Clone, Debug, Default, TypedBuilder)]
119pub struct GridFSFindOptions {
120    /**
121     * Enables writing to temporary files on the server. When set to true, the server
122     * can write temporary data to disk while executing the find operation on the files collection.
123     *
124     * This option is sent only if the caller explicitly provides a value. The default
125     * is to not send a value. For servers < 3.2, this option is ignored and not sent
126     * as allowDiskUse does not exist in the OP_QUERY wire protocol.
127     *
128     * @see <https://docs.mongodb.com/manual/reference/command/find/>
129     */
130    #[builder(default)]
131    pub allow_disk_use: Option<bool>,
132
133    /**
134     * The number of documents to return per batch.
135     */
136    #[builder(default)]
137    pub batch_size: Option<u32>,
138
139    /**
140     * The maximum number of documents to return.
141     */
142    #[builder(default)]
143    pub limit: Option<i64>,
144
145    /**
146     * The maximum amount of time to allow the query to run.
147     */
148    #[builder(default)]
149    pub max_time: Option<Duration>,
150
151    /**
152     * The server normally times out idle cursors after an inactivity period (10 minutes)
153     * to prevent excess memory use. Set this option to prevent that.
154     */
155    #[builder(default)]
156    pub no_cursor_timeout: Option<bool>,
157
158    /**
159     * The number of documents to skip before returning.
160     */
161    #[builder(default)]
162    pub skip: u64,
163
164    /**
165     * The order by which to sort results. Defaults to not sorting.
166     */
167    #[builder(default)]
168    pub sort: Option<Document>,
169}
170
171#[cfg(test)]
172mod tests {
173    use super::{GridFSBucketOptions, GridFSFindOptions};
174
175    #[test]
176    fn grid_fs_bucket_options_default() {
177        let options = GridFSBucketOptions::default();
178        assert_eq!(options.bucket_name, "fs");
179        assert_eq!(options.chunk_size_bytes, 255 * 1024);
180        assert_eq!(options.disable_md5, false);
181    }
182    #[test]
183    fn grid_fs_bucket_options_builder_default() {
184        let options = GridFSBucketOptions::builder().build();
185        assert_eq!(options.bucket_name, "fs");
186        assert_eq!(options.chunk_size_bytes, 255 * 1024);
187        assert_eq!(options.disable_md5, false);
188    }
189    #[test]
190    fn grid_fs_bucket_options_bucket_name() {
191        let options = GridFSBucketOptions::builder()
192            .bucket_name("newfs".into())
193            .build();
194        assert_eq!(options.bucket_name, "newfs");
195    }
196    #[test]
197    fn grid_fs_bucket_options_chunk_size_bytes() {
198        let options = GridFSBucketOptions::builder()
199            .chunk_size_bytes(1023)
200            .build();
201        assert_eq!(options.chunk_size_bytes, 1023);
202    }
203    #[test]
204    fn grid_fs_bucket_options_builder_chain() {
205        let options = GridFSBucketOptions::builder()
206            .bucket_name("newfs".into())
207            .chunk_size_bytes(1023)
208            .build();
209        assert_eq!(options.bucket_name, "newfs");
210        assert_eq!(options.chunk_size_bytes, 1023);
211    }
212
213    #[test]
214    fn grid_fs_find_options_builder_chain() {
215        let options = GridFSFindOptions::builder().skip(4).build();
216        assert_eq!(options.skip, 4);
217    }
218    #[test]
219    fn grid_fs_find_options_builder_default() {
220        let options = GridFSFindOptions::builder().build();
221        assert_eq!(options.allow_disk_use, None);
222        assert_eq!(options.batch_size, None);
223        assert_eq!(options.limit, None);
224        assert_eq!(options.max_time, None);
225        assert_eq!(options.no_cursor_timeout, None);
226        assert_eq!(options.skip, 0);
227        assert_eq!(options.sort, None);
228    }
229    #[test]
230    fn grid_fs_find_options_default() {
231        let options = GridFSFindOptions::default();
232        assert_eq!(options.allow_disk_use, None);
233        assert_eq!(options.batch_size, None);
234        assert_eq!(options.limit, None);
235        assert_eq!(options.max_time, None);
236        assert_eq!(options.no_cursor_timeout, None);
237        assert_eq!(options.skip, 0);
238        assert_eq!(options.sort, None);
239    }
240}