diff --git a/src/filetransfer/ftp_transfer.rs b/src/filetransfer/ftp_transfer.rs index 8e5c2cf..fce9dc3 100644 --- a/src/filetransfer/ftp_transfer.rs +++ b/src/filetransfer/ftp_transfer.rs @@ -166,10 +166,10 @@ impl FtpFileTransfer { let mut abs_path: PathBuf = PathBuf::from(path); abs_path.push(file_name.as_str()); // get extension - let extension: Option = match abs_path.as_path().extension() { - None => None, - Some(s) => Some(String::from(s.to_string_lossy())), - }; + let extension: Option = abs_path + .as_path() + .extension() + .map(|s| String::from(s.to_string_lossy())); // Return // Push to entries Ok(match is_dir { @@ -254,10 +254,10 @@ impl FtpFileTransfer { let mut abs_path: PathBuf = PathBuf::from(path); abs_path.push(file_name.as_str()); // Get extension - let extension: Option = match abs_path.as_path().extension() { - None => None, - Some(s) => Some(String::from(s.to_string_lossy())), - }; + let extension: Option = abs_path + .as_path() + .extension() + .map(|s| String::from(s.to_string_lossy())); // Return entry Ok(match is_dir { true => FsEntry::Directory(FsDirectory { diff --git a/src/filetransfer/scp_transfer.rs b/src/filetransfer/scp_transfer.rs index 79a89ab..af4abec 100644 --- a/src/filetransfer/scp_transfer.rs +++ b/src/filetransfer/scp_transfer.rs @@ -175,10 +175,10 @@ impl ScpFileTransfer { let mut abs_path: PathBuf = PathBuf::from(path); abs_path.push(file_name.as_str()); // Get extension - let extension: Option = match abs_path.as_path().extension() { - None => None, - Some(s) => Some(String::from(s.to_string_lossy())), - }; + let extension: Option = abs_path + .as_path() + .extension() + .map(|s| String::from(s.to_string_lossy())); // Return // Push to entries Ok(match is_dir { @@ -220,10 +220,7 @@ impl ScpFileTransfer { fn get_name_and_link(&self, token: &str) -> (String, Option) { let tokens: Vec<&str> = token.split(" -> ").collect(); let filename: String = String::from(*tokens.get(0).unwrap()); - let symlink: Option = match tokens.get(1) { - Some(s) => Some(PathBuf::from(s)), - None => None, - }; + let symlink: Option = tokens.get(1).map(PathBuf::from); (filename, symlink) } @@ -382,10 +379,7 @@ impl FileTransfer for ScpFileTransfer { } } // Get banner - let banner: Option = match session.banner() { - Some(s) => Some(String::from(s)), - None => None, - }; + let banner: Option = session.banner().map(String::from); // Set session self.session = Some(session); // Get working directory diff --git a/src/filetransfer/sftp_transfer.rs b/src/filetransfer/sftp_transfer.rs index e744b67..e002a5b 100644 --- a/src/filetransfer/sftp_transfer.rs +++ b/src/filetransfer/sftp_transfer.rs @@ -123,20 +123,18 @@ impl SftpFileTransfer { fn make_fsentry(&mut self, path: &Path, metadata: &FileStat) -> FsEntry { // Get common parameters let file_name: String = String::from(path.file_name().unwrap().to_str().unwrap_or("")); - let file_type: Option = match path.extension() { - Some(ext) => Some(String::from(ext.to_str().unwrap_or(""))), - None => None, - }; + let file_type: Option = path + .extension() + .map(|ext| String::from(ext.to_str().unwrap_or(""))); let uid: Option = metadata.uid; let gid: Option = metadata.gid; - let pex: Option<(u8, u8, u8)> = match metadata.perm { - Some(perms) => Some(( - ((perms >> 6) & 0x7) as u8, - ((perms >> 3) & 0x7) as u8, - (perms & 0x7) as u8, - )), - None => None, - }; + let pex: Option<(u8, u8, u8)> = metadata.perm.map(|x| { + ( + ((x >> 6) & 0x7) as u8, + ((x >> 3) & 0x7) as u8, + (x & 0x7) as u8, + ) + }); let size: u64 = metadata.size.unwrap_or(0); let mut atime: SystemTime = SystemTime::UNIX_EPOCH; atime = atime @@ -365,10 +363,7 @@ impl FileTransfer for SftpFileTransfer { } }; // Set session - let banner: Option = match session.banner() { - Some(s) => Some(String::from(s)), - None => None, - }; + let banner: Option = session.banner().map(String::from); self.session = Some(session); // Set sftp self.sftp = Some(sftp); diff --git a/src/fs/explorer/formatter.rs b/src/fs/explorer/formatter.rs index ca4a674..6294c6c 100644 --- a/src/fs/explorer/formatter.rs +++ b/src/fs/explorer/formatter.rs @@ -510,10 +510,10 @@ impl Formatter { None => None, }; // Match format extra: group 2 + 1 - let fmt_extra: Option = match ®ex_match.get(5) { - Some(extra) => Some(extra.as_str().to_string()), - None => None, - }; + let fmt_extra: Option = regex_match + .get(5) + .as_ref() + .map(|extra| extra.as_str().to_string()); // Create a callchain or push new element to its back match callchain.as_mut() { None => { diff --git a/src/fs/explorer/mod.rs b/src/fs/explorer/mod.rs index 2b44d5f..afc5f8c 100644 --- a/src/fs/explorer/mod.rs +++ b/src/fs/explorer/mod.rs @@ -190,10 +190,7 @@ impl FileExplorer { pass }) .collect::>(); - match filtered.get(idx) { - None => None, - Some(file) => Some(file), - } + filtered.get(idx).copied() } // Formatting diff --git a/src/host/mod.rs b/src/host/mod.rs index 1f3aca2..d5ad17e 100644 --- a/src/host/mod.rs +++ b/src/host/mod.rs @@ -353,10 +353,9 @@ impl Localhost { }), false => { // Is File - let extension: Option = match path.extension() { - Some(s) => Some(String::from(s.to_str().unwrap_or(""))), - None => None, - }; + let extension: Option = path + .extension() + .map(|s| String::from(s.to_str().unwrap_or(""))); FsEntry::File(FsFile { name: file_name, abs_path: path.clone(), @@ -534,13 +533,11 @@ impl Localhost { Err(err) => return Err(HostError::new(HostErrorType::DirNotAccessible, Some(err))), }; let mut fs_entries: Vec = Vec::new(); - for entry in entries { - if let Ok(entry) = entry { - fs_entries.push(match self.stat(entry.path().as_path()) { - Ok(entry) => entry, - Err(err) => return Err(err), - }); - } + for entry in entries.flatten() { + fs_entries.push(match self.stat(entry.path().as_path()) { + Ok(entry) => entry, + Err(err) => return Err(err), + }); } Ok(fs_entries) } diff --git a/src/system/bookmarks_client.rs b/src/system/bookmarks_client.rs index 72d76e8..873ae9a 100644 --- a/src/system/bookmarks_client.rs +++ b/src/system/bookmarks_client.rs @@ -342,10 +342,7 @@ impl BookmarksClient { port, username, protocol: protocol.to_string(), - password: match password { - Some(p) => Some(self.encrypt_str(p.as_str())), // Encrypt password if provided - None => None, - }, + password: password.map(|p| self.encrypt_str(p.as_str())), } } diff --git a/src/system/config_client.rs b/src/system/config_client.rs index eb02a90..2c22790 100644 --- a/src/system/config_client.rs +++ b/src/system/config_client.rs @@ -173,10 +173,7 @@ impl ConfigClient { /// Set value for group_dir in configuration. /// Provided value, if `Some` will be converted to `GroupDirs` pub fn set_group_dirs(&mut self, val: Option) { - self.config.user_interface.group_dirs = match val { - None => None, - Some(val) => Some(val.to_string()), - }; + self.config.user_interface.group_dirs = val.map(|val| val.to_string()); } /// ### get_file_fmt diff --git a/src/ui/activities/auth_activity/update.rs b/src/ui/activities/auth_activity/update.rs index d9912af..bc789d3 100644 --- a/src/ui/activities/auth_activity/update.rs +++ b/src/ui/activities/auth_activity/update.rs @@ -45,10 +45,7 @@ impl AuthActivity { /// Update auth activity model based on msg /// The function exits when returns None pub(super) fn update(&mut self, msg: Option<(String, Msg)>) -> Option<(String, Msg)> { - let ref_msg: Option<(&str, &Msg)> = match msg.as_ref() { - None => None, - Some((s, msg)) => Some((s, msg)), - }; + let ref_msg: Option<(&str, &Msg)> = msg.as_ref().map(|(s, msg)| (s.as_str(), msg)); // Match msg match ref_msg { None => None, // Exit after None diff --git a/src/ui/activities/filetransfer_activity/actions.rs b/src/ui/activities/filetransfer_activity/actions.rs index 69fcd9a..bc6a435 100644 --- a/src/ui/activities/filetransfer_activity/actions.rs +++ b/src/ui/activities/filetransfer_activity/actions.rs @@ -185,10 +185,7 @@ impl FileTransferActivity { } pub(super) fn action_local_rename(&mut self, input: String) { - let entry: Option = match self.get_local_file_entry() { - Some(f) => Some(f.clone()), - None => None, - }; + let entry: Option = self.get_local_file_entry().cloned(); if let Some(entry) = entry { let mut dst_path: PathBuf = PathBuf::from(input); // Check if path is relative @@ -265,10 +262,7 @@ impl FileTransferActivity { } pub(super) fn action_local_delete(&mut self) { - let entry: Option = match self.get_local_file_entry() { - Some(f) => Some(f.clone()), - None => None, - }; + let entry: Option = self.get_local_file_entry().cloned(); if let Some(entry) = entry { let full_path: PathBuf = entry.get_abs_path(); // Delete file or directory and report status as popup @@ -528,10 +522,7 @@ impl FileTransferActivity { } pub(super) fn action_find_transfer(&mut self, idx: usize, name: Option) { - let entry: Option = match self.found.as_ref().unwrap().get(idx) { - None => None, - Some(e) => Some(e.clone()), - }; + let entry: Option = self.found.as_ref().unwrap().get(idx).cloned(); if let Some(entry) = entry { // Download file match self.tab { @@ -548,10 +539,7 @@ impl FileTransferActivity { } pub(super) fn action_find_delete(&mut self, idx: usize) { - let entry: Option = match self.found.as_ref().unwrap().get(idx) { - None => None, - Some(e) => Some(e.clone()), - }; + let entry: Option = self.found.as_ref().unwrap().get(idx).cloned(); if let Some(entry) = entry { // Download file match self.tab { diff --git a/src/ui/activities/filetransfer_activity/update.rs b/src/ui/activities/filetransfer_activity/update.rs index db4476c..50472d0 100644 --- a/src/ui/activities/filetransfer_activity/update.rs +++ b/src/ui/activities/filetransfer_activity/update.rs @@ -57,10 +57,7 @@ impl FileTransferActivity { /// Update auth activity model based on msg /// The function exits when returns None pub(super) fn update(&mut self, msg: Option<(String, Msg)>) -> Option<(String, Msg)> { - let ref_msg: Option<(&str, &Msg)> = match msg.as_ref() { - None => None, - Some((s, msg)) => Some((s, msg)), - }; + let ref_msg: Option<(&str, &Msg)> = msg.as_ref().map(|(s, msg)| (s.as_str(), msg)); // Match msg match ref_msg { None => None, // Exit after None @@ -132,10 +129,7 @@ impl FileTransferActivity { self.update_local_filelist() } (COMPONENT_EXPLORER_LOCAL, &MSG_KEY_CHAR_I) => { - let file: Option = match self.get_local_file_entry() { - Some(f) => Some(f.clone()), - None => None, - }; + let file: Option = self.get_local_file_entry().cloned(); if let Some(file) = file { self.mount_file_info(&file); } @@ -251,10 +245,7 @@ impl FileTransferActivity { self.update_remote_filelist() } (COMPONENT_EXPLORER_REMOTE, &MSG_KEY_CHAR_I) => { - let file: Option = match self.get_remote_file_entry() { - Some(f) => Some(f.clone()), - None => None, - }; + let file: Option = self.get_remote_file_entry().cloned(); if let Some(file) = file { self.mount_file_info(&file); } diff --git a/src/ui/activities/setup_activity/actions.rs b/src/ui/activities/setup_activity/actions.rs index 0b18180..db7a45a 100644 --- a/src/ui/activities/setup_activity/actions.rs +++ b/src/ui/activities/setup_activity/actions.rs @@ -68,10 +68,7 @@ impl SetupActivity { _ => None, }; if let Some(idx) = idx { - let key: Option = match config_cli.iter_ssh_keys().nth(idx) { - Some(k) => Some(k.clone()), - None => None, - }; + let key: Option = config_cli.iter_ssh_keys().nth(idx).cloned(); if let Some(key) = key { match config_cli.get_ssh_key(&key) { Ok(opt) => { diff --git a/src/ui/activities/setup_activity/update.rs b/src/ui/activities/setup_activity/update.rs index 3168d72..39e699c 100644 --- a/src/ui/activities/setup_activity/update.rs +++ b/src/ui/activities/setup_activity/update.rs @@ -43,10 +43,7 @@ impl SetupActivity { /// Update auth activity model based on msg /// The function exits when returns None pub(super) fn update(&mut self, msg: Option<(String, Msg)>) -> Option<(String, Msg)> { - let ref_msg: Option<(&str, &Msg)> = match msg.as_ref() { - None => None, - Some((s, msg)) => Some((s, msg)), - }; + let ref_msg: Option<(&str, &Msg)> = msg.as_ref().map(|(s, msg)| (s.as_str(), msg)); // Match msg match ref_msg { None => None, diff --git a/src/ui/layout/components/text.rs b/src/ui/layout/components/text.rs index b803785..653f042 100644 --- a/src/ui/layout/components/text.rs +++ b/src/ui/layout/components/text.rs @@ -35,23 +35,10 @@ use tui::{ widgets::Paragraph, }; -// -- state - -struct OwnStates { - focus: bool, -} - -impl Default for OwnStates { - fn default() -> Self { - OwnStates { focus: false } - } -} - // -- component pub struct Text { props: Props, - states: OwnStates, } impl Text { @@ -59,10 +46,7 @@ impl Text { /// /// Instantiate a new Text component pub fn new(props: Props) -> Self { - Text { - props, - states: OwnStates::default(), - } + Text { props } } } @@ -151,16 +135,12 @@ impl Component for Text { /// ### blur /// /// Blur component - fn blur(&mut self) { - self.states.focus = false; - } + fn blur(&mut self) {} /// ### active /// /// Active component - fn active(&mut self) { - self.states.focus = true; - } + fn active(&mut self) {} } #[cfg(test)] @@ -189,12 +169,6 @@ mod tests { )) .build(), ); - // Focus - assert_eq!(component.states.focus, false); - component.active(); - assert_eq!(component.states.focus, true); - component.blur(); - assert_eq!(component.states.focus, false); // Get value assert_eq!(component.get_value(), Payload::None); // Event diff --git a/src/ui/layout/view.rs b/src/ui/layout/view.rs index b72ff1e..ed8a3d9 100644 --- a/src/ui/layout/view.rs +++ b/src/ui/layout/view.rs @@ -99,10 +99,7 @@ impl View { /// /// Get component properties pub fn get_props(&self, id: &str) -> Option { - match self.components.get(id) { - None => None, - Some(cmp) => Some(cmp.get_props()), - } + self.components.get(id).map(|cmp| cmp.get_props()) } /// update @@ -110,10 +107,9 @@ impl View { /// Update component properties /// Returns `None` if component doesn't exist pub fn update(&mut self, id: &str, props: Props) -> Option<(String, Msg)> { - match self.components.get_mut(id) { - None => None, - Some(cmp) => Some((id.to_string(), cmp.update(props))), - } + self.components + .get_mut(id) + .map(|cmp| (id.to_string(), cmp.update(props))) } // -- state @@ -122,10 +118,7 @@ impl View { /// /// Get component value pub fn get_value(&self, id: &str) -> Option { - match self.components.get(id) { - None => None, - Some(cmp) => Some(cmp.get_value()), - } + self.components.get(id).map(|cmp| cmp.get_value()) } // -- events @@ -137,10 +130,10 @@ impl View { pub fn on(&mut self, ev: InputEvent) -> Option<(String, Msg)> { match self.focus.as_ref() { None => None, - Some(id) => match self.components.get_mut(id) { - None => None, - Some(cmp) => Some((id.to_string(), cmp.on(ev))), - }, + Some(id) => self + .components + .get_mut(id) + .map(|cmp| (id.to_string(), cmp.on(ev))), } } diff --git a/src/utils/parser.rs b/src/utils/parser.rs index f54ad56..54fa7bc 100644 --- a/src/utils/parser.rs +++ b/src/utils/parser.rs @@ -154,10 +154,7 @@ pub fn parse_remote_opt(remote: &str) -> Result { }; } // Get workdir - let wrkdir: Option = match groups.get(5) { - Some(group) => Some(PathBuf::from(group.as_str())), - None => None, - }; + let wrkdir: Option = groups.get(5).map(|group| PathBuf::from(group.as_str())); Ok(RemoteOptions { hostname, port, @@ -225,10 +222,7 @@ pub fn parse_datetime(tm: &str, fmt: &str) -> Result { /// Parse semver string pub fn parse_semver(haystack: &str) -> Option { match SEMVER_REGEX.captures(haystack) { - Some(groups) => match groups.get(1) { - Some(version) => Some(version.as_str().to_string()), - None => None, - }, + Some(groups) => groups.get(1).map(|version| version.as_str().to_string()), None => None, } }