opendal_core/services/pcloud/
config.rs1use std::fmt::Debug;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23use super::backend::PcloudBuilder;
24
25#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct PcloudConfig {
30 pub root: Option<String>,
34 pub endpoint: String,
36 pub username: Option<String>,
38 pub password: Option<String>,
40}
41
42impl Debug for PcloudConfig {
43 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44 f.debug_struct("PcloudConfig")
45 .field("root", &self.root)
46 .field("endpoint", &self.endpoint)
47 .field("username", &self.username)
48 .finish_non_exhaustive()
49 }
50}
51
52impl crate::Configurator for PcloudConfig {
53 type Builder = PcloudBuilder;
54
55 fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
56 let mut map = uri.options().clone();
57 if let Some(authority) = uri.authority() {
58 map.insert("endpoint".to_string(), format!("https://{authority}"));
59 }
60
61 if let Some(root) = uri.root() {
62 if !root.is_empty() {
63 map.insert("root".to_string(), root.to_string());
64 }
65 }
66
67 Self::from_iter(map)
68 }
69
70 #[allow(deprecated)]
71 fn into_builder(self) -> Self::Builder {
72 PcloudBuilder {
73 config: self,
74 http_client: None,
75 }
76 }
77}
78
79#[cfg(test)]
80mod tests {
81 use super::*;
82 use crate::Configurator;
83 use crate::types::OperatorUri;
84
85 #[test]
86 fn from_uri_sets_endpoint_and_root() {
87 let uri = OperatorUri::new(
88 "pcloud://api.pcloud.com/drive/photos",
89 vec![("username".to_string(), "alice".to_string())],
90 )
91 .unwrap();
92
93 let cfg = PcloudConfig::from_uri(&uri).unwrap();
94 assert_eq!(cfg.endpoint, "https://api.pcloud.com".to_string());
95 assert_eq!(cfg.root.as_deref(), Some("drive/photos"));
96 assert_eq!(cfg.username.as_deref(), Some("alice"));
97 }
98
99 #[test]
100 fn from_uri_allows_missing_authority() {
101 let uri = OperatorUri::new("pcloud", Vec::<(String, String)>::new()).unwrap();
102
103 let cfg = PcloudConfig::from_uri(&uri).unwrap();
104 assert!(cfg.endpoint.is_empty());
105 }
106}