opendal_core/services/webdav/
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::WebdavBuilder;
24
25/// Config for [WebDAV](https://datatracker.ietf.org/doc/html/rfc4918) backend support.
26#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct WebdavConfig {
30    /// endpoint of this backend
31    pub endpoint: Option<String>,
32    /// username of this backend
33    pub username: Option<String>,
34    /// password of this backend
35    pub password: Option<String>,
36    /// token of this backend
37    pub token: Option<String>,
38    /// root of this backend
39    pub root: Option<String>,
40    /// WebDAV Service doesn't support copy.
41    pub disable_copy: bool,
42}
43
44impl Debug for WebdavConfig {
45    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46        f.debug_struct("WebdavConfig")
47            .field("endpoint", &self.endpoint)
48            .field("username", &self.username)
49            .field("root", &self.root)
50            .field("disable_copy", &self.disable_copy)
51            .finish_non_exhaustive()
52    }
53}
54
55impl crate::Configurator for WebdavConfig {
56    type Builder = WebdavBuilder;
57
58    fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
59        let mut map = uri.options().clone();
60        if let Some(authority) = uri.authority() {
61            map.insert("endpoint".to_string(), format!("https://{authority}"));
62        }
63
64        if let Some(root) = uri.root() {
65            map.insert("root".to_string(), root.to_string());
66        }
67
68        Self::from_iter(map)
69    }
70
71    fn into_builder(self) -> Self::Builder {
72        WebdavBuilder { config: self }
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79    use crate::Configurator;
80    use crate::types::OperatorUri;
81
82    #[test]
83    fn from_uri_sets_endpoint_and_root() {
84        let uri = OperatorUri::new(
85            "webdav://webdav.example.com/remote.php/webdav",
86            Vec::<(String, String)>::new(),
87        )
88        .unwrap();
89
90        let cfg = WebdavConfig::from_uri(&uri).unwrap();
91        assert_eq!(cfg.endpoint.as_deref(), Some("https://webdav.example.com"));
92        assert_eq!(cfg.root.as_deref(), Some("remote.php/webdav"));
93    }
94
95    #[test]
96    fn from_uri_ignores_endpoint_override() {
97        let uri = OperatorUri::new(
98            "webdav://dav.internal/data",
99            vec![(
100                "endpoint".to_string(),
101                "http://dav.internal:8080".to_string(),
102            )],
103        )
104        .unwrap();
105
106        let cfg = WebdavConfig::from_uri(&uri).unwrap();
107        assert_eq!(cfg.endpoint.as_deref(), Some("https://dav.internal"));
108    }
109
110    #[test]
111    fn from_uri_propagates_disable_copy() {
112        let uri = OperatorUri::new(
113            "webdav://dav.example.com",
114            vec![("disable_copy".to_string(), "true".to_string())],
115        )
116        .unwrap();
117
118        let cfg = WebdavConfig::from_uri(&uri).unwrap();
119        assert!(cfg.disable_copy);
120    }
121}