From c216a5b314d8e35d4cb2d462e6524770068b407e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=9B=8B=E7=96=BC=E7=9A=84=E8=9B=8B=E8=9B=8B?= Date: Wed, 8 Nov 2023 00:40:36 +0800 Subject: [PATCH] refactor(service/ftp): Add FtpConfig to implement ConfigDeserializer (#3510) * refactor(service/ftp): Add FtpConfig to implement ConfigDeserializer * add from_map deserialize * add pub to Config member * add docstring for pub member * trigger GitHub actions --- core/src/services/ftp/backend.rs | 64 ++++++++++++++++++++------------ core/src/services/ftp/mod.rs | 1 + core/src/services/mod.rs | 2 + 3 files changed, 44 insertions(+), 23 deletions(-) diff --git a/core/src/services/ftp/backend.rs b/core/src/services/ftp/backend.rs index 0fe5a17784b2..e7e4b101f17e 100644 --- a/core/src/services/ftp/backend.rs +++ b/core/src/services/ftp/backend.rs @@ -44,21 +44,43 @@ use crate::raw::*; use crate::services::ftp::writer::FtpWriters; use crate::*; +use serde::Deserialize; + +/// Config for Ftpservices support. +#[derive(Default, Deserialize)] +#[serde(default)] +#[non_exhaustive] +pub struct FtpConfig { + /// endpoint of this backend + pub endpoint: Option, + /// root of this backend + pub root: Option, + /// user of this backend + pub user: Option, + /// password of this backend + pub password: Option, +} + +impl Debug for FtpConfig { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.debug_struct("FtpConfig") + .field("endpoint", &self.endpoint) + .field("root", &self.root) + .finish_non_exhaustive() + } +} + /// FTP and FTPS services support. #[doc = include_str!("docs.md")] #[derive(Default)] pub struct FtpBuilder { - endpoint: Option, - root: Option, - user: Option, - password: Option, + config: FtpConfig, } impl Debug for FtpBuilder { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Builder") - .field("endpoint", &self.endpoint) - .field("root", &self.root) + f.debug_struct("FtpBuilder") + .field("config", &self.config) .finish() } } @@ -66,7 +88,7 @@ impl Debug for FtpBuilder { impl FtpBuilder { /// set endpoint for ftp backend. pub fn endpoint(&mut self, endpoint: &str) -> &mut Self { - self.endpoint = if endpoint.is_empty() { + self.config.endpoint = if endpoint.is_empty() { None } else { Some(endpoint.to_string()) @@ -77,7 +99,7 @@ impl FtpBuilder { /// set root path for ftp backend. pub fn root(&mut self, root: &str) -> &mut Self { - self.root = if root.is_empty() { + self.config.root = if root.is_empty() { None } else { Some(root.to_string()) @@ -88,7 +110,7 @@ impl FtpBuilder { /// set user for ftp backend. pub fn user(&mut self, user: &str) -> &mut Self { - self.user = if user.is_empty() { + self.config.user = if user.is_empty() { None } else { Some(user.to_string()) @@ -99,7 +121,7 @@ impl FtpBuilder { /// set password for ftp backend. pub fn password(&mut self, password: &str) -> &mut Self { - self.password = if password.is_empty() { + self.config.password = if password.is_empty() { None } else { Some(password.to_string()) @@ -115,7 +137,7 @@ impl Builder for FtpBuilder { fn build(&mut self) -> Result { debug!("ftp backend build started: {:?}", &self); - let endpoint = match &self.endpoint { + let endpoint = match &self.config.endpoint { None => return Err(Error::new(ErrorKind::ConfigInvalid, "endpoint is empty")), Some(v) => v, }; @@ -149,14 +171,14 @@ impl Builder for FtpBuilder { } }; - let root = normalize_root(&self.root.take().unwrap_or_default()); + let root = normalize_root(&self.config.root.take().unwrap_or_default()); - let user = match &self.user { + let user = match &self.config.user { None => "".to_string(), Some(v) => v.clone(), }; - let password = match &self.password { + let password = match &self.config.password { None => "".to_string(), Some(v) => v.clone(), }; @@ -174,14 +196,10 @@ impl Builder for FtpBuilder { } fn from_map(map: HashMap) -> Self { - let mut builder = FtpBuilder::default(); - - map.get("root").map(|v| builder.root(v)); - map.get("endpoint").map(|v| builder.endpoint(v)); - map.get("user").map(|v| builder.user(v)); - map.get("password").map(|v| builder.password(v)); - - builder + FtpBuilder { + config: FtpConfig::deserialize(ConfigDeserializer::new(map)) + .expect("config deserialize must succeed"), + } } } diff --git a/core/src/services/ftp/mod.rs b/core/src/services/ftp/mod.rs index 5258707d2807..963732fd4126 100644 --- a/core/src/services/ftp/mod.rs +++ b/core/src/services/ftp/mod.rs @@ -17,6 +17,7 @@ mod backend; pub use backend::FtpBuilder as Ftp; +pub use backend::FtpConfig; mod err; mod pager; diff --git a/core/src/services/mod.rs b/core/src/services/mod.rs index 475cd57f3de3..2c18ad6780ea 100644 --- a/core/src/services/mod.rs +++ b/core/src/services/mod.rs @@ -58,6 +58,8 @@ pub use fs::Fs; mod ftp; #[cfg(feature = "services-ftp")] pub use ftp::Ftp; +#[cfg(feature = "services-ftp")] +pub use ftp::FtpConfig; #[cfg(feature = "services-gcs")] mod gcs;