Skip to main content

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