opendal_core/services/seafile/
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::SEAFILE_SCHEME;
24use super::backend::SeafileBuilder;
25
26/// Config for seafile services support.
27#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
28#[serde(default)]
29#[non_exhaustive]
30pub struct SeafileConfig {
31    /// root of this backend.
32    ///
33    /// All operations will happen under this root.
34    pub root: Option<String>,
35    /// endpoint address of this backend.
36    pub endpoint: Option<String>,
37    /// username of this backend.
38    pub username: Option<String>,
39    /// password of this backend.
40    pub password: Option<String>,
41    /// repo_name of this backend.
42    ///
43    /// required.
44    pub repo_name: String,
45}
46
47impl Debug for SeafileConfig {
48    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49        f.debug_struct("SeafileConfig")
50            .field("root", &self.root)
51            .field("endpoint", &self.endpoint)
52            .field("username", &self.username)
53            .field("repo_name", &self.repo_name)
54            .finish_non_exhaustive()
55    }
56}
57
58impl crate::Configurator for SeafileConfig {
59    type Builder = SeafileBuilder;
60
61    fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
62        let mut map = uri.options().clone();
63        if let Some(authority) = uri.authority() {
64            map.insert("endpoint".to_string(), format!("https://{authority}"));
65        }
66        if let Some(raw_path) = uri.root() {
67            let mut segments = raw_path.splitn(2, '/');
68            if let Some(repo_name) = segments.next().filter(|s| !s.is_empty()) {
69                map.insert("repo_name".to_string(), repo_name.to_string());
70            }
71
72            if let Some(rest) = segments.next() {
73                if !rest.is_empty() {
74                    map.insert("root".to_string(), rest.to_string());
75                }
76            }
77        }
78
79        if !map.contains_key("repo_name") {
80            return Err(crate::Error::new(
81                crate::ErrorKind::ConfigInvalid,
82                "repo_name is required via uri path or option",
83            )
84            .with_context("service", SEAFILE_SCHEME));
85        }
86
87        Self::from_iter(map)
88    }
89
90    #[allow(deprecated)]
91    fn into_builder(self) -> Self::Builder {
92        SeafileBuilder {
93            config: self,
94            http_client: None,
95        }
96    }
97}
98
99#[cfg(test)]
100mod tests {
101    use super::*;
102    use crate::Configurator;
103    use crate::types::OperatorUri;
104
105    #[test]
106    fn from_uri_sets_endpoint_repo_and_root() {
107        let uri = OperatorUri::new(
108            "seafile://files.example.com/myrepo/projects/app",
109            Vec::<(String, String)>::new(),
110        )
111        .unwrap();
112
113        let cfg = SeafileConfig::from_uri(&uri).unwrap();
114        assert_eq!(cfg.endpoint.as_deref(), Some("https://files.example.com"));
115        assert_eq!(cfg.repo_name, "myrepo".to_string());
116        assert_eq!(cfg.root.as_deref(), Some("projects/app"));
117    }
118}