mongodb/gridfs/
options.rs

1use std::time::Duration;
2
3use macro_magic::export_tokens;
4use serde::Deserialize;
5use typed_builder::TypedBuilder;
6
7use crate::{
8    bson::Document,
9    options::{FindOneOptions, FindOptions, ReadConcern, SelectionCriteria, WriteConcern},
10};
11
12/// Contains the options for creating a [`GridFsBucket`](crate::gridfs::GridFsBucket).
13#[derive(Clone, Debug, Default, Deserialize, TypedBuilder)]
14#[builder(field_defaults(default, setter(into)))]
15#[non_exhaustive]
16pub struct GridFsBucketOptions {
17    /// The bucket name. Defaults to 'fs'.
18    pub bucket_name: Option<String>,
19
20    /// The chunk size in bytes used to break the user file into chunks. Defaults to 255 KiB.
21    pub chunk_size_bytes: Option<u32>,
22
23    /// The write concern. Defaults to the write concern of the database.
24    pub write_concern: Option<WriteConcern>,
25
26    /// The read concern. Defaults to the read concern of the database.
27    pub read_concern: Option<ReadConcern>,
28
29    /// The selection criteria. Defaults to the selection criteria of the database.
30    pub selection_criteria: Option<SelectionCriteria>,
31}
32
33/// Contains the options for uploading a file to a [`GridFsBucket`](crate::gridfs::GridFsBucket).
34#[derive(Clone, Debug, Default, Deserialize, TypedBuilder)]
35#[serde(rename_all = "camelCase")]
36#[builder(field_defaults(default, setter(into)))]
37#[non_exhaustive]
38#[export_tokens]
39pub struct GridFsUploadOptions {
40    /// The number of bytes per chunk of this file. Defaults to the `chunk_size_bytes` specified
41    /// in the [`GridFsBucketOptions`](crate::options::GridFsBucketOptions).
42    pub chunk_size_bytes: Option<u32>,
43
44    /// User data for the 'metadata' field of the files collection document.
45    pub metadata: Option<Document>,
46}
47
48/// Contains the options for downloading a file from a [`GridFsBucket`](crate::gridfs::GridFsBucket)
49/// by name.
50#[derive(Clone, Debug, Default, Deserialize, TypedBuilder)]
51#[builder(field_defaults(default, setter(into)))]
52#[non_exhaustive]
53#[export_tokens]
54pub struct GridFsDownloadByNameOptions {
55    /// Which revision (documents with the same filename and different `upload_date`s)
56    /// of the file to retrieve. Defaults to -1 (the most recent revision).
57    ///
58    /// Revision numbers are defined as follows:
59    /// 0 = the original stored file
60    /// 1 = the first revision
61    /// 2 = the second revision
62    /// etc...
63    /// -2 = the second most recent revision
64    /// -1 = the most recent revision
65    pub revision: Option<i32>,
66}
67
68/// Contains the options for finding
69/// [`FilesCollectionDocument`](crate::gridfs::FilesCollectionDocument)s in a
70/// [`GridFsBucket`](crate::gridfs::GridFsBucket).
71#[derive(Clone, Debug, Default, Deserialize, TypedBuilder)]
72#[builder(field_defaults(default, setter(into)))]
73#[non_exhaustive]
74#[export_tokens]
75pub struct GridFsFindOptions {
76    /// Enables writing to temporary files on the server. When set to true, the
77    /// server can write temporary data to disk while executing the find operation
78    /// on the files collection.
79    pub allow_disk_use: Option<bool>,
80
81    /// The number of documents to return per batch.
82    pub batch_size: Option<u32>,
83
84    /// The maximum number of documents to return.
85    pub limit: Option<i64>,
86
87    /// The maximum amount of time to allow the query to run.
88    pub max_time: Option<Duration>,
89
90    /// The number of documents to skip before returning.
91    pub skip: Option<u64>,
92
93    /// The order by which to sort results. Defaults to not sorting.
94    pub sort: Option<Document>,
95}
96
97impl From<GridFsFindOptions> for FindOptions {
98    fn from(options: GridFsFindOptions) -> Self {
99        Self {
100            allow_disk_use: options.allow_disk_use,
101            batch_size: options.batch_size,
102            limit: options.limit,
103            max_time: options.max_time,
104            skip: options.skip,
105            sort: options.sort,
106            ..Default::default()
107        }
108    }
109}
110
111/// Contains the options for finding a single
112/// [`FilesCollectionDocument`](crate::gridfs::FilesCollectionDocument) in a
113/// [`GridFsBucket`](crate::gridfs::GridFsBucket).
114#[derive(Clone, Debug, Default, Deserialize, TypedBuilder)]
115#[builder(field_defaults(default, setter(into)))]
116#[non_exhaustive]
117#[export_tokens]
118pub struct GridFsFindOneOptions {
119    /// The maximum amount of time to allow the query to run.
120    pub max_time: Option<Duration>,
121
122    /// The number of documents to skip before returning.
123    pub skip: Option<u64>,
124
125    /// The order by which to sort results. Defaults to not sorting.
126    pub sort: Option<Document>,
127}
128
129impl From<GridFsFindOneOptions> for FindOneOptions {
130    fn from(options: GridFsFindOneOptions) -> Self {
131        Self {
132            max_time: options.max_time,
133            skip: options.skip,
134            sort: options.sort,
135            ..Default::default()
136        }
137    }
138}