opendal/services/b2/
config.rs1use std::fmt::Debug;
19use std::fmt::Formatter;
20
21use super::backend::B2Builder;
22use serde::Deserialize;
23use serde::Serialize;
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 Formatter<'_>) -> std::fmt::Result {
56        let mut d = f.debug_struct("B2Config");
57
58        d.field("root", &self.root)
59            .field("application_key_id", &self.application_key_id)
60            .field("bucket_id", &self.bucket_id)
61            .field("bucket", &self.bucket);
62
63        d.finish_non_exhaustive()
64    }
65}
66
67impl crate::Configurator for B2Config {
68    type Builder = B2Builder;
69
70    fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
71        let mut map = uri.options().clone();
72
73        if let Some(name) = uri.name() {
74            map.insert("bucket".to_string(), name.to_string());
75        }
76
77        if let Some(root) = uri.root() {
78            map.insert("root".to_string(), root.to_string());
79        }
80
81        Self::from_iter(map)
82    }
83
84    #[allow(deprecated)]
85    fn into_builder(self) -> Self::Builder {
86        B2Builder {
87            config: self,
88            http_client: None,
89        }
90    }
91}
92
93#[cfg(test)]
94mod tests {
95    use super::*;
96    use crate::Configurator;
97    use crate::types::OperatorUri;
98
99    #[test]
100    fn from_uri_extracts_bucket_and_root() {
101        let uri = OperatorUri::new(
102            "b2://example-bucket/path/to/root",
103            vec![("bucket_id".to_string(), "bucket-id".to_string())],
104        )
105        .unwrap();
106
107        let cfg = B2Config::from_uri(&uri).unwrap();
108        assert_eq!(cfg.bucket, "example-bucket");
109        assert_eq!(cfg.root.as_deref(), Some("path/to/root"));
110        assert_eq!(cfg.bucket_id, "bucket-id");
111    }
112}