summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKen Matsui <26405363+ken-matsui@users.noreply.github.com>2021-11-09 17:57:59 +0900
committerGitHub <noreply@github.com>2021-11-09 09:57:59 +0100
commitaed2a7def60f0a5f57fd7f379711a2aafda551bf (patch)
treec4a2a77a9bcbced5b215ba5f32916ef99d591715 /src
parentf9879bdc04a67d0dc14d349f0799d3d681b757a0 (diff)
fix(match): Make match arms much simpler (#844)
Diffstat (limited to 'src')
-rw-r--r--src/commands.rs91
-rw-r--r--src/sessions.rs85
2 files changed, 65 insertions, 111 deletions
diff --git a/src/commands.rs b/src/commands.rs
index 0879e5361..da97cee87 100644
--- a/src/commands.rs
+++ b/src/commands.rs
@@ -23,27 +23,26 @@ pub(crate) use crate::sessions::list_sessions;
pub(crate) fn kill_all_sessions(yes: bool) {
match get_sessions() {
+ Ok(sessions) if sessions.is_empty() => {
+ println!("No active zellij sessions found.");
+ process::exit(1);
+ }
Ok(sessions) => {
- if sessions.is_empty() {
- println!("No active zellij sessions found.");
- process::exit(1);
- } else {
- if !yes {
- println!("WARNING: this action will kill all sessions.");
- if !Confirm::new()
- .with_prompt("Do you want to continue?")
- .interact()
- .unwrap()
- {
- println!("Abort.");
- process::exit(1);
- }
- }
- for session in sessions.iter() {
- kill_session_impl(session);
+ if !yes {
+ println!("WARNING: this action will kill all sessions.");
+ if !Confirm::new()
+ .with_prompt("Do you want to continue?")
+ .interact()
+ .unwrap()
+ {
+ println!("Abort.");
+ process::exit(1);
}
- process::exit(0);
}
+ for session in sessions.iter() {
+ kill_session_impl(session);
+ }
+ process::exit(0);
}
Err(e) => {
eprintln!("Error occurred: {:?}", e);
@@ -95,17 +94,14 @@ fn find_indexed_session(
) -> ClientInfo {
match sessions.get(index) {
Some(session) => ClientInfo::Attach(session.clone(), config_options),
+ None if create => create_new_client(),
None => {
- if create {
- create_new_client()
- } else {
- println!(
- "No session indexed by {} found. The following sessions are active:",
- index
- );
- print_sessions_with_index(sessions);
- process::exit(1);
- }
+ println!(
+ "No session indexed by {} found. The following sessions are active:",
+ index
+ );
+ print_sessions_with_index(sessions);
+ process::exit(1);
}
}
}
@@ -113,18 +109,15 @@ fn find_indexed_session(
fn attach_with_session_index(config_options: Options, index: usize, create: bool) -> ClientInfo {
// Ignore the session_name when `--index` is provided
match get_sessions_sorted_by_creation_date() {
- Ok(sessions) => {
- if sessions.is_empty() {
- if create {
- create_new_client()
- } else {
- println!("No active zellij sessions found.");
- process::exit(1);
- }
+ Ok(sessions) if sessions.is_empty() => {
+ if create {
+ create_new_client()
} else {
- find_indexed_session(sessions, config_options, index, create)
+ println!("No active zellij sessions found.");
+ process::exit(1);
}
}
+ Ok(sessions) => find_indexed_session(sessions, config_options, index, create),
Err(e) => {
eprintln!("Error occurred: {:?}", e);
process::exit(1);
@@ -138,26 +131,22 @@ fn attach_with_session_name(
create: bool,
) -> ClientInfo {
match session_name.as_ref() {
- Some(session) => {
- if create {
- if !session_exists(session).unwrap() {
- ClientInfo::New(session_name.unwrap())
- } else {
- ClientInfo::Attach(session_name.unwrap(), config_options)
- }
+ Some(session) if create => {
+ if !session_exists(session).unwrap() {
+ ClientInfo::New(session_name.unwrap())
} else {
- assert_session(session);
ClientInfo::Attach(session_name.unwrap(), config_options)
}
}
+ Some(session) => {
+ assert_session(session);
+ ClientInfo::Attach(session_name.unwrap(), config_options)
+ }
None => match get_active_session() {
+ ActiveSession::None if create => create_new_client(),
ActiveSession::None => {
- if create {
- create_new_client()
- } else {
- println!("No active zellij sessions found.");
- process::exit(1);
- }
+ println!("No active zellij sessions found.");
+ process::exit(1);
}
ActiveSession::One(session_name) => ClientInfo::Attach(session_name, config_options),
ActiveSession::Many => {
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);