>(mut self, dir: Option) -> Self {
+ self.entry_directory = dir.map(|x| x.as_ref().to_path_buf());
+ self
+ }
+}
+
+impl Default for FileTransferParams {
+ fn default() -> Self {
+ Self::new(FileTransferProtocol::Sftp, ProtocolParams::default())
+ }
+}
+
+impl Default for ProtocolParams {
+ fn default() -> Self {
+ Self::Generic(GenericProtocolParams::default())
+ }
+}
+
+impl ProtocolParams {
+ /// ### generic_params
+ ///
+ /// Retrieve generic parameters from protocol params if any
+ pub fn generic_params(&self) -> Option<&GenericProtocolParams> {
+ match self {
+ ProtocolParams::Generic(params) => Some(params),
+ _ => None,
+ }
+ }
+
+ pub fn mut_generic_params(&mut self) -> Option<&mut GenericProtocolParams> {
+ match self {
+ ProtocolParams::Generic(params) => Some(params),
+ _ => None,
+ }
+ }
+
+ /// ### s3_params
+ ///
+ /// Retrieve AWS S3 parameters if any
+ pub fn s3_params(&self) -> Option<&AwsS3Params> {
+ match self {
+ ProtocolParams::AwsS3(params) => Some(params),
+ _ => None,
+ }
+ }
+}
+
+// -- Generic protocol params
+
+impl Default for GenericProtocolParams {
+ fn default() -> Self {
+ Self {
+ address: "localhost".to_string(),
+ port: 22,
+ username: None,
+ password: None,
+ }
+ }
+}
+
+impl GenericProtocolParams {
+ /// ### address
+ ///
+ /// Set address to params
+ pub fn address>(mut self, address: S) -> Self {
+ self.address = address.as_ref().to_string();
self
}
- /// ### protocol
+ /// ### port
///
- /// Set protocol for params
- pub fn protocol(mut self, protocol: FileTransferProtocol) -> Self {
- self.protocol = protocol;
+ /// Set port to params
+ pub fn port(mut self, port: u16) -> Self {
+ self.port = port;
self
}
@@ -88,19 +176,20 @@ impl FileTransferParams {
self.password = password.map(|x| x.as_ref().to_string());
self
}
-
- /// ### entry_directory
- ///
- /// Set entry directory
- pub fn entry_directory>(mut self, dir: Option) -> Self {
- self.entry_directory = dir.map(|x| x.as_ref().to_path_buf());
- self
- }
}
-impl Default for FileTransferParams {
- fn default() -> Self {
- Self::new("localhost")
+// -- S3 params
+
+impl AwsS3Params {
+ /// ### new
+ ///
+ /// Instantiates a new `AwsS3Params` struct
+ pub fn new>(bucket: S, region: S, profile: Option) -> Self {
+ Self {
+ bucket_name: bucket.as_ref().to_string(),
+ region: region.as_ref().to_string(),
+ profile: profile.map(|x| x.as_ref().to_string()),
+ }
}
}
@@ -112,26 +201,49 @@ mod test {
#[test]
fn test_filetransfer_params() {
- let params: FileTransferParams = FileTransferParams::new("test.rebex.net")
- .port(2222)
- .protocol(FileTransferProtocol::Scp)
- .username(Some("omar"))
- .password(Some("foobar"))
- .entry_directory(Some(&Path::new("/tmp")));
- assert_eq!(params.address.as_str(), "test.rebex.net");
- assert_eq!(params.port, 2222);
+ let params: FileTransferParams =
+ FileTransferParams::new(FileTransferProtocol::Scp, ProtocolParams::default())
+ .entry_directory(Some(&Path::new("/tmp")));
+ assert_eq!(
+ params.params.generic_params().unwrap().address.as_str(),
+ "localhost"
+ );
assert_eq!(params.protocol, FileTransferProtocol::Scp);
- assert_eq!(params.username.as_ref().unwrap(), "omar");
- assert_eq!(params.password.as_ref().unwrap(), "foobar");
+ assert_eq!(
+ params.entry_directory.as_deref().unwrap(),
+ Path::new("/tmp")
+ );
}
#[test]
- fn test_filetransfer_params_default() {
- let params: FileTransferParams = FileTransferParams::default();
+ fn params_default() {
+ let params: GenericProtocolParams = ProtocolParams::default()
+ .generic_params()
+ .unwrap()
+ .to_owned();
assert_eq!(params.address.as_str(), "localhost");
assert_eq!(params.port, 22);
- assert_eq!(params.protocol, FileTransferProtocol::Sftp);
assert!(params.username.is_none());
assert!(params.password.is_none());
}
+
+ #[test]
+ fn params_aws_s3() {
+ let params: AwsS3Params = AwsS3Params::new("omar", "eu-west-1", Some("test"));
+ assert_eq!(params.bucket_name.as_str(), "omar");
+ assert_eq!(params.region.as_str(), "eu-west-1");
+ assert_eq!(params.profile.as_deref().unwrap(), "test");
+ }
+
+ #[test]
+ fn references() {
+ let mut params = ProtocolParams::AwsS3(AwsS3Params::new("omar", "eu-west-1", Some("test")));
+ assert!(params.s3_params().is_some());
+ assert!(params.generic_params().is_none());
+ assert!(params.mut_generic_params().is_none());
+ let mut params = ProtocolParams::default();
+ assert!(params.s3_params().is_none());
+ assert!(params.generic_params().is_some());
+ assert!(params.mut_generic_params().is_some());
+ }
}
diff --git a/src/filetransfer/ftp_transfer.rs b/src/filetransfer/transfer/ftp.rs
similarity index 92%
rename from src/filetransfer/ftp_transfer.rs
rename to src/filetransfer/transfer/ftp.rs
index b0f5b82..4d9ea65 100644
--- a/src/filetransfer/ftp_transfer.rs
+++ b/src/filetransfer/transfer/ftp.rs
@@ -1,4 +1,4 @@
-//! ## Ftp_transfer
+//! ## FTP transfer
//!
//! `ftp_transfer` is the module which provides the implementation for the FTP/FTPS file transfer
@@ -25,7 +25,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
-use super::{FileTransfer, FileTransferError, FileTransferErrorType};
+use super::{FileTransfer, FileTransferError, FileTransferErrorType, ProtocolParams};
use crate::fs::{FsDirectory, FsEntry, FsFile, UnixPex};
use crate::utils::fmt::shadow_password;
use crate::utils::path;
@@ -178,25 +178,24 @@ impl FileTransfer for FtpFileTransfer {
///
/// Connect to the remote server
- fn connect(
- &mut self,
- address: String,
- port: u16,
- username: Option,
- password: Option,
- ) -> Result