Skip to main content

opendal_service_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 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}