Skip to main content

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