opendal/services/gridfs/
config.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::fmt::Debug;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23use super::backend::GridfsBuilder;
24
25/// Config for Grid file system support.
26#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct GridfsConfig {
30    /// The connection string of the MongoDB service.
31    pub connection_string: Option<String>,
32    /// The database name of the MongoDB GridFs service to read/write.
33    pub database: Option<String>,
34    /// The bucket name of the MongoDB GridFs service to read/write.
35    pub bucket: Option<String>,
36    /// The chunk size of the MongoDB GridFs service used to break the user file into chunks.
37    pub chunk_size: Option<u32>,
38    /// The working directory, all operations will be performed under it.
39    pub root: Option<String>,
40}
41
42impl Debug for GridfsConfig {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        f.debug_struct("GridFsConfig")
45            .field("database", &self.database)
46            .field("bucket", &self.bucket)
47            .field("chunk_size", &self.chunk_size)
48            .field("root", &self.root)
49            .finish_non_exhaustive()
50    }
51}
52
53impl crate::Configurator for GridfsConfig {
54    type Builder = GridfsBuilder;
55
56    fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
57        let mut map = uri.options().clone();
58
59        if let Some(authority) = uri.authority() {
60            map.entry("connection_string".to_string())
61                .or_insert_with(|| format!("mongodb://{authority}"));
62        }
63
64        if let Some(path) = uri.root() {
65            if !path.is_empty() {
66                let mut segments = path.splitn(3, '/');
67                if let Some(database) = segments.next() {
68                    if !database.is_empty() {
69                        map.entry("database".to_string())
70                            .or_insert_with(|| database.to_string());
71                    }
72                }
73                if let Some(bucket) = segments.next() {
74                    if !bucket.is_empty() {
75                        map.entry("bucket".to_string())
76                            .or_insert_with(|| bucket.to_string());
77                    }
78                }
79                if let Some(rest) = segments.next() {
80                    if !rest.is_empty() {
81                        map.insert("root".to_string(), rest.to_string());
82                    }
83                }
84            }
85        }
86
87        Self::from_iter(map)
88    }
89
90    fn into_builder(self) -> Self::Builder {
91        GridfsBuilder { config: self }
92    }
93}
94
95#[cfg(test)]
96mod tests {
97    use super::*;
98    use crate::Configurator;
99    use crate::types::OperatorUri;
100
101    #[test]
102    fn from_uri_sets_connection_database_bucket_and_root() {
103        let uri = OperatorUri::new(
104            "gridfs://mongo.example.com:27017/app_files/assets/images",
105            Vec::<(String, String)>::new(),
106        )
107        .unwrap();
108
109        let cfg = GridfsConfig::from_uri(&uri).unwrap();
110        assert_eq!(
111            cfg.connection_string.as_deref(),
112            Some("mongodb://mongo.example.com:27017")
113        );
114        assert_eq!(cfg.database.as_deref(), Some("app_files"));
115        assert_eq!(cfg.bucket.as_deref(), Some("assets"));
116        assert_eq!(cfg.root.as_deref(), Some("images"));
117    }
118}