opendal/services/aliyun_drive/
config.rs1use std::fmt::Debug;
19use std::fmt::Formatter;
20
21use super::backend::AliyunDriveBuilder;
22use serde::Deserialize;
23use serde::Serialize;
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 Formatter<'_>) -> std::fmt::Result {
66 let mut d = f.debug_struct("AliyunDriveConfig");
67
68 d.field("root", &self.root)
69 .field("drive_type", &self.drive_type);
70
71 d.finish_non_exhaustive()
72 }
73}
74
75impl crate::Configurator for AliyunDriveConfig {
76 type Builder = AliyunDriveBuilder;
77
78 fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
79 let mut map = uri.options().clone();
80
81 if let Some(drive_type) = uri.name() {
82 if !drive_type.is_empty() {
83 map.insert("drive_type".to_string(), drive_type.to_string());
84 }
85 }
86
87 if let Some(root) = uri.root() {
88 if !root.is_empty() {
89 map.insert("root".to_string(), root.to_string());
90 }
91 }
92
93 Self::from_iter(map)
94 }
95
96 #[allow(deprecated)]
97 fn into_builder(self) -> Self::Builder {
98 AliyunDriveBuilder {
99 config: self,
100 http_client: None,
101 }
102 }
103}
104
105#[cfg(test)]
106mod tests {
107 use super::*;
108 use crate::Configurator;
109 use crate::types::OperatorUri;
110
111 #[test]
112 fn from_uri_sets_drive_type_and_root() {
113 let uri = OperatorUri::new(
114 "aliyun-drive://resource/library/photos",
115 Vec::<(String, String)>::new(),
116 )
117 .unwrap();
118
119 let cfg = AliyunDriveConfig::from_uri(&uri).unwrap();
120 assert_eq!(cfg.drive_type, "resource".to_string());
121 assert_eq!(cfg.root.as_deref(), Some("library/photos"));
122 }
123
124 #[test]
125 fn from_uri_allows_missing_drive_type() {
126 let uri =
127 OperatorUri::new("aliyun-drive:///documents", Vec::<(String, String)>::new()).unwrap();
128
129 let cfg = AliyunDriveConfig::from_uri(&uri).unwrap();
130 assert_eq!(cfg.drive_type, String::default());
131 assert_eq!(cfg.root.as_deref(), Some("documents"));
132 }
133}