opendal/services/cos/
config.rs1use std::fmt::Debug;
19use std::fmt::Formatter;
20
21use super::backend::CosBuilder;
22use serde::Deserialize;
23use serde::Serialize;
24
25#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28pub struct CosConfig {
29 pub root: Option<String>,
31 pub endpoint: Option<String>,
33 pub secret_id: Option<String>,
35 pub secret_key: Option<String>,
37 pub bucket: Option<String>,
39 pub enable_versioning: bool,
41 pub disable_config_load: bool,
43}
44
45impl Debug for CosConfig {
46 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
47 f.debug_struct("CosConfig")
48 .field("root", &self.root)
49 .field("endpoint", &self.endpoint)
50 .field("secret_id", &"<redacted>")
51 .field("secret_key", &"<redacted>")
52 .field("bucket", &self.bucket)
53 .finish_non_exhaustive()
54 }
55}
56
57impl crate::Configurator for CosConfig {
58 type Builder = CosBuilder;
59
60 fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
61 let mut map = uri.options().clone();
62
63 if let Some(name) = uri.name() {
64 map.insert("bucket".to_string(), name.to_string());
65 }
66
67 if let Some(root) = uri.root() {
68 map.insert("root".to_string(), root.to_string());
69 }
70
71 Self::from_iter(map)
72 }
73
74 #[allow(deprecated)]
75 fn into_builder(self) -> Self::Builder {
76 CosBuilder {
77 config: self,
78
79 http_client: None,
80 }
81 }
82}
83
84#[cfg(test)]
85mod tests {
86 use super::*;
87 use crate::Configurator;
88 use crate::types::OperatorUri;
89
90 #[test]
91 fn from_uri_extracts_bucket_and_root() {
92 let uri = OperatorUri::new(
93 "cos://example-bucket/path/to/root",
94 Vec::<(String, String)>::new(),
95 )
96 .unwrap();
97 let cfg = CosConfig::from_uri(&uri).unwrap();
98 assert_eq!(cfg.bucket.as_deref(), Some("example-bucket"));
99 assert_eq!(cfg.root.as_deref(), Some("path/to/root"));
100 }
101}