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