opendal/services/gridfs/
config.rs1use std::fmt::Debug;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23use super::backend::GridfsBuilder;
24
25#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct GridfsConfig {
30 pub connection_string: Option<String>,
32 pub database: Option<String>,
34 pub bucket: Option<String>,
36 pub chunk_size: Option<u32>,
38 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}