fix(match): Make match arms much simpler (#844)
This commit is contained in:
parent
f9879bdc04
commit
aed2a7def6
2 changed files with 65 additions and 111 deletions
|
|
@ -23,11 +23,11 @@ pub(crate) use crate::sessions::list_sessions;
|
|||
|
||||
pub(crate) fn kill_all_sessions(yes: bool) {
|
||||
match get_sessions() {
|
||||
Ok(sessions) => {
|
||||
if sessions.is_empty() {
|
||||
Ok(sessions) if sessions.is_empty() => {
|
||||
println!("No active zellij sessions found.");
|
||||
process::exit(1);
|
||||
} else {
|
||||
}
|
||||
Ok(sessions) => {
|
||||
if !yes {
|
||||
println!("WARNING: this action will kill all sessions.");
|
||||
if !Confirm::new()
|
||||
|
|
@ -44,7 +44,6 @@ pub(crate) fn kill_all_sessions(yes: bool) {
|
|||
}
|
||||
process::exit(0);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error occurred: {:?}", e);
|
||||
process::exit(1);
|
||||
|
|
@ -95,10 +94,8 @@ 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
|
||||
|
|
@ -108,23 +105,19 @@ 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() {
|
||||
Ok(sessions) if sessions.is_empty() => {
|
||||
if create {
|
||||
create_new_client()
|
||||
} else {
|
||||
println!("No active zellij sessions found.");
|
||||
process::exit(1);
|
||||
}
|
||||
} else {
|
||||
find_indexed_session(sessions, config_options, index, create)
|
||||
}
|
||||
}
|
||||
Ok(sessions) => find_indexed_session(sessions, config_options, index, create),
|
||||
Err(e) => {
|
||||
eprintln!("Error occurred: {:?}", e);
|
||||
process::exit(1);
|
||||
|
|
@ -138,27 +131,23 @@ fn attach_with_session_name(
|
|||
create: bool,
|
||||
) -> ClientInfo {
|
||||
match session_name.as_ref() {
|
||||
Some(session) => {
|
||||
if create {
|
||||
Some(session) if create => {
|
||||
if !session_exists(session).unwrap() {
|
||||
ClientInfo::New(session_name.unwrap())
|
||||
} else {
|
||||
ClientInfo::Attach(session_name.unwrap(), config_options)
|
||||
}
|
||||
} else {
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
ActiveSession::One(session_name) => ClientInfo::Attach(session_name, config_options),
|
||||
ActiveSession::Many => {
|
||||
println!("Please specify the session name to attach to. The following sessions are active:");
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
Err(e) if e.kind() == io::ErrorKind::ConnectionRefused => {
|
||||
drop(fs::remove_file(path));
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}
|
||||
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 {
|
||||
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);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue