jam_tooling/
run_main.rs

1use crate::log;
2use std::{fmt::Display, future::Future, process::ExitCode};
3use tokio::{select, signal::ctrl_c};
4
5/// This is inner main function wrapper.
6///
7/// It sets up logging, runs the provided `main` future or returns on Ctrl-C.
8#[inline]
9pub async fn run_main<E: Display>(main: impl Future<Output = Result<(), E>>) -> ExitCode {
10	log::setup_log(&log::Config::default());
11	select! {
12		_ = ctrl_c() => {
13			eprintln!("Ctrl-C received");
14			ExitCode::FAILURE
15		}
16		result = main => match result {
17			Ok(()) => ExitCode::SUCCESS,
18			Err(e) => {
19				eprintln!("{e}");
20				ExitCode::FAILURE
21			},
22		}
23	}
24}