This commit is contained in:
veeso
2021-03-27 12:17:35 +01:00
parent 67e36fa38f
commit 55e884889c
16 changed files with 64 additions and 156 deletions
+8 -8
View File
@@ -166,10 +166,10 @@ impl FtpFileTransfer {
let mut abs_path: PathBuf = PathBuf::from(path); let mut abs_path: PathBuf = PathBuf::from(path);
abs_path.push(file_name.as_str()); abs_path.push(file_name.as_str());
// get extension // get extension
let extension: Option<String> = match abs_path.as_path().extension() { let extension: Option<String> = abs_path
None => None, .as_path()
Some(s) => Some(String::from(s.to_string_lossy())), .extension()
}; .map(|s| String::from(s.to_string_lossy()));
// Return // Return
// Push to entries // Push to entries
Ok(match is_dir { Ok(match is_dir {
@@ -254,10 +254,10 @@ impl FtpFileTransfer {
let mut abs_path: PathBuf = PathBuf::from(path); let mut abs_path: PathBuf = PathBuf::from(path);
abs_path.push(file_name.as_str()); abs_path.push(file_name.as_str());
// Get extension // Get extension
let extension: Option<String> = match abs_path.as_path().extension() { let extension: Option<String> = abs_path
None => None, .as_path()
Some(s) => Some(String::from(s.to_string_lossy())), .extension()
}; .map(|s| String::from(s.to_string_lossy()));
// Return entry // Return entry
Ok(match is_dir { Ok(match is_dir {
true => FsEntry::Directory(FsDirectory { true => FsEntry::Directory(FsDirectory {
+6 -12
View File
@@ -175,10 +175,10 @@ impl ScpFileTransfer {
let mut abs_path: PathBuf = PathBuf::from(path); let mut abs_path: PathBuf = PathBuf::from(path);
abs_path.push(file_name.as_str()); abs_path.push(file_name.as_str());
// Get extension // Get extension
let extension: Option<String> = match abs_path.as_path().extension() { let extension: Option<String> = abs_path
None => None, .as_path()
Some(s) => Some(String::from(s.to_string_lossy())), .extension()
}; .map(|s| String::from(s.to_string_lossy()));
// Return // Return
// Push to entries // Push to entries
Ok(match is_dir { Ok(match is_dir {
@@ -220,10 +220,7 @@ impl ScpFileTransfer {
fn get_name_and_link(&self, token: &str) -> (String, Option<PathBuf>) { fn get_name_and_link(&self, token: &str) -> (String, Option<PathBuf>) {
let tokens: Vec<&str> = token.split(" -> ").collect(); let tokens: Vec<&str> = token.split(" -> ").collect();
let filename: String = String::from(*tokens.get(0).unwrap()); let filename: String = String::from(*tokens.get(0).unwrap());
let symlink: Option<PathBuf> = match tokens.get(1) { let symlink: Option<PathBuf> = tokens.get(1).map(PathBuf::from);
Some(s) => Some(PathBuf::from(s)),
None => None,
};
(filename, symlink) (filename, symlink)
} }
@@ -382,10 +379,7 @@ impl FileTransfer for ScpFileTransfer {
} }
} }
// Get banner // Get banner
let banner: Option<String> = match session.banner() { let banner: Option<String> = session.banner().map(String::from);
Some(s) => Some(String::from(s)),
None => None,
};
// Set session // Set session
self.session = Some(session); self.session = Some(session);
// Get working directory // Get working directory
+11 -16
View File
@@ -123,20 +123,18 @@ impl SftpFileTransfer {
fn make_fsentry(&mut self, path: &Path, metadata: &FileStat) -> FsEntry { fn make_fsentry(&mut self, path: &Path, metadata: &FileStat) -> FsEntry {
// Get common parameters // Get common parameters
let file_name: String = String::from(path.file_name().unwrap().to_str().unwrap_or("")); let file_name: String = String::from(path.file_name().unwrap().to_str().unwrap_or(""));
let file_type: Option<String> = match path.extension() { let file_type: Option<String> = path
Some(ext) => Some(String::from(ext.to_str().unwrap_or(""))), .extension()
None => None, .map(|ext| String::from(ext.to_str().unwrap_or("")));
};
let uid: Option<u32> = metadata.uid; let uid: Option<u32> = metadata.uid;
let gid: Option<u32> = metadata.gid; let gid: Option<u32> = metadata.gid;
let pex: Option<(u8, u8, u8)> = match metadata.perm { let pex: Option<(u8, u8, u8)> = metadata.perm.map(|x| {
Some(perms) => Some(( (
((perms >> 6) & 0x7) as u8, ((x >> 6) & 0x7) as u8,
((perms >> 3) & 0x7) as u8, ((x >> 3) & 0x7) as u8,
(perms & 0x7) as u8, (x & 0x7) as u8,
)), )
None => None, });
};
let size: u64 = metadata.size.unwrap_or(0); let size: u64 = metadata.size.unwrap_or(0);
let mut atime: SystemTime = SystemTime::UNIX_EPOCH; let mut atime: SystemTime = SystemTime::UNIX_EPOCH;
atime = atime atime = atime
@@ -365,10 +363,7 @@ impl FileTransfer for SftpFileTransfer {
} }
}; };
// Set session // Set session
let banner: Option<String> = match session.banner() { let banner: Option<String> = session.banner().map(String::from);
Some(s) => Some(String::from(s)),
None => None,
};
self.session = Some(session); self.session = Some(session);
// Set sftp // Set sftp
self.sftp = Some(sftp); self.sftp = Some(sftp);
+4 -4
View File
@@ -510,10 +510,10 @@ impl Formatter {
None => None, None => None,
}; };
// Match format extra: group 2 + 1 // Match format extra: group 2 + 1
let fmt_extra: Option<String> = match &regex_match.get(5) { let fmt_extra: Option<String> = regex_match
Some(extra) => Some(extra.as_str().to_string()), .get(5)
None => None, .as_ref()
}; .map(|extra| extra.as_str().to_string());
// Create a callchain or push new element to its back // Create a callchain or push new element to its back
match callchain.as_mut() { match callchain.as_mut() {
None => { None => {
+1 -4
View File
@@ -190,10 +190,7 @@ impl FileExplorer {
pass pass
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
match filtered.get(idx) { filtered.get(idx).copied()
None => None,
Some(file) => Some(file),
}
} }
// Formatting // Formatting
+8 -11
View File
@@ -353,10 +353,9 @@ impl Localhost {
}), }),
false => { false => {
// Is File // Is File
let extension: Option<String> = match path.extension() { let extension: Option<String> = path
Some(s) => Some(String::from(s.to_str().unwrap_or(""))), .extension()
None => None, .map(|s| String::from(s.to_str().unwrap_or("")));
};
FsEntry::File(FsFile { FsEntry::File(FsFile {
name: file_name, name: file_name,
abs_path: path.clone(), abs_path: path.clone(),
@@ -534,13 +533,11 @@ impl Localhost {
Err(err) => return Err(HostError::new(HostErrorType::DirNotAccessible, Some(err))), Err(err) => return Err(HostError::new(HostErrorType::DirNotAccessible, Some(err))),
}; };
let mut fs_entries: Vec<FsEntry> = Vec::new(); let mut fs_entries: Vec<FsEntry> = Vec::new();
for entry in entries { for entry in entries.flatten() {
if let Ok(entry) = entry { fs_entries.push(match self.stat(entry.path().as_path()) {
fs_entries.push(match self.stat(entry.path().as_path()) { Ok(entry) => entry,
Ok(entry) => entry, Err(err) => return Err(err),
Err(err) => return Err(err), });
});
}
} }
Ok(fs_entries) Ok(fs_entries)
} }
+1 -4
View File
@@ -342,10 +342,7 @@ impl BookmarksClient {
port, port,
username, username,
protocol: protocol.to_string(), protocol: protocol.to_string(),
password: match password { password: password.map(|p| self.encrypt_str(p.as_str())),
Some(p) => Some(self.encrypt_str(p.as_str())), // Encrypt password if provided
None => None,
},
} }
} }
+1 -4
View File
@@ -173,10 +173,7 @@ impl ConfigClient {
/// Set value for group_dir in configuration. /// Set value for group_dir in configuration.
/// Provided value, if `Some` will be converted to `GroupDirs` /// Provided value, if `Some` will be converted to `GroupDirs`
pub fn set_group_dirs(&mut self, val: Option<GroupDirs>) { pub fn set_group_dirs(&mut self, val: Option<GroupDirs>) {
self.config.user_interface.group_dirs = match val { self.config.user_interface.group_dirs = val.map(|val| val.to_string());
None => None,
Some(val) => Some(val.to_string()),
};
} }
/// ### get_file_fmt /// ### get_file_fmt
+1 -4
View File
@@ -45,10 +45,7 @@ impl AuthActivity {
/// Update auth activity model based on msg /// Update auth activity model based on msg
/// The function exits when returns None /// The function exits when returns None
pub(super) fn update(&mut self, msg: Option<(String, Msg)>) -> Option<(String, Msg)> { pub(super) fn update(&mut self, msg: Option<(String, Msg)>) -> Option<(String, Msg)> {
let ref_msg: Option<(&str, &Msg)> = match msg.as_ref() { let ref_msg: Option<(&str, &Msg)> = msg.as_ref().map(|(s, msg)| (s.as_str(), msg));
None => None,
Some((s, msg)) => Some((s, msg)),
};
// Match msg // Match msg
match ref_msg { match ref_msg {
None => None, // Exit after None None => None, // Exit after None
@@ -185,10 +185,7 @@ impl FileTransferActivity {
} }
pub(super) fn action_local_rename(&mut self, input: String) { pub(super) fn action_local_rename(&mut self, input: String) {
let entry: Option<FsEntry> = match self.get_local_file_entry() { let entry: Option<FsEntry> = self.get_local_file_entry().cloned();
Some(f) => Some(f.clone()),
None => None,
};
if let Some(entry) = entry { if let Some(entry) = entry {
let mut dst_path: PathBuf = PathBuf::from(input); let mut dst_path: PathBuf = PathBuf::from(input);
// Check if path is relative // Check if path is relative
@@ -265,10 +262,7 @@ impl FileTransferActivity {
} }
pub(super) fn action_local_delete(&mut self) { pub(super) fn action_local_delete(&mut self) {
let entry: Option<FsEntry> = match self.get_local_file_entry() { let entry: Option<FsEntry> = self.get_local_file_entry().cloned();
Some(f) => Some(f.clone()),
None => None,
};
if let Some(entry) = entry { if let Some(entry) = entry {
let full_path: PathBuf = entry.get_abs_path(); let full_path: PathBuf = entry.get_abs_path();
// Delete file or directory and report status as popup // 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<String>) { pub(super) fn action_find_transfer(&mut self, idx: usize, name: Option<String>) {
let entry: Option<FsEntry> = match self.found.as_ref().unwrap().get(idx) { let entry: Option<FsEntry> = self.found.as_ref().unwrap().get(idx).cloned();
None => None,
Some(e) => Some(e.clone()),
};
if let Some(entry) = entry { if let Some(entry) = entry {
// Download file // Download file
match self.tab { match self.tab {
@@ -548,10 +539,7 @@ impl FileTransferActivity {
} }
pub(super) fn action_find_delete(&mut self, idx: usize) { pub(super) fn action_find_delete(&mut self, idx: usize) {
let entry: Option<FsEntry> = match self.found.as_ref().unwrap().get(idx) { let entry: Option<FsEntry> = self.found.as_ref().unwrap().get(idx).cloned();
None => None,
Some(e) => Some(e.clone()),
};
if let Some(entry) = entry { if let Some(entry) = entry {
// Download file // Download file
match self.tab { match self.tab {
@@ -57,10 +57,7 @@ impl FileTransferActivity {
/// Update auth activity model based on msg /// Update auth activity model based on msg
/// The function exits when returns None /// The function exits when returns None
pub(super) fn update(&mut self, msg: Option<(String, Msg)>) -> Option<(String, Msg)> { pub(super) fn update(&mut self, msg: Option<(String, Msg)>) -> Option<(String, Msg)> {
let ref_msg: Option<(&str, &Msg)> = match msg.as_ref() { let ref_msg: Option<(&str, &Msg)> = msg.as_ref().map(|(s, msg)| (s.as_str(), msg));
None => None,
Some((s, msg)) => Some((s, msg)),
};
// Match msg // Match msg
match ref_msg { match ref_msg {
None => None, // Exit after None None => None, // Exit after None
@@ -132,10 +129,7 @@ impl FileTransferActivity {
self.update_local_filelist() self.update_local_filelist()
} }
(COMPONENT_EXPLORER_LOCAL, &MSG_KEY_CHAR_I) => { (COMPONENT_EXPLORER_LOCAL, &MSG_KEY_CHAR_I) => {
let file: Option<FsEntry> = match self.get_local_file_entry() { let file: Option<FsEntry> = self.get_local_file_entry().cloned();
Some(f) => Some(f.clone()),
None => None,
};
if let Some(file) = file { if let Some(file) = file {
self.mount_file_info(&file); self.mount_file_info(&file);
} }
@@ -251,10 +245,7 @@ impl FileTransferActivity {
self.update_remote_filelist() self.update_remote_filelist()
} }
(COMPONENT_EXPLORER_REMOTE, &MSG_KEY_CHAR_I) => { (COMPONENT_EXPLORER_REMOTE, &MSG_KEY_CHAR_I) => {
let file: Option<FsEntry> = match self.get_remote_file_entry() { let file: Option<FsEntry> = self.get_remote_file_entry().cloned();
Some(f) => Some(f.clone()),
None => None,
};
if let Some(file) = file { if let Some(file) = file {
self.mount_file_info(&file); self.mount_file_info(&file);
} }
+1 -4
View File
@@ -68,10 +68,7 @@ impl SetupActivity {
_ => None, _ => None,
}; };
if let Some(idx) = idx { if let Some(idx) = idx {
let key: Option<String> = match config_cli.iter_ssh_keys().nth(idx) { let key: Option<String> = config_cli.iter_ssh_keys().nth(idx).cloned();
Some(k) => Some(k.clone()),
None => None,
};
if let Some(key) = key { if let Some(key) = key {
match config_cli.get_ssh_key(&key) { match config_cli.get_ssh_key(&key) {
Ok(opt) => { Ok(opt) => {
+1 -4
View File
@@ -43,10 +43,7 @@ impl SetupActivity {
/// Update auth activity model based on msg /// Update auth activity model based on msg
/// The function exits when returns None /// The function exits when returns None
pub(super) fn update(&mut self, msg: Option<(String, Msg)>) -> Option<(String, Msg)> { pub(super) fn update(&mut self, msg: Option<(String, Msg)>) -> Option<(String, Msg)> {
let ref_msg: Option<(&str, &Msg)> = match msg.as_ref() { let ref_msg: Option<(&str, &Msg)> = msg.as_ref().map(|(s, msg)| (s.as_str(), msg));
None => None,
Some((s, msg)) => Some((s, msg)),
};
// Match msg // Match msg
match ref_msg { match ref_msg {
None => None, None => None,
+3 -29
View File
@@ -35,23 +35,10 @@ use tui::{
widgets::Paragraph, widgets::Paragraph,
}; };
// -- state
struct OwnStates {
focus: bool,
}
impl Default for OwnStates {
fn default() -> Self {
OwnStates { focus: false }
}
}
// -- component // -- component
pub struct Text { pub struct Text {
props: Props, props: Props,
states: OwnStates,
} }
impl Text { impl Text {
@@ -59,10 +46,7 @@ impl Text {
/// ///
/// Instantiate a new Text component /// Instantiate a new Text component
pub fn new(props: Props) -> Self { pub fn new(props: Props) -> Self {
Text { Text { props }
props,
states: OwnStates::default(),
}
} }
} }
@@ -151,16 +135,12 @@ impl Component for Text {
/// ### blur /// ### blur
/// ///
/// Blur component /// Blur component
fn blur(&mut self) { fn blur(&mut self) {}
self.states.focus = false;
}
/// ### active /// ### active
/// ///
/// Active component /// Active component
fn active(&mut self) { fn active(&mut self) {}
self.states.focus = true;
}
} }
#[cfg(test)] #[cfg(test)]
@@ -189,12 +169,6 @@ mod tests {
)) ))
.build(), .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 // Get value
assert_eq!(component.get_value(), Payload::None); assert_eq!(component.get_value(), Payload::None);
// Event // Event
+9 -16
View File
@@ -99,10 +99,7 @@ impl View {
/// ///
/// Get component properties /// Get component properties
pub fn get_props(&self, id: &str) -> Option<PropsBuilder> { pub fn get_props(&self, id: &str) -> Option<PropsBuilder> {
match self.components.get(id) { self.components.get(id).map(|cmp| cmp.get_props())
None => None,
Some(cmp) => Some(cmp.get_props()),
}
} }
/// update /// update
@@ -110,10 +107,9 @@ impl View {
/// Update component properties /// Update component properties
/// Returns `None` if component doesn't exist /// Returns `None` if component doesn't exist
pub fn update(&mut self, id: &str, props: Props) -> Option<(String, Msg)> { pub fn update(&mut self, id: &str, props: Props) -> Option<(String, Msg)> {
match self.components.get_mut(id) { self.components
None => None, .get_mut(id)
Some(cmp) => Some((id.to_string(), cmp.update(props))), .map(|cmp| (id.to_string(), cmp.update(props)))
}
} }
// -- state // -- state
@@ -122,10 +118,7 @@ impl View {
/// ///
/// Get component value /// Get component value
pub fn get_value(&self, id: &str) -> Option<Payload> { pub fn get_value(&self, id: &str) -> Option<Payload> {
match self.components.get(id) { self.components.get(id).map(|cmp| cmp.get_value())
None => None,
Some(cmp) => Some(cmp.get_value()),
}
} }
// -- events // -- events
@@ -137,10 +130,10 @@ impl View {
pub fn on(&mut self, ev: InputEvent) -> Option<(String, Msg)> { pub fn on(&mut self, ev: InputEvent) -> Option<(String, Msg)> {
match self.focus.as_ref() { match self.focus.as_ref() {
None => None, None => None,
Some(id) => match self.components.get_mut(id) { Some(id) => self
None => None, .components
Some(cmp) => Some((id.to_string(), cmp.on(ev))), .get_mut(id)
}, .map(|cmp| (id.to_string(), cmp.on(ev))),
} }
} }
+2 -8
View File
@@ -154,10 +154,7 @@ pub fn parse_remote_opt(remote: &str) -> Result<RemoteOptions, String> {
}; };
} }
// Get workdir // Get workdir
let wrkdir: Option<PathBuf> = match groups.get(5) { let wrkdir: Option<PathBuf> = groups.get(5).map(|group| PathBuf::from(group.as_str()));
Some(group) => Some(PathBuf::from(group.as_str())),
None => None,
};
Ok(RemoteOptions { Ok(RemoteOptions {
hostname, hostname,
port, port,
@@ -225,10 +222,7 @@ pub fn parse_datetime(tm: &str, fmt: &str) -> Result<SystemTime, ParseError> {
/// Parse semver string /// Parse semver string
pub fn parse_semver(haystack: &str) -> Option<String> { pub fn parse_semver(haystack: &str) -> Option<String> {
match SEMVER_REGEX.captures(haystack) { match SEMVER_REGEX.captures(haystack) {
Some(groups) => match groups.get(1) { Some(groups) => groups.get(1).map(|version| version.as_str().to_string()),
Some(version) => Some(version.as_str().to_string()),
None => None,
},
None => None, None => None,
} }
} }