opendal_service_aliyun_drive/
config.rs1use std::fmt::Debug;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23use super::backend::AliyunDriveBuilder;
24
25#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct AliyunDriveConfig {
30 pub root: Option<String>,
36 pub access_token: Option<String>,
42 pub client_id: Option<String>,
46 pub client_secret: Option<String>,
50 pub refresh_token: Option<String>,
54 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 opendal_core::Configurator for AliyunDriveConfig {
74 type Builder = AliyunDriveBuilder;
75
76 fn from_uri(uri: &opendal_core::OperatorUri) -> opendal_core::Result<Self> {
77 let mut map = uri.options().clone();
78
79 if let Some(drive_type) = uri.name()
80 && !drive_type.is_empty()
81 {
82 map.insert("drive_type".to_string(), drive_type.to_string());
83 }
84
85 if let Some(root) = uri.root()
86 && !root.is_empty()
87 {
88 map.insert("root".to_string(), root.to_string());
89 }
90
91 Self::from_iter(map)
92 }
93
94 #[allow(deprecated)]
95 fn into_builder(self) -> Self::Builder {
96 AliyunDriveBuilder { config: self }
97 }
98}
99
100#[cfg(test)]
101mod tests {
102 use super::*;
103 use opendal_core::Configurator;
104 use opendal_core::OperatorUri;
105
106 #[test]
107 fn from_uri_sets_drive_type_and_root() {
108 let uri = OperatorUri::new(
109 "aliyun-drive://resource/library/photos",
110 Vec::<(String, String)>::new(),
111 )
112 .unwrap();
113
114 let cfg = AliyunDriveConfig::from_uri(&uri).unwrap();
115 assert_eq!(cfg.drive_type, "resource".to_string());
116 assert_eq!(cfg.root.as_deref(), Some("library/photos"));
117 }
118
119 #[test]
120 fn from_uri_allows_missing_drive_type() {
121 let uri =
122 OperatorUri::new("aliyun-drive:///documents", Vec::<(String, String)>::new()).unwrap();
123
124 let cfg = AliyunDriveConfig::from_uri(&uri).unwrap();
125 assert_eq!(cfg.drive_type, String::default());
126 assert_eq!(cfg.root.as_deref(), Some("documents"));
127 }
128}