Optimized code and performance using clippy

This commit is contained in:
ChristianVisintin
2020-12-12 12:14:51 +01:00
parent 0eae159bb9
commit 55bda874f0
16 changed files with 716 additions and 772 deletions

View File

@@ -53,10 +53,7 @@ impl FtpFileTransfer {
///
/// Instantiates a new `FtpFileTransfer`
pub fn new(ftps: bool) -> FtpFileTransfer {
FtpFileTransfer {
stream: None,
ftps: ftps,
}
FtpFileTransfer { stream: None, ftps }
}
/// ### parse_list_line
@@ -97,13 +94,12 @@ impl FtpFileTransfer {
match c {
'-' => {}
_ => {
count = count
+ match i {
0 => 4,
1 => 2,
2 => 1,
_ => 0,
}
count += match i {
0 => 4,
1 => 2,
2 => 1,
_ => 0,
}
}
}
}
@@ -115,13 +111,12 @@ impl FtpFileTransfer {
match c {
'-' => {}
_ => {
count = count
+ match i {
0 => 4,
1 => 2,
2 => 1,
_ => 0,
}
count += match i {
0 => 4,
1 => 2,
2 => 1,
_ => 0,
}
}
}
}
@@ -133,13 +128,12 @@ impl FtpFileTransfer {
match c {
'-' => {}
_ => {
count = count
+ match i {
0 => 4,
1 => 2,
2 => 1,
_ => 0,
}
count += match i {
0 => 4,
1 => 2,
2 => 1,
_ => 0,
}
}
}
}
@@ -174,7 +168,7 @@ impl FtpFileTransfer {
let file_name: String = String::from(metadata.get(8).unwrap().as_str());
// Check if file_name is '.' or '..'
if file_name.as_str() == "." || file_name.as_str() == ".." {
return Err(())
return Err(());
}
let mut abs_path: PathBuf = PathBuf::from(path);
let extension: Option<String> = match abs_path.as_path().extension() {
@@ -187,7 +181,7 @@ impl FtpFileTransfer {
Ok(match is_dir {
true => FsEntry::Directory(FsDirectory {
name: file_name,
abs_path: abs_path,
abs_path,
last_change_time: mtime,
last_access_time: mtime,
creation_time: mtime,
@@ -199,7 +193,7 @@ impl FtpFileTransfer {
}),
false => FsEntry::File(FsFile {
name: file_name,
abs_path: abs_path,
abs_path,
last_change_time: mtime,
last_access_time: mtime,
creation_time: mtime,
@@ -267,11 +261,11 @@ impl FileTransfer for FtpFileTransfer {
}
// Login (use anonymous if credentials are unspecified)
let username: String = match username {
Some(u) => u.clone(),
Some(u) => u,
None => String::from("anonymous"),
};
let password: String = match password {
Some(pwd) => String::from(pwd),
Some(pwd) => pwd,
None => String::new(),
};
if let Err(err) = stream.login(username.as_str(), password.as_str()) {
@@ -309,10 +303,7 @@ impl FileTransfer for FtpFileTransfer {
///
/// Indicates whether the client is connected to remote
fn is_connected(&self) -> bool {
match self.stream {
Some(_) => true,
None => false,
}
self.stream.is_some()
}
/// ### pwd

View File

@@ -79,7 +79,7 @@ impl FileTransferError {
/// Instantiates a new FileTransferError
pub fn new(code: FileTransferErrorType) -> FileTransferError {
FileTransferError {
code: code,
code,
msg: None,
}
}

View File

@@ -48,6 +48,12 @@ pub struct ScpFileTransfer {
wrkdir: PathBuf,
}
impl Default for ScpFileTransfer {
fn default() -> Self {
Self::new()
}
}
impl ScpFileTransfer {
/// ### new
///
@@ -97,13 +103,12 @@ impl ScpFileTransfer {
match c {
'-' => {}
_ => {
count = count
+ match i {
0 => 4,
1 => 2,
2 => 1,
_ => 0,
}
count += match i {
0 => 4,
1 => 2,
2 => 1,
_ => 0,
}
}
}
}
@@ -115,13 +120,12 @@ impl ScpFileTransfer {
match c {
'-' => {}
_ => {
count = count
+ match i {
0 => 4,
1 => 2,
2 => 1,
_ => 0,
}
count += match i {
0 => 4,
1 => 2,
2 => 1,
_ => 0,
}
}
}
}
@@ -133,13 +137,12 @@ impl ScpFileTransfer {
match c {
'-' => {}
_ => {
count = count
+ match i {
0 => 4,
1 => 2,
2 => 1,
_ => 0,
}
count += match i {
0 => 4,
1 => 2,
2 => 1,
_ => 0,
}
}
}
}
@@ -178,7 +181,7 @@ impl ScpFileTransfer {
};
// Check if file_name is '.' or '..'
if file_name.as_str() == "." || file_name.as_str() == ".." {
return Err(())
return Err(());
}
let mut abs_path: PathBuf = PathBuf::from(path);
let extension: Option<String> = match abs_path.as_path().extension() {
@@ -191,7 +194,7 @@ impl ScpFileTransfer {
Ok(match is_dir {
true => FsEntry::Directory(FsDirectory {
name: file_name,
abs_path: abs_path,
abs_path,
last_change_time: mtime,
last_access_time: mtime,
creation_time: mtime,
@@ -203,7 +206,7 @@ impl ScpFileTransfer {
}),
false => FsEntry::File(FsFile {
name: file_name,
abs_path: abs_path,
abs_path,
last_change_time: mtime,
last_access_time: mtime,
creation_time: mtime,
@@ -331,15 +334,15 @@ impl FileTransfer for ScpFileTransfer {
));
}
let username: String = match username {
Some(u) => u.clone(),
Some(u) => u,
None => String::from(""),
};
// Try authenticating with user agent
if let Err(_) = session.userauth_agent(username.as_str()) {
if session.userauth_agent(username.as_str()).is_err() {
// Try authentication with password then
if let Err(err) = session.userauth_password(
username.as_str(),
password.unwrap_or(String::from("")).as_str(),
password.unwrap_or_else(|| String::from("")).as_str(),
) {
return Err(FileTransferError::new_ex(
FileTransferErrorType::AuthenticationFailed,
@@ -391,10 +394,7 @@ impl FileTransfer for ScpFileTransfer {
///
/// Indicates whether the client is connected to remote
fn is_connected(&self) -> bool {
match self.session.as_ref() {
Some(_) => true,
None => false,
}
self.session.as_ref().is_some()
}
/// ### pwd
@@ -435,7 +435,7 @@ impl FileTransfer for ScpFileTransfer {
// Trim
let output: String = String::from(output.as_str().trim());
// Check if output starts with 0; should be 0{PWD}
match output.as_str().starts_with("0") {
match output.as_str().starts_with('0') {
true => {
// Set working directory
self.wrkdir = PathBuf::from(&output.as_str()[1..].trim());
@@ -857,7 +857,7 @@ mod tests {
}
#[test]
#[cfg(any(unix, macos, linux))]
#[cfg(any(target_os = "unix", target_os = "macos", target_os = "linux"))]
fn test_filetransfer_scp_cwd() {
let mut client: ScpFileTransfer = ScpFileTransfer::new();
assert!(client

View File

@@ -46,6 +46,12 @@ pub struct SftpFileTransfer {
wrkdir: PathBuf,
}
impl Default for SftpFileTransfer {
fn default() -> Self {
Self::new()
}
}
impl SftpFileTransfer {
/// ### new
///
@@ -68,7 +74,7 @@ impl SftpFileTransfer {
root.push(p);
match self.sftp.as_ref().unwrap().realpath(root.as_path()) {
Ok(p) => match self.sftp.as_ref().unwrap().stat(p.as_path()) {
Ok(_) => Ok(PathBuf::from(p)),
Ok(_) => Ok(p),
Err(err) => Err(FileTransferError::new_ex(
FileTransferErrorType::NoSuchFileOrDirectory,
format!("{}", err),
@@ -82,7 +88,7 @@ impl SftpFileTransfer {
}
false => match self.sftp.as_ref().unwrap().realpath(p) {
Ok(p) => match self.sftp.as_ref().unwrap().stat(p.as_path()) {
Ok(_) => Ok(PathBuf::from(p)),
Ok(_) => Ok(p),
Err(err) => Err(FileTransferError::new_ex(
FileTransferErrorType::NoSuchFileOrDirectory,
format!("{}", err),
@@ -162,7 +168,7 @@ impl SftpFileTransfer {
last_access_time: atime,
creation_time: SystemTime::UNIX_EPOCH,
readonly: false,
symlink: symlink,
symlink,
user: uid,
group: gid,
unix_pex: pex,
@@ -176,7 +182,7 @@ impl SftpFileTransfer {
last_access_time: atime,
creation_time: SystemTime::UNIX_EPOCH,
readonly: false,
symlink: symlink,
symlink,
user: uid,
group: gid,
unix_pex: pex,
@@ -226,15 +232,15 @@ impl FileTransfer for SftpFileTransfer {
));
}
let username: String = match username {
Some(u) => u.clone(),
Some(u) => u,
None => String::from(""),
};
// Try authenticating with user agent
if let Err(_) = session.userauth_agent(username.as_str()) {
if session.userauth_agent(username.as_str()).is_err() {
// Try authentication with password then
if let Err(err) = session.userauth_password(
username.as_str(),
password.unwrap_or(String::from("")).as_str(),
password.unwrap_or_else(|| String::from("")).as_str(),
) {
return Err(FileTransferError::new_ex(
FileTransferErrorType::AuthenticationFailed,
@@ -352,12 +358,10 @@ impl FileTransfer for SftpFileTransfer {
};
// Get files
match sftp.readdir(dir.as_path()) {
Err(err) => {
return Err(FileTransferError::new_ex(
FileTransferErrorType::DirStatFailed,
format!("{}", err),
))
}
Err(err) => Err(FileTransferError::new_ex(
FileTransferErrorType::DirStatFailed,
format!("{}", err),
)),
Ok(files) => {
// Allocate vector
let mut entries: Vec<FsEntry> = Vec::with_capacity(files.len());