main.rs (2057B)
1 #![forbid(unsafe_code)] 2 3 use std::process::ExitCode; 4 5 use rhi::{RhiLogRecord, RhiProcessResult}; 6 7 struct RhiOsSignalSource { 8 #[cfg(unix)] 9 interrupt: tokio::signal::unix::Signal, 10 #[cfg(unix)] 11 terminate: tokio::signal::unix::Signal, 12 } 13 14 impl RhiOsSignalSource { 15 fn new() -> Option<Self> { 16 #[cfg(unix)] 17 { 18 let interrupt = 19 tokio::signal::unix::signal(tokio::signal::unix::SignalKind::interrupt()).ok()?; 20 let terminate = 21 tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()).ok()?; 22 Some(Self { 23 interrupt, 24 terminate, 25 }) 26 } 27 #[cfg(not(unix))] 28 { 29 Some(Self {}) 30 } 31 } 32 } 33 34 impl rhi::RhiProcessSignalSource for RhiOsSignalSource { 35 fn next_signal(&mut self) -> rhi::RhiProcessSignalFuture<'_> { 36 #[cfg(unix)] 37 { 38 Box::pin(async move { 39 tokio::select! { 40 observed = self.interrupt.recv() => observed.map(|()| rhi::RhiProcessSignal::Interrupt), 41 observed = self.terminate.recv() => observed.map(|()| rhi::RhiProcessSignal::Terminate), 42 } 43 }) 44 } 45 #[cfg(not(unix))] 46 { 47 Box::pin(async move { 48 tokio::signal::ctrl_c() 49 .await 50 .ok() 51 .map(|()| rhi::RhiProcessSignal::Interrupt) 52 }) 53 } 54 } 55 } 56 57 fn main() -> ExitCode { 58 match rhi::parse_rhi_cli_v1_from(std::env::args_os()) { 59 Ok(invocation) => { 60 let result = 61 rhi::execute_rhi_cli_v1_with_signal_source(invocation, RhiOsSignalSource::new); 62 eprintln!("{}", RhiLogRecord::process_result(result)); 63 result.exit_code() 64 } 65 Err(_) => { 66 let result = RhiProcessResult::InputOrConfiguration; 67 eprintln!("{}", RhiLogRecord::process_result(result)); 68 result.exit_code() 69 } 70 } 71 }