Skip to content

Commit

Permalink
refactor(service/ftp): Add FtpConfig to implement ConfigDeserializer (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
sd44 authored Nov 7, 2023
1 parent 8465d2d commit c216a5b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 23 deletions.
64 changes: 41 additions & 23 deletions core/src/services/ftp/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,51 @@ 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()
}
}

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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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,
};
Expand Down Expand Up @@ -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(),
};
Expand All @@ -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"),
}
}
}

Expand Down
1 change: 1 addition & 0 deletions core/src/services/ftp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

mod backend;
pub use backend::FtpBuilder as Ftp;
pub use backend::FtpConfig;

mod err;
mod pager;
Expand Down
2 changes: 2 additions & 0 deletions core/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c216a5b

Please sign in to comment.