opendal_core/services/webhdfs/
config.rs1use std::fmt::Debug;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23use super::backend::WebhdfsBuilder;
24
25#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct WebhdfsConfig {
30 pub root: Option<String>,
32 pub endpoint: Option<String>,
34 pub user_name: Option<String>,
36 pub delegation: Option<String>,
38 pub disable_list_batch: bool,
40 pub atomic_write_dir: Option<String>,
42}
43
44impl Debug for WebhdfsConfig {
45 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46 f.debug_struct("WebhdfsConfig")
47 .field("root", &self.root)
48 .field("endpoint", &self.endpoint)
49 .field("user_name", &self.user_name)
50 .field("disable_list_batch", &self.disable_list_batch)
51 .field("atomic_write_dir", &self.atomic_write_dir)
52 .finish_non_exhaustive()
53 }
54}
55
56impl crate::Configurator for WebhdfsConfig {
57 type Builder = WebhdfsBuilder;
58
59 fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
60 let mut map = uri.options().clone();
61 if let Some(authority) = uri.authority() {
62 map.insert("endpoint".to_string(), format!("http://{authority}"));
63 }
64
65 if let Some(root) = uri.root() {
66 if !root.is_empty() {
67 map.insert("root".to_string(), root.to_string());
68 }
69 }
70
71 Self::from_iter(map)
72 }
73
74 fn into_builder(self) -> Self::Builder {
75 WebhdfsBuilder { config: self }
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 "webhdfs://namenode.example.com:50070/user/hadoop/data",
89 vec![("user_name".to_string(), "hadoop".to_string())],
90 )
91 .unwrap();
92
93 let cfg = WebhdfsConfig::from_uri(&uri).unwrap();
94 assert_eq!(
95 cfg.endpoint.as_deref(),
96 Some("http://namenode.example.com:50070")
97 );
98 assert_eq!(cfg.root.as_deref(), Some("user/hadoop/data"));
99 assert_eq!(cfg.user_name.as_deref(), Some("hadoop"));
100 }
101
102 #[test]
103 fn from_uri_allows_missing_authority() {
104 let uri = OperatorUri::new("webhdfs", Vec::<(String, String)>::new()).unwrap();
105
106 let cfg = WebhdfsConfig::from_uri(&uri).unwrap();
107 assert!(cfg.endpoint.is_none());
108 }
109}