rhi

Coordinated trade for connected markets
git clone https://radroots.dev/git/rhi.git
Log | Files | Refs | README | LICENSE

main.rs (2475B)


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