opendal/services/aliyun_drive/
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::AliyunDriveBuilder;
24
25/// Config for Aliyun Drive services support.
26#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct AliyunDriveConfig {
30    /// The Root of this backend.
31    ///
32    /// All operations will happen under this root.
33    ///
34    /// Default to `/` if not set.
35    pub root: Option<String>,
36    /// The access_token of this backend.
37    ///
38    /// Solution for client-only purpose. #4733
39    ///
40    /// Required if no client_id, client_secret and refresh_token are provided.
41    pub access_token: Option<String>,
42    /// The client_id of this backend.
43    ///
44    /// Required if no access_token is provided.
45    pub client_id: Option<String>,
46    /// The client_secret of this backend.
47    ///
48    /// Required if no access_token is provided.
49    pub client_secret: Option<String>,
50    /// The refresh_token of this backend.
51    ///
52    /// Required if no access_token is provided.
53    pub refresh_token: Option<String>,
54    /// The drive_type of this backend.
55    ///
56    /// All operations will happen under this type of drive.
57    ///
58    /// Available values are `default`, `backup` and `resource`.
59    ///
60    /// Fallback to default if not set or no other drives can be found.
61    pub drive_type: String,
62}
63
64impl Debug for AliyunDriveConfig {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        f.debug_struct("AliyunDriveConfig")
67            .field("root", &self.root)
68            .field("drive_type", &self.drive_type)
69            .finish_non_exhaustive()
70    }
71}
72
73impl crate::Configurator for AliyunDriveConfig {
74    type Builder = AliyunDriveBuilder;
75
76    fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
77        let mut map = uri.options().clone();
78
79        if let Some(drive_type) = uri.name() {
80            if !drive_type.is_empty() {
81                map.insert("drive_type".to_string(), drive_type.to_string());
82            }
83        }
84
85        if let Some(root) = uri.root() {
86            if !root.is_empty() {
87                map.insert("root".to_string(), root.to_string());
88            }
89        }
90
91        Self::from_iter(map)
92    }
93
94    #[allow(deprecated)]
95    fn into_builder(self) -> Self::Builder {
96        AliyunDriveBuilder {
97            config: self,
98            http_client: None,
99        }
100    }
101}
102
103#[cfg(test)]
104mod tests {
105    use super::*;
106    use crate::Configurator;
107    use crate::types::OperatorUri;
108
109    #[test]
110    fn from_uri_sets_drive_type_and_root() {
111        let uri = OperatorUri::new(
112            "aliyun-drive://resource/library/photos",
113            Vec::<(String, String)>::new(),
114        )
115        .unwrap();
116
117        let cfg = AliyunDriveConfig::from_uri(&uri).unwrap();
118        assert_eq!(cfg.drive_type, "resource".to_string());
119        assert_eq!(cfg.root.as_deref(), Some("library/photos"));
120    }
121
122    #[test]
123    fn from_uri_allows_missing_drive_type() {
124        let uri =
125            OperatorUri::new("aliyun-drive:///documents", Vec::<(String, String)>::new()).unwrap();
126
127        let cfg = AliyunDriveConfig::from_uri(&uri).unwrap();
128        assert_eq!(cfg.drive_type, String::default());
129        assert_eq!(cfg.root.as_deref(), Some("documents"));
130    }
131}