opendal/services/b2/
config.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::fmt::Debug;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23use super::backend::B2Builder;
24
25/// Config for backblaze b2 services support.
26#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct B2Config {
30    /// root of this backend.
31    ///
32    /// All operations will happen under this root.
33    pub root: Option<String>,
34    /// keyID of this backend.
35    ///
36    /// - If application_key_id is set, we will take user's input first.
37    /// - If not, we will try to load it from environment.
38    pub application_key_id: Option<String>,
39    /// applicationKey of this backend.
40    ///
41    /// - If application_key is set, we will take user's input first.
42    /// - If not, we will try to load it from environment.
43    pub application_key: Option<String>,
44    /// bucket of this backend.
45    ///
46    /// required.
47    pub bucket: String,
48    /// bucket id of this backend.
49    ///
50    /// required.
51    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}