opendal/services/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 crate::Configurator for B2Config {
66 type Builder = B2Builder;
67
68 fn from_uri(uri: &crate::types::OperatorUri) -> crate::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 #[allow(deprecated)]
83 fn into_builder(self) -> Self::Builder {
84 B2Builder {
85 config: self,
86 http_client: None,
87 }
88 }
89}
90
91#[cfg(test)]
92mod tests {
93 use super::*;
94 use crate::Configurator;
95 use crate::types::OperatorUri;
96
97 #[test]
98 fn from_uri_extracts_bucket_and_root() {
99 let uri = OperatorUri::new(
100 "b2://example-bucket/path/to/root",
101 vec![("bucket_id".to_string(), "bucket-id".to_string())],
102 )
103 .unwrap();
104
105 let cfg = B2Config::from_uri(&uri).unwrap();
106 assert_eq!(cfg.bucket, "example-bucket");
107 assert_eq!(cfg.root.as_deref(), Some("path/to/root"));
108 assert_eq!(cfg.bucket_id, "bucket-id");
109 }
110}