From 562a1b3ae8c098a5201cc033f982ca026e088ff2 Mon Sep 17 00:00:00 2001 From: ChristianVisintin Date: Wed, 16 Dec 2020 16:35:11 +0100 Subject: [PATCH] Clippy optimizations --- src/activity_manager.rs | 2 +- src/system/bookmarks_client.rs | 77 ++++++++++++++----- src/ui/activities/auth_activity/bookmarks.rs | 2 +- .../activities/filetransfer_activity/mod.rs | 2 +- 4 files changed, 59 insertions(+), 24 deletions(-) diff --git a/src/activity_manager.rs b/src/activity_manager.rs index 68133c3..de81411 100644 --- a/src/activity_manager.rs +++ b/src/activity_manager.rs @@ -160,7 +160,7 @@ impl ActivityManager { 0 => None, _ => Some(activity.password.clone()), }, - protocol: activity.protocol.clone(), + protocol: activity.protocol, }); break; } diff --git a/src/system/bookmarks_client.rs b/src/system/bookmarks_client.rs index ac9f05d..0f48c35 100644 --- a/src/system/bookmarks_client.rs +++ b/src/system/bookmarks_client.rs @@ -101,7 +101,7 @@ impl BookmarksClient { /// Get bookmark associated to key pub fn get_bookmark( &self, - key: &String, + key: &str, ) -> Option<(String, u16, FileTransferProtocol, String, Option)> { let entry: &Bookmark = self.hosts.bookmarks.get(key)?; Some(( @@ -145,7 +145,7 @@ impl BookmarksClient { /// ### del_bookmark /// /// Delete entry from bookmarks - pub fn del_bookmark(&mut self, name: &String) { + pub fn del_bookmark(&mut self, name: &str) { let _ = self.hosts.bookmarks.remove(name); } /// ### iter_recents @@ -158,7 +158,7 @@ impl BookmarksClient { /// ### get_recent /// /// Get recent associated to key - pub fn get_recent(&self, key: &String) -> Option<(String, u16, FileTransferProtocol, String)> { + pub fn get_recent(&self, key: &str) -> Option<(String, u16, FileTransferProtocol, String)> { // NOTE: password is not decrypted; recents will never have password let entry: &Bookmark = self.hosts.recents.get(key)?; Some(( @@ -217,7 +217,7 @@ impl BookmarksClient { /// ### del_recent /// /// Delete entry from recents - pub fn del_recent(&mut self, name: &String) { + pub fn del_recent(&mut self, name: &str) { let _ = self.hosts.recents.remove(name); } @@ -392,7 +392,8 @@ mod tests { let tmp_dir: tempfile::TempDir = create_tmp_dir(); let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path()); // Initialize a new bookmarks client - let client: BookmarksClient = BookmarksClient::new(cfg_path.as_path(), key_path.as_path()).unwrap(); + let client: BookmarksClient = + BookmarksClient::new(cfg_path.as_path(), key_path.as_path()).unwrap(); // Verify client assert_eq!(client.hosts.bookmarks.len(), 0); assert_eq!(client.hosts.recents.len(), 0); @@ -402,7 +403,10 @@ mod tests { #[test] fn test_system_bookmarks_new_err() { - assert!(BookmarksClient::new(Path::new("/tmp/oifoif/omar"), Path::new("/tmp/efnnu/omar")).is_err()); + assert!( + BookmarksClient::new(Path::new("/tmp/oifoif/omar"), Path::new("/tmp/efnnu/omar")) + .is_err() + ); } #[test] @@ -410,24 +414,40 @@ mod tests { let tmp_dir: tempfile::TempDir = create_tmp_dir(); let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path()); // Initialize a new bookmarks client - let mut client: BookmarksClient = BookmarksClient::new(cfg_path.as_path(), key_path.as_path()).unwrap(); + let mut client: BookmarksClient = + BookmarksClient::new(cfg_path.as_path(), key_path.as_path()).unwrap(); // Add some bookmarks - client.add_bookmark(String::from("raspberry"), String::from("192.168.1.31"), 22, FileTransferProtocol::Sftp, String::from("pi"), Some(String::from("mypassword"))); - client.add_recent(String::from("192.168.1.31"), 22, FileTransferProtocol::Sftp, String::from("pi")); + client.add_bookmark( + String::from("raspberry"), + String::from("192.168.1.31"), + 22, + FileTransferProtocol::Sftp, + String::from("pi"), + Some(String::from("mypassword")), + ); + client.add_recent( + String::from("192.168.1.31"), + 22, + FileTransferProtocol::Sftp, + String::from("pi"), + ); let recent_key: String = String::from(client.iter_recents().next().unwrap()); assert!(client.write_bookmarks().is_ok()); let key: String = client.key.clone(); // Re-initialize a client - let client: BookmarksClient = BookmarksClient::new(cfg_path.as_path(), key_path.as_path()).unwrap(); + let client: BookmarksClient = + BookmarksClient::new(cfg_path.as_path(), key_path.as_path()).unwrap(); // Verify it loaded parameters correctly assert_eq!(client.key, key); - let bookmark: (String, u16, FileTransferProtocol, String, Option) = client.get_bookmark(&String::from("raspberry")).unwrap(); + let bookmark: (String, u16, FileTransferProtocol, String, Option) = + client.get_bookmark(&String::from("raspberry")).unwrap(); assert_eq!(bookmark.0, String::from("192.168.1.31")); assert_eq!(bookmark.1, 22); assert_eq!(bookmark.2, FileTransferProtocol::Sftp); assert_eq!(bookmark.3, String::from("pi")); assert_eq!(*bookmark.4.as_ref().unwrap(), String::from("mypassword")); - let bookmark: (String, u16, FileTransferProtocol, String) = client.get_recent(&recent_key).unwrap(); + let bookmark: (String, u16, FileTransferProtocol, String) = + client.get_recent(&recent_key).unwrap(); assert_eq!(bookmark.0, String::from("192.168.1.31")); assert_eq!(bookmark.1, 22); assert_eq!(bookmark.2, FileTransferProtocol::Sftp); @@ -439,11 +459,20 @@ mod tests { let tmp_dir: tempfile::TempDir = create_tmp_dir(); let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path()); // Initialize a new bookmarks client - let mut client: BookmarksClient = BookmarksClient::new(cfg_path.as_path(), key_path.as_path()).unwrap(); + let mut client: BookmarksClient = + BookmarksClient::new(cfg_path.as_path(), key_path.as_path()).unwrap(); // Add bookmark - client.add_bookmark(String::from("raspberry"), String::from("192.168.1.31"), 22, FileTransferProtocol::Sftp, String::from("pi"), Some(String::from("mypassword"))); + client.add_bookmark( + String::from("raspberry"), + String::from("192.168.1.31"), + 22, + FileTransferProtocol::Sftp, + String::from("pi"), + Some(String::from("mypassword")), + ); // Get bookmark - let bookmark: (String, u16, FileTransferProtocol, String, Option) = client.get_bookmark(&String::from("raspberry")).unwrap(); + let bookmark: (String, u16, FileTransferProtocol, String, Option) = + client.get_bookmark(&String::from("raspberry")).unwrap(); assert_eq!(bookmark.0, String::from("192.168.1.31")); assert_eq!(bookmark.1, 22); assert_eq!(bookmark.2, FileTransferProtocol::Sftp); @@ -464,12 +493,19 @@ mod tests { let tmp_dir: tempfile::TempDir = create_tmp_dir(); let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path()); // Initialize a new bookmarks client - let mut client: BookmarksClient = BookmarksClient::new(cfg_path.as_path(), key_path.as_path()).unwrap(); + let mut client: BookmarksClient = + BookmarksClient::new(cfg_path.as_path(), key_path.as_path()).unwrap(); // Add bookmark - client.add_recent(String::from("192.168.1.31"), 22, FileTransferProtocol::Sftp, String::from("pi")); + client.add_recent( + String::from("192.168.1.31"), + 22, + FileTransferProtocol::Sftp, + String::from("pi"), + ); let key: String = String::from(client.iter_recents().next().unwrap()); // Get bookmark - let bookmark: (String, u16, FileTransferProtocol, String) = client.get_recent(&key).unwrap(); + let bookmark: (String, u16, FileTransferProtocol, String) = + client.get_recent(&key).unwrap(); assert_eq!(bookmark.0, String::from("192.168.1.31")); assert_eq!(bookmark.1, 22); assert_eq!(bookmark.2, FileTransferProtocol::Sftp); @@ -485,7 +521,7 @@ mod tests { } /// ### get_paths - /// + /// /// Get paths for configuration and key for bookmarks fn get_paths(dir: &Path) -> (PathBuf, PathBuf) { let mut k: PathBuf = PathBuf::from(dir); @@ -496,10 +532,9 @@ mod tests { } /// ### create_tmp_dir - /// + /// /// Create temporary directory fn create_tmp_dir() -> tempfile::TempDir { tempfile::TempDir::new().ok().unwrap() } - } diff --git a/src/ui/activities/auth_activity/bookmarks.rs b/src/ui/activities/auth_activity/bookmarks.rs index eddc7cd..e473c81 100644 --- a/src/ui/activities/auth_activity/bookmarks.rs +++ b/src/ui/activities/auth_activity/bookmarks.rs @@ -228,7 +228,7 @@ impl AuthActivity { // Prepare paths let mut bookmarks_file: PathBuf = path.clone(); bookmarks_file.push("bookmarks.toml"); - let mut key_file: PathBuf = path.clone(); + let mut key_file: PathBuf = path; key_file.push(".bookmarks.key"); // key file is hidden // Initialize client match BookmarksClient::new(bookmarks_file.as_path(), key_file.as_path()) { diff --git a/src/ui/activities/filetransfer_activity/mod.rs b/src/ui/activities/filetransfer_activity/mod.rs index ce7f5c3..dba2add 100644 --- a/src/ui/activities/filetransfer_activity/mod.rs +++ b/src/ui/activities/filetransfer_activity/mod.rs @@ -269,7 +269,7 @@ impl FileTransferActivity { /// /// Instantiates a new FileTransferActivity pub fn new(params: FileTransferParams) -> FileTransferActivity { - let protocol: FileTransferProtocol = params.protocol.clone(); + let protocol: FileTransferProtocol = params.protocol; FileTransferActivity { disconnected: false, quit: false,