From 8449ae00d612811c1ff6cbd3b5702edd0b8dcf9c Mon Sep 17 00:00:00 2001 From: Valerie Date: Wed, 24 Jul 2024 20:59:33 -0400 Subject: [PATCH] error functions now return a never type --- src/command/session.rs | 2 +- src/error.rs | 22 +++++++++++----------- src/state.rs | 5 +---- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/command/session.rs b/src/command/session.rs index 2f9cbe7..1ab8d16 100644 --- a/src/command/session.rs +++ b/src/command/session.rs @@ -36,7 +36,7 @@ pub fn switch(state: &mut State) { let target: String = match if let Some(inner) = args.get(0) { inner.to_str() } else { None } { None | Some("-") => if let Some(prev) = message(MSG_PREVIOUS) { prev } - else { error::missing_target(); panic!() }, + else { error::missing_target() }, Some(inner) => inner.to_owned() }; diff --git a/src/error.rs b/src/error.rs index f8c72a5..11b6f0d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,14 +1,14 @@ use std::process::exit; /// no subcommand that matches user input; code 1 -pub fn no_subcommand(subcommand: String) { +pub fn no_subcommand(subcommand: String) -> ! { eprintln!("remux: no command match for \"{subcommand}\""); exit(1); } /// target session not found; code 2 -pub fn no_target>(target: S) { +pub fn no_target>(target: S) -> ! { let target = target.into(); eprintln!("remux: no session \"{target}\" exists"); exit(2); @@ -16,26 +16,26 @@ pub fn no_target>(target: S) { /// help topic doesn't exist; code 3 -pub fn no_help(topic: String) { +pub fn no_help(topic: String) -> ! { eprintln!("remux: no help for \"{topic}\""); exit(3); } /// user provided no target; code 4 -pub fn missing_target() { +pub fn missing_target() -> ! { eprintln!("remux: no target provided"); exit(4); } /// refuse to attach to current session; code 4 -pub fn same_session() { +pub fn same_session() -> ! { eprintln!("remux: cannot attach to same session"); exit(4); } /// a session with the target name already exists; code 4 -pub fn target_exists>(target: S) { +pub fn target_exists>(target: S) -> ! { let target = target.into(); eprintln!("remux: session \"{target}\" already exists"); exit(4); @@ -43,26 +43,26 @@ pub fn target_exists>(target: S) { /// non-terminal environment prevention; code 5 -pub fn not_terminal() { +pub fn not_terminal() -> ! { eprintln!("remux: not running from a terminal"); exit(5); } /// tried to nest while not in a session; code 6 -pub fn not_nesting() { +pub fn not_nesting() -> ! { eprintln!("remux: inappropriate nesting flag (-n); not in a session"); exit(6); } /// operation requires nesting flag; code 6 -pub fn prevent_nest() { +pub fn prevent_nest() -> ! { eprintln!("remux: the nesting flag (-n) is required for nesting operation"); exit(6); } /// operation conflicts with nesting flag; code 6 -pub fn conflict_nest(reason: Option<&'static str>) { +pub fn conflict_nest(reason: Option<&'static str>) -> ! { if let Some(reason) = reason { eprintln!("remux: inappropriate nesting flag (-n): {reason}"); } else { eprintln!("remux: nesting flag (-n) is inappropriate for this operation."); } exit(6); @@ -70,7 +70,7 @@ pub fn conflict_nest(reason: Option<&'static str>) { /// tried to run a session command outside a session; code 7 -pub fn not_in_session(cmd: &'static str) { +pub fn not_in_session(cmd: &'static str) -> ! { eprintln!("remux: '{cmd}' must be run from within a session"); exit(7); } diff --git a/src/state.rs b/src/state.rs index 10ccbd5..93af5f4 100644 --- a/src/state.rs +++ b/src/state.rs @@ -67,10 +67,7 @@ impl State<'_> { let from_args = self.target(); if from_args.is_some() { return from_args; } else if let Some(repository) = &self.repository { Some(repository.name.clone()) } - else { - error::missing_target(); - None - } + else { error::missing_target() } } }