opendal_service_b2/
config.rs1use std::fmt::Debug;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23use super::backend::B2Builder;
24
25#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct B2Config {
30 pub root: Option<String>,
34 pub application_key_id: Option<String>,
39 pub application_key: Option<String>,
44 pub bucket: String,
48 pub bucket_id: String,
52}
53
54impl Debug for B2Config {
55 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56 f.debug_struct("B2Config")
57 .field("root", &self.root)
58 .field("application_key_id", &self.application_key_id)
59 .field("bucket_id", &self.bucket_id)
60 .field("bucket", &self.bucket)
61 .finish_non_exhaustive()
62 }
63}
64
65impl opendal_core::Configurator for B2Config {
66 type Builder = B2Builder;
67
68 fn from_uri(uri: &opendal_core::OperatorUri) -> opendal_core::Result<Self> {
69 let mut map = uri.options().clone();
70
71 if let Some(name) = uri.name() {
72 map.insert("bucket".to_string(), name.to_string());
73 }
74
75 if let Some(root) = uri.root() {
76 map.insert("root".to_string(), root.to_string());
77 }
78
79 Self::from_iter(map)
80 }
81
82 fn into_builder(self) -> Self::Builder {
83 B2Builder { config: self }
84 }
85}
86
87#[cfg(test)]
88mod tests {
89 use super::*;
90 use opendal_core::Configurator;
91 use opendal_core::OperatorUri;
92
93 #[test]
94 fn from_uri_extracts_bucket_and_root() {
95 let uri = OperatorUri::new(
96 "b2://example-bucket/path/to/root",
97 vec![("bucket_id".to_string(), "bucket-id".to_string())],
98 )
99 .unwrap();
100
101 let cfg = B2Config::from_uri(&uri).unwrap();
102 assert_eq!(cfg.bucket, "example-bucket");
103 assert_eq!(cfg.root.as_deref(), Some("path/to/root"));
104 assert_eq!(cfg.bucket_id, "bucket-id");
105 }
106}