mirror of
https://github.com/veeso/termscp.git
synced 2025-12-07 09:36:00 -08:00
FileTransferProtocol ToString and FromStr traits
This commit is contained in:
@@ -226,6 +226,34 @@ pub trait FileTransfer {
|
|||||||
fn on_recv(&mut self, readable: Box<dyn Read>) -> Result<(), FileTransferError>;
|
fn on_recv(&mut self, readable: Box<dyn Read>) -> Result<(), FileTransferError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Traits
|
||||||
|
|
||||||
|
impl std::string::ToString for FileTransferProtocol {
|
||||||
|
fn to_string(&self) -> String {
|
||||||
|
String::from(match self {
|
||||||
|
FileTransferProtocol::Ftp(secure) => match secure {
|
||||||
|
true => "FTPS",
|
||||||
|
false => "FTP",
|
||||||
|
},
|
||||||
|
FileTransferProtocol::Scp => "SCP",
|
||||||
|
FileTransferProtocol::Sftp => "SFTP",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::str::FromStr for FileTransferProtocol {
|
||||||
|
type Err = ();
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
match s.to_ascii_uppercase().as_str() {
|
||||||
|
"FTP" => Ok(FileTransferProtocol::Ftp(false)),
|
||||||
|
"FTPS" => Ok(FileTransferProtocol::Ftp(true)),
|
||||||
|
"SCP" => Ok(FileTransferProtocol::Scp),
|
||||||
|
"SFTP" => Ok(FileTransferProtocol::Sftp),
|
||||||
|
_ => Err(()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Tests
|
// Tests
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -233,6 +261,9 @@ mod tests {
|
|||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
use std::str::FromStr;
|
||||||
|
use std::string::ToString;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_filetransfer_mod_protocol() {
|
fn test_filetransfer_mod_protocol() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@@ -243,6 +274,52 @@ mod tests {
|
|||||||
FileTransferProtocol::Ftp(false),
|
FileTransferProtocol::Ftp(false),
|
||||||
FileTransferProtocol::Ftp(false)
|
FileTransferProtocol::Ftp(false)
|
||||||
);
|
);
|
||||||
|
// From str
|
||||||
|
assert_eq!(
|
||||||
|
FileTransferProtocol::from_str("FTPS").ok().unwrap(),
|
||||||
|
FileTransferProtocol::Ftp(true)
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
FileTransferProtocol::from_str("ftps").ok().unwrap(),
|
||||||
|
FileTransferProtocol::Ftp(true)
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
FileTransferProtocol::from_str("FTP").ok().unwrap(),
|
||||||
|
FileTransferProtocol::Ftp(false)
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
FileTransferProtocol::from_str("ftp").ok().unwrap(),
|
||||||
|
FileTransferProtocol::Ftp(false)
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
FileTransferProtocol::from_str("SFTP").ok().unwrap(),
|
||||||
|
FileTransferProtocol::Sftp
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
FileTransferProtocol::from_str("sftp").ok().unwrap(),
|
||||||
|
FileTransferProtocol::Sftp
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
FileTransferProtocol::from_str("SCP").ok().unwrap(),
|
||||||
|
FileTransferProtocol::Scp
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
FileTransferProtocol::from_str("scp").ok().unwrap(),
|
||||||
|
FileTransferProtocol::Scp
|
||||||
|
);
|
||||||
|
// Error
|
||||||
|
assert!(FileTransferProtocol::from_str("dummy").is_err());
|
||||||
|
// To String
|
||||||
|
assert_eq!(
|
||||||
|
FileTransferProtocol::Ftp(true).to_string(),
|
||||||
|
String::from("FTPS")
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
FileTransferProtocol::Ftp(false).to_string(),
|
||||||
|
String::from("FTP")
|
||||||
|
);
|
||||||
|
assert_eq!(FileTransferProtocol::Scp.to_string(), String::from("SCP"));
|
||||||
|
assert_eq!(FileTransferProtocol::Sftp.to_string(), String::from("SFTP"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -37,6 +37,8 @@ use rand::{distributions::Alphanumeric, thread_rng, Rng};
|
|||||||
use std::fs::{OpenOptions, Permissions};
|
use std::fs::{OpenOptions, Permissions};
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::str::FromStr;
|
||||||
|
use std::string::ToString;
|
||||||
use std::time::SystemTime;
|
use std::time::SystemTime;
|
||||||
|
|
||||||
/// ## BookmarksClient
|
/// ## BookmarksClient
|
||||||
@@ -112,11 +114,9 @@ impl BookmarksClient {
|
|||||||
Some((
|
Some((
|
||||||
entry.address.clone(),
|
entry.address.clone(),
|
||||||
entry.port,
|
entry.port,
|
||||||
match entry.protocol.to_ascii_uppercase().as_str() {
|
match FileTransferProtocol::from_str(entry.protocol.as_str()) {
|
||||||
"FTP" => FileTransferProtocol::Ftp(false),
|
Ok(proto) => proto,
|
||||||
"FTPS" => FileTransferProtocol::Ftp(true),
|
Err(_) => FileTransferProtocol::Sftp, // Default
|
||||||
"SCP" => FileTransferProtocol::Scp,
|
|
||||||
_ => FileTransferProtocol::Sftp,
|
|
||||||
},
|
},
|
||||||
entry.username.clone(),
|
entry.username.clone(),
|
||||||
match &entry.password {
|
match &entry.password {
|
||||||
@@ -172,11 +172,9 @@ impl BookmarksClient {
|
|||||||
Some((
|
Some((
|
||||||
entry.address.clone(),
|
entry.address.clone(),
|
||||||
entry.port,
|
entry.port,
|
||||||
match entry.protocol.to_ascii_uppercase().as_str() {
|
match FileTransferProtocol::from_str(entry.protocol.as_str()) {
|
||||||
"FTP" => FileTransferProtocol::Ftp(false),
|
Ok(proto) => proto,
|
||||||
"FTPS" => FileTransferProtocol::Ftp(true),
|
Err(_) => FileTransferProtocol::Sftp, // Default
|
||||||
"SCP" => FileTransferProtocol::Scp,
|
|
||||||
_ => FileTransferProtocol::Sftp,
|
|
||||||
},
|
},
|
||||||
entry.username.clone(),
|
entry.username.clone(),
|
||||||
))
|
))
|
||||||
@@ -333,14 +331,7 @@ impl BookmarksClient {
|
|||||||
address: addr,
|
address: addr,
|
||||||
port,
|
port,
|
||||||
username,
|
username,
|
||||||
protocol: match protocol {
|
protocol: protocol.to_string(),
|
||||||
FileTransferProtocol::Ftp(secure) => match secure {
|
|
||||||
true => String::from("FTPS"),
|
|
||||||
false => String::from("FTP"),
|
|
||||||
},
|
|
||||||
FileTransferProtocol::Scp => String::from("SCP"),
|
|
||||||
FileTransferProtocol::Sftp => String::from("SFTP"),
|
|
||||||
},
|
|
||||||
password: match password {
|
password: match password {
|
||||||
Some(p) => Some(self.encrypt_str(p.as_str())), // Encrypt password if provided
|
Some(p) => Some(self.encrypt_str(p.as_str())), // Encrypt password if provided
|
||||||
None => None,
|
None => None,
|
||||||
|
|||||||
Reference in New Issue
Block a user