remi_gridfs/
config.rs

1// ๐Ÿปโ€โ„๏ธ๐Ÿงถ remi-rs: Asynchronous Rust crate to handle communication between applications and object storage providers
2// Copyright (c) 2022-2025 Noelware, LLC. <team@noelware.org>
3//
4// Permission is hereby granted, free of charge, to any person obtaining a copy
5// of this software and associated documentation files (the "Software"), to deal
6// in the Software without restriction, including without limitation the rights
7// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8// copies of the Software, and to permit persons to whom the Software is
9// furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in all
12// copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20// SOFTWARE.
21
22use mongodb::options::{ClientOptions, GridFsBucketOptions, ReadConcern, SelectionCriteria, WriteConcern};
23
24#[derive(Debug, Clone, Default)]
25#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
26pub struct StorageConfig {
27    /// Specifies the [`SelectionCriteria`].
28    #[cfg_attr(
29        feature = "serde",
30        serde(
31            default,
32            serialize_with = "serialize_selection_criteria",
33            skip_serializing_if = "Option::is_none"
34        )
35    )]
36    pub selection_criteria: Option<SelectionCriteria>,
37
38    /// Specifies the [`WriteConcern`] for all level acknowledgment when writing
39    /// new documents into the GridFS datastore. Read the [`MongoDB` documentation](https://www.mongodb.com/docs/manual/reference/write-concern)
40    /// for more information.
41    #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "Option::is_none"))]
42    pub write_concern: Option<WriteConcern>,
43
44    /// Configure the [`ClientOptions`] that allows to connect to a MongoDB server.
45    #[cfg_attr(feature = "serde", serde(default, skip_serializing))]
46    pub client_options: ClientOptions,
47
48    /// Specifies the [`ReadConcern`] for isolation for when reading documents from the GridFS store. Read the
49    /// [`MongoDB` documentation](https://www.mongodb.com/docs/manual/reference/write-concern) for more information.
50    #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "Option::is_none"))]
51    pub read_concern: Option<ReadConcern>,
52
53    /// Chunk size (in bytes) used to break the user file into chunks.
54    #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "Option::is_none"))]
55    pub chunk_size: Option<u32>,
56
57    /// Database to connect to if [`client_options`][StorageConfig::client_options] was set. It will default
58    /// to the default database.
59    #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "Option::is_none"))]
60    pub database: Option<String>,
61
62    /// Bucket name that holds all the GridFS datastore blobs.
63    pub bucket: String,
64}
65
66impl From<StorageConfig> for GridFsBucketOptions {
67    fn from(value: StorageConfig) -> Self {
68        GridFsBucketOptions::builder()
69            .selection_criteria(value.selection_criteria)
70            .read_concern(value.read_concern)
71            .write_concern(value.write_concern)
72            .chunk_size_bytes(value.chunk_size)
73            .bucket_name(value.bucket)
74            .build()
75    }
76}
77
78#[cfg(feature = "serde")]
79#[allow(unused)]
80fn serialize_selection_criteria<S: ::serde::ser::Serializer>(
81    value: &Option<SelectionCriteria>,
82    serializer: S,
83) -> Result<S::Ok, S::Error> {
84    use ::mongodb::options::ReadPreference;
85    use ::serde::{ser::Error, Serialize};
86
87    if let Some(value) = value {
88        if matches!(value, SelectionCriteria::Predicate(_)) {
89            return Err(S::Error::custom(
90                "cannot use `SelectionCriteria::Predicate` to be serialized",
91            ));
92        }
93
94        match value {
95            SelectionCriteria::ReadPreference(rp) => return ReadPreference::serialize(rp, serializer),
96            _ => unimplemented!(),
97        }
98    }
99
100    serializer.serialize_none()
101}