opendal/services/gridfs/
config.rs1use std::fmt::Debug;
19use std::fmt::Formatter;
20
21use serde::Deserialize;
22use serde::Serialize;
23
24use super::backend::GridfsBuilder;
25
26#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
28#[serde(default)]
29#[non_exhaustive]
30pub struct GridfsConfig {
31 pub connection_string: Option<String>,
33 pub database: Option<String>,
35 pub bucket: Option<String>,
37 pub chunk_size: Option<u32>,
39 pub root: Option<String>,
41}
42
43impl Debug for GridfsConfig {
44 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
45 f.debug_struct("GridFsConfig")
46 .field("database", &self.database)
47 .field("bucket", &self.bucket)
48 .field("chunk_size", &self.chunk_size)
49 .field("root", &self.root)
50 .finish()
51 }
52}
53
54impl crate::Configurator for GridfsConfig {
55 type Builder = GridfsBuilder;
56
57 fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
58 let mut map = uri.options().clone();
59
60 if let Some(authority) = uri.authority() {
61 map.entry("connection_string".to_string())
62 .or_insert_with(|| format!("mongodb://{authority}"));
63 }
64
65 if let Some(path) = uri.root() {
66 if !path.is_empty() {
67 let mut segments = path.splitn(3, '/');
68 if let Some(database) = segments.next() {
69 if !database.is_empty() {
70 map.entry("database".to_string())
71 .or_insert_with(|| database.to_string());
72 }
73 }
74 if let Some(bucket) = segments.next() {
75 if !bucket.is_empty() {
76 map.entry("bucket".to_string())
77 .or_insert_with(|| bucket.to_string());
78 }
79 }
80 if let Some(rest) = segments.next() {
81 if !rest.is_empty() {
82 map.insert("root".to_string(), rest.to_string());
83 }
84 }
85 }
86 }
87
88 Self::from_iter(map)
89 }
90
91 fn into_builder(self) -> Self::Builder {
92 GridfsBuilder { config: self }
93 }
94}
95
96#[cfg(test)]
97mod tests {
98 use super::*;
99 use crate::Configurator;
100 use crate::types::OperatorUri;
101
102 #[test]
103 fn from_uri_sets_connection_database_bucket_and_root() {
104 let uri = OperatorUri::new(
105 "gridfs://mongo.example.com:27017/app_files/assets/images",
106 Vec::<(String, String)>::new(),
107 )
108 .unwrap();
109
110 let cfg = GridfsConfig::from_uri(&uri).unwrap();
111 assert_eq!(
112 cfg.connection_string.as_deref(),
113 Some("mongodb://mongo.example.com:27017")
114 );
115 assert_eq!(cfg.database.as_deref(), Some("app_files"));
116 assert_eq!(cfg.bucket.as_deref(), Some("assets"));
117 assert_eq!(cfg.root.as_deref(), Some("images"));
118 }
119}