fix(match): Make match arms much simpler (#844)

This commit is contained in:
Ken Matsui 2021-11-09 17:57:59 +09:00 committed by GitHub
parent f9879bdc04
commit aed2a7def6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 111 deletions

View file

@ -23,27 +23,26 @@ pub(crate) use crate::sessions::list_sessions;
pub(crate) fn kill_all_sessions(yes: bool) { pub(crate) fn kill_all_sessions(yes: bool) {
match get_sessions() { match get_sessions() {
Ok(sessions) if sessions.is_empty() => {
println!("No active zellij sessions found.");
process::exit(1);
}
Ok(sessions) => { Ok(sessions) => {
if sessions.is_empty() { if !yes {
println!("No active zellij sessions found."); println!("WARNING: this action will kill all sessions.");
process::exit(1); if !Confirm::new()
} else { .with_prompt("Do you want to continue?")
if !yes { .interact()
println!("WARNING: this action will kill all sessions."); .unwrap()
if !Confirm::new() {
.with_prompt("Do you want to continue?") println!("Abort.");
.interact() process::exit(1);
.unwrap()
{
println!("Abort.");
process::exit(1);
}
} }
for session in sessions.iter() {
kill_session_impl(session);
}
process::exit(0);
} }
for session in sessions.iter() {
kill_session_impl(session);
}
process::exit(0);
} }
Err(e) => { Err(e) => {
eprintln!("Error occurred: {:?}", e); eprintln!("Error occurred: {:?}", e);
@ -95,17 +94,14 @@ fn find_indexed_session(
) -> ClientInfo { ) -> ClientInfo {
match sessions.get(index) { match sessions.get(index) {
Some(session) => ClientInfo::Attach(session.clone(), config_options), Some(session) => ClientInfo::Attach(session.clone(), config_options),
None if create => create_new_client(),
None => { None => {
if create { println!(
create_new_client() "No session indexed by {} found. The following sessions are active:",
} else { index
println!( );
"No session indexed by {} found. The following sessions are active:", print_sessions_with_index(sessions);
index process::exit(1);
);
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 { fn attach_with_session_index(config_options: Options, index: usize, create: bool) -> ClientInfo {
// Ignore the session_name when `--index` is provided // Ignore the session_name when `--index` is provided
match get_sessions_sorted_by_creation_date() { match get_sessions_sorted_by_creation_date() {
Ok(sessions) => { Ok(sessions) if sessions.is_empty() => {
if sessions.is_empty() { if create {
if create { create_new_client()
create_new_client()
} else {
println!("No active zellij sessions found.");
process::exit(1);
}
} else { } 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) => { Err(e) => {
eprintln!("Error occurred: {:?}", e); eprintln!("Error occurred: {:?}", e);
process::exit(1); process::exit(1);
@ -138,26 +131,22 @@ fn attach_with_session_name(
create: bool, create: bool,
) -> ClientInfo { ) -> ClientInfo {
match session_name.as_ref() { match session_name.as_ref() {
Some(session) => { Some(session) if create => {
if create { if !session_exists(session).unwrap() {
if !session_exists(session).unwrap() { ClientInfo::New(session_name.unwrap())
ClientInfo::New(session_name.unwrap())
} else {
ClientInfo::Attach(session_name.unwrap(), config_options)
}
} else { } else {
assert_session(session);
ClientInfo::Attach(session_name.unwrap(), config_options) ClientInfo::Attach(session_name.unwrap(), config_options)
} }
} }
Some(session) => {
assert_session(session);
ClientInfo::Attach(session_name.unwrap(), config_options)
}
None => match get_active_session() { None => match get_active_session() {
ActiveSession::None if create => create_new_client(),
ActiveSession::None => { ActiveSession::None => {
if create { println!("No active zellij sessions found.");
create_new_client() process::exit(1);
} else {
println!("No active zellij sessions found.");
process::exit(1);
}
} }
ActiveSession::One(session_name) => ClientInfo::Attach(session_name, config_options), ActiveSession::One(session_name) => ClientInfo::Attach(session_name, config_options),
ActiveSession::Many => { ActiveSession::Many => {

View file

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