|
| 1 | +#![feature(never_type)] |
| 2 | +#![feature(try_trait_v2)] |
| 3 | +#![feature(try_trait_v2_residual)] |
| 4 | + |
| 5 | +use std::{ |
| 6 | + fmt::Debug, |
| 7 | + io, |
| 8 | + process::{Child, Output, Termination as _T}, |
| 9 | +}; |
| 10 | + |
| 11 | +use exit_safely::Termination; |
| 12 | +use try_v2::{Try, Try_ConvertResult}; |
| 13 | + |
| 14 | +pub mod commands; |
| 15 | + |
| 16 | +#[derive(Debug, Termination, Try, Try_ConvertResult, PartialEq, PartialOrd, Eq, Ord)] |
| 17 | +#[repr(u8)] |
| 18 | +#[must_use] |
| 19 | +pub enum Exit<T: _T> { |
| 20 | + Ok(T) = 0, |
| 21 | + Error(String) = 1, |
| 22 | + InvocationError(String) = 2, |
| 23 | + IO(String) = 3, |
| 24 | +} |
| 25 | + |
| 26 | +impl Exit<()> { |
| 27 | + fn message(&self) -> &str { |
| 28 | + match self { |
| 29 | + Exit::Ok(_) => "", |
| 30 | + Exit::Error(m) => m, |
| 31 | + Exit::InvocationError(m) => m, |
| 32 | + Exit::IO(m) => m, |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + fn replace_message(self, msg: String) -> Option<Self> { |
| 37 | + match self { |
| 38 | + Exit::Ok(_) => None, |
| 39 | + Exit::Error(_) => Some(Exit::Error(msg)), |
| 40 | + Exit::InvocationError(_) => Some(Exit::InvocationError(msg)), |
| 41 | + Exit::IO(_) => Some(Exit::IO(msg)), |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl FromIterator<Exit<()>> for Exit<()> { |
| 47 | + fn from_iter<I: IntoIterator<Item = Exit<()>>>(iter: I) -> Self { |
| 48 | + let mut msg = String::new(); |
| 49 | + iter.into_iter() |
| 50 | + .filter_map(|e| { |
| 51 | + if let Exit::Ok(_) = e { |
| 52 | + None |
| 53 | + } else { |
| 54 | + msg.push_str(e.message()); |
| 55 | + msg.push('\n'); |
| 56 | + Some(e) |
| 57 | + } |
| 58 | + }) |
| 59 | + .min() |
| 60 | + .and_then(|e| e.replace_message(msg)) |
| 61 | + .unwrap_or(Exit::Ok(())) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl<T: _T> From<clap::Error> for Exit<T> { |
| 66 | + fn from(e: clap::Error) -> Self { |
| 67 | + Self::InvocationError(e.to_string()) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +#[derive(Debug)] |
| 72 | +pub struct Cmd { |
| 73 | + pub name: &'static str, |
| 74 | + pub result: Result<Output, io::Error>, |
| 75 | +} |
| 76 | + |
| 77 | +trait CmdExt { |
| 78 | + fn into_cmd(self, name: &'static str) -> Cmd; |
| 79 | +} |
| 80 | + |
| 81 | +impl CmdExt for Result<Output, io::Error> { |
| 82 | + fn into_cmd(self, name: &'static str) -> Cmd { |
| 83 | + Cmd { name, result: self } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +impl From<Cmd> for Exit<()> { |
| 88 | + fn from(cmd: Cmd) -> Self { |
| 89 | + match cmd.result { |
| 90 | + Ok(output) => { |
| 91 | + if output.status.success() { |
| 92 | + println!("{}: OK", cmd.name); |
| 93 | + Self::Ok(()) |
| 94 | + } else { |
| 95 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 96 | + Self::Error(stderr.to_string()) |
| 97 | + } |
| 98 | + } |
| 99 | + Err(e) => { |
| 100 | + let msg = format!("{} failed: {}", cmd.name, e); |
| 101 | + Self::IO(msg) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +#[derive(Debug)] |
| 108 | +pub struct Spawned { |
| 109 | + pub name: &'static str, |
| 110 | + pub child: Result<Child, io::Error>, |
| 111 | +} |
| 112 | + |
| 113 | +impl Spawned { |
| 114 | + pub fn wait(self) -> Cmd { |
| 115 | + match self.child { |
| 116 | + Ok(child) => child.wait_with_output().into_cmd(self.name), |
| 117 | + Err(e) => Cmd { |
| 118 | + name: self.name, |
| 119 | + result: Err(e), |
| 120 | + }, |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +trait SpawnedExt { |
| 126 | + fn into_spawned(self, name: &'static str) -> Spawned; |
| 127 | +} |
| 128 | + |
| 129 | +impl SpawnedExt for Result<Child, io::Error> { |
| 130 | + fn into_spawned(self, name: &'static str) -> Spawned { |
| 131 | + Spawned { name, child: self } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +impl From<Vec<Spawned>> for Exit<()> { |
| 136 | + fn from(spawns: Vec<Spawned>) -> Self { |
| 137 | + spawns |
| 138 | + .into_iter() |
| 139 | + .map(|spawn| spawn.wait()) |
| 140 | + .map(Exit::from) |
| 141 | + .collect() |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +#[cfg(test)] |
| 146 | +mod tests { |
| 147 | + use std::process::Command; |
| 148 | + |
| 149 | + use super::*; |
| 150 | + |
| 151 | + #[test] |
| 152 | + fn exit_from_404() { |
| 153 | + let splat: Cmd = Command::new("splat").output().into_cmd("splat"); |
| 154 | + assert_eq!(splat.name, "splat"); |
| 155 | + assert!( |
| 156 | + matches!(splat.result, Result::Err(ref e) if matches!(e.kind(), io::ErrorKind::NotFound)) |
| 157 | + ); |
| 158 | + let exit: Exit<()> = Exit::from(splat); |
| 159 | + let Exit::IO(ref msg) = exit else { |
| 160 | + panic!("not an IO2") |
| 161 | + }; |
| 162 | + eprintln!("{}", msg); |
| 163 | + assert!(msg.starts_with("splat failed: ")); |
| 164 | + } |
| 165 | + |
| 166 | + #[test] |
| 167 | + fn collect_exit() { |
| 168 | + let exits = [ |
| 169 | + Exit::Ok(()), |
| 170 | + Exit::IO("one".to_string()), |
| 171 | + Exit::Error("two".to_string()), |
| 172 | + Exit::Error("three".to_string()), |
| 173 | + ]; |
| 174 | + let exit: Exit<()> = exits.into_iter().collect(); |
| 175 | + let expected = "one\ntwo\nthree\n"; |
| 176 | + dbg!(&exit); |
| 177 | + assert!(matches!(exit, Exit::Error(s) if s == expected)); |
| 178 | + } |
| 179 | +} |
0 commit comments