opendal/services/upyun/
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::UpyunBuilder;
24
25/// Config for upyun services support.
26#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct UpyunConfig {
30    /// root of this backend.
31    ///
32    /// All operations will happen under this root.
33    pub root: Option<String>,
34    /// bucket address of this backend.
35    pub bucket: String,
36    /// username of this backend.
37    pub operator: Option<String>,
38    /// password of this backend.
39    pub password: Option<String>,
40}
41
42impl Debug for UpyunConfig {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        f.debug_struct("UpyunConfig")
45            .field("root", &self.root)
46            .field("bucket", &self.bucket)
47            .field("operator", &self.operator)
48            .finish_non_exhaustive()
49    }
50}
51
52impl crate::Configurator for UpyunConfig {
53    type Builder = UpyunBuilder;
54
55    fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
56        let mut map = uri.options().clone();
57
58        if let Some(name) = uri.name() {
59            map.insert("bucket".to_string(), name.to_string());
60        }
61
62        if let Some(root) = uri.root() {
63            map.insert("root".to_string(), root.to_string());
64        }
65
66        Self::from_iter(map)
67    }
68
69    #[allow(deprecated)]
70    fn into_builder(self) -> Self::Builder {
71        UpyunBuilder {
72            config: self,
73            http_client: None,
74        }
75    }
76}
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81    use crate::Configurator;
82    use crate::types::OperatorUri;
83
84    #[test]
85    fn from_uri_extracts_bucket_and_root() {
86        let uri = OperatorUri::new(
87            "upyun://example-bucket/path/to/root",
88            Vec::<(String, String)>::new(),
89        )
90        .unwrap();
91        let cfg = UpyunConfig::from_uri(&uri).unwrap();
92        assert_eq!(cfg.bucket, "example-bucket");
93        assert_eq!(cfg.root.as_deref(), Some("path/to/root"));
94    }
95}