opendal/services/webhdfs/
config.rs1use std::fmt::Debug;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23use super::WEBHDFS_SCHEME;
24use super::backend::WebhdfsBuilder;
25
26#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
28#[serde(default)]
29#[non_exhaustive]
30pub struct WebhdfsConfig {
31 pub root: Option<String>,
33 pub endpoint: Option<String>,
35 pub user_name: Option<String>,
37 pub delegation: Option<String>,
39 pub disable_list_batch: bool,
41 pub atomic_write_dir: Option<String>,
43}
44
45impl Debug for WebhdfsConfig {
46 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47 f.debug_struct("WebhdfsConfig")
48 .field("root", &self.root)
49 .field("endpoint", &self.endpoint)
50 .field("user_name", &self.user_name)
51 .field("disable_list_batch", &self.disable_list_batch)
52 .field("atomic_write_dir", &self.atomic_write_dir)
53 .finish_non_exhaustive()
54 }
55}
56
57impl crate::Configurator for WebhdfsConfig {
58 type Builder = WebhdfsBuilder;
59
60 fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
61 let authority = uri.authority().ok_or_else(|| {
62 crate::Error::new(crate::ErrorKind::ConfigInvalid, "uri authority is required")
63 .with_context("service", WEBHDFS_SCHEME)
64 })?;
65
66 let mut map = uri.options().clone();
67 map.insert("endpoint".to_string(), format!("http://{authority}"));
68
69 if let Some(root) = uri.root() {
70 if !root.is_empty() {
71 map.insert("root".to_string(), root.to_string());
72 }
73 }
74
75 Self::from_iter(map)
76 }
77
78 fn into_builder(self) -> Self::Builder {
79 WebhdfsBuilder { config: self }
80 }
81}
82
83#[cfg(test)]
84mod tests {
85 use super::*;
86 use crate::Configurator;
87 use crate::types::OperatorUri;
88
89 #[test]
90 fn from_uri_sets_endpoint_and_root() {
91 let uri = OperatorUri::new(
92 "webhdfs://namenode.example.com:50070/user/hadoop/data",
93 vec![("user_name".to_string(), "hadoop".to_string())],
94 )
95 .unwrap();
96
97 let cfg = WebhdfsConfig::from_uri(&uri).unwrap();
98 assert_eq!(
99 cfg.endpoint.as_deref(),
100 Some("http://namenode.example.com:50070")
101 );
102 assert_eq!(cfg.root.as_deref(), Some("user/hadoop/data"));
103 assert_eq!(cfg.user_name.as_deref(), Some("hadoop"));
104 }
105}