This is an automated email from the ASF dual-hosted git repository.

xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git


The following commit(s) were added to refs/heads/main by this push:
     new c216a5b31 refactor(service/ftp): Add FtpConfig to implement 
ConfigDeserializer (#3510)
c216a5b31 is described below

commit c216a5b314d8e35d4cb2d462e6524770068b407e
Author: 蛋疼的蛋蛋 <[email protected]>
AuthorDate: Wed Nov 8 00:40:36 2023 +0800

    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 0fe5a1778..e7e4b101f 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<String>,
+    /// root of this backend
+    pub root: Option<String>,
+    /// user of this backend
+    pub user: Option<String>,
+    /// password of this backend
+    pub password: Option<String>,
+}
+
+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<String>,
-    root: Option<String>,
-    user: Option<String>,
-    password: Option<String>,
+    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<Self::Accessor> {
         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<String, String>) -> 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 5258707d2..963732fd4 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 475cd57f3..2c18ad678 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;

Reply via email to