diff --git a/src/controller/controller_main.rs b/src/controller/controller_main.rs index ccfc5b64..687f38d1 100644 --- a/src/controller/controller_main.rs +++ b/src/controller/controller_main.rs @@ -8,12 +8,9 @@ pub async fn run() { let command_line_args = env::args().collect(); let usecase = args_to_usecase(command_line_args); - match usecase.run().await { - Err(e) => { - print_error(&e); - std::process::exit(1); - } - Ok(_) => std::process::exit(0), + if let Err(e) = usecase.run().await { + print_error(&e); + std::process::exit(1); } } diff --git a/src/main.rs b/src/main.rs index a419007c..2fab4a2e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,9 +8,10 @@ mod usecase; #[tokio::main] async fn main() { - let result = controller::controller_main::run(); // ref: https://zenn.dev/techno_tanoc/articles/4c207397df3ab0#assertunwindsafe - let res = AssertUnwindSafe(result).catch_unwind().await; + let res = AssertUnwindSafe(controller::controller_main::run()) + .catch_unwind() + .await; if let Err(e) = res { println!("{}", err::any_to_string::any_to_string(&*e)); diff --git a/src/usecase/tui/app.rs b/src/usecase/tui/app.rs index 5647288b..e6135008 100644 --- a/src/usecase/tui/app.rs +++ b/src/usecase/tui/app.rs @@ -28,7 +28,6 @@ use std::{ env, io::{self, Stderr}, path::PathBuf, - process, sync::{Arc, Mutex}, }; use std::{panic::AssertUnwindSafe, time::Duration}; @@ -174,43 +173,34 @@ pub async fn main(config: config::Config) -> Result<()> { let mut terminal = Terminal::new(backend)?; let result = AssertUnwindSafe(async { - let mut model = match Model::new(config) { - Ok(m) => m, - Err(e) => { - shutdown_terminal(&mut terminal)?; - return Err(e); - } - }; - - let command = match run(&mut terminal, &mut model).await { - Ok(t) => t, - Err(e) => { - shutdown_terminal(&mut terminal)?; - return Err(e); - } - }; - - shutdown_terminal(&mut terminal)?; - - match command { - Some((runner, command)) => { - runner.show_command(&command); - let _ = runner.execute(&command); // TODO: handle error - Ok(()) + match Model::new(config) { + Ok(mut m) => { + match run(&mut terminal, &mut m).await { + // If async closure will be stabilized, use map instead of match + Ok(command) => match command { + Some((runner, command)) => Ok(Some((runner, command))), + None => Ok(None), // If no command selected, show nothing. + }, + Err(e) => Err(e), + } } - None => Ok(()), + Err(e) => Err(e), } }) .catch_unwind() .await; + shutdown_terminal(&mut terminal)?; + match result { - Ok(usecase_result) => usecase_result, - Err(e) => { - shutdown_terminal(&mut terminal)?; - println!("{}", any_to_string::any_to_string(&*e)); - process::exit(1); + // some kind of command was selected + Ok(Ok(Some((runner, command)))) => { + runner.show_command(&command); + runner.execute(&command) } + Ok(Ok(None)) => Ok(()), // no command was selected + Ok(Err(e)) => Err(e), // Model::new or run returned Err + Err(e) => Err(anyhow!(any_to_string::any_to_string(&*e))), // panic occurred } }