diff options
Diffstat (limited to 'src/sessions.rs')
-rw-r--r-- | src/sessions.rs | 85 |
1 files changed, 25 insertions, 60 deletions
diff --git a/src/sessions.rs b/src/sessions.rs index bf44d0885..31c4df4ef 100644 --- a/src/sessions.rs +++ b/src/sessions.rs @@ -20,13 +20,8 @@ pub(crate) fn get_sessions() -> Result<Vec<String>, io::ErrorKind> { }); Ok(sessions) } - Err(err) => { - if let io::ErrorKind::NotFound = err.kind() { - Ok(Vec::with_capacity(0)) - } else { - Err(err.kind()) - } - } + Err(err) if io::ErrorKind::NotFound != err.kind() => Err(err.kind()), + Err(_) => Ok(Vec::with_capacity(0)), } } @@ -50,13 +45,8 @@ pub(crate) fn get_sessions_sorted_by_creation_date() -> anyhow::Result<Vec<Strin .collect(); Ok(sessions) } - Err(err) => { - if let io::ErrorKind::NotFound = err.kind() { - Ok(Vec::with_capacity(0)) - } else { - Err(err.into()) - } - } + Err(err) if io::ErrorKind::NotFound != err.kind() => Err(err.into()), + Err(_) => Ok(Vec::with_capacity(0)), } } @@ -67,14 +57,11 @@ fn assert_socket(name: &str) -> bool { IpcSenderWithContext::new(stream).send(ClientToServerMsg::ClientExited); true } - Err(e) => { - if e.kind() == io::ErrorKind::ConnectionRefused { - drop(fs::remove_file(path)); - false - } else { - true - } + Err(e) if e.kind() == io::ErrorKind::ConnectionRefused => { + drop(fs::remove_file(path)); + false } + Err(_) => true, } } @@ -110,16 +97,9 @@ pub(crate) enum ActiveSession { pub(crate) fn get_active_session() -> ActiveSession { match get_sessions() { - Ok(mut sessions) => { - if sessions.len() == 1 { - return ActiveSession::One(sessions.pop().unwrap()); - } - if sessions.is_empty() { - ActiveSession::None - } else { - ActiveSession::Many - } - } + Ok(sessions) if sessions.is_empty() => ActiveSession::None, + Ok(mut sessions) if sessions.len() == 1 => ActiveSession::One(sessions.pop().unwrap()), + Ok(_) => ActiveSession::Many, Err(e) => { eprintln!("Error occurred: {:?}", e); process::exit(1); @@ -142,12 +122,12 @@ pub(crate) fn kill_session(name: &str) { pub(crate) fn list_sessions() { let exit_code = match get_sessions() { - Ok(sessions) => { - if sessions.is_empty() { - println!("No active zellij sessions found."); - } else { - print_sessions(sessions); - } + Ok(sessions) if !sessions.is_empty() => { + print_sessions(sessions); + 0 + } + Ok(_) => { + println!("No active zellij sessions found."); 0 } Err(e) => { @@ -160,40 +140,25 @@ pub(crate) fn list_sessions() { pub(crate) fn session_exists(name: &str) -> Result<bool, io::ErrorKind> { return match get_sessions() { - Ok(sessions) => { - if sessions.iter().any(|s| s == name) { - return Ok(true); - } - Ok(false) - } + Ok(sessions) if sessions.iter().any(|s| s == name) => Ok(true), + Ok(_) => Ok(false), Err(e) => Err(e), }; } pub(crate) fn assert_session(name: &str) { match session_exists(name) { - Ok(result) => { - if result { - return; - } else { - println!("No session named {:?} found.", name); - } - } - Err(e) => { - eprintln!("Error occurred: {:?}", e); - } + Ok(result) if result => return, + Ok(_) => println!("No session named {:?} found.", name), + Err(e) => eprintln!("Error occurred: {:?}", e), }; process::exit(1); } pub(crate) fn assert_session_ne(name: &str) { - match get_sessions() { - Ok(sessions) => { - if sessions.iter().all(|s| s != name) { - return; - } - println!("Session with name {:?} aleady exists. Use attach command to connect to it or specify a different name.", name); - } + match session_exists(name) { + Ok(result) if !result => return, + Ok(_) => println!("Session with name {:?} already exists. Use attach command to connect to it or specify a different name.", name), Err(e) => eprintln!("Error occurred: {:?}", e), }; process::exit(1); |