From: Ian Jackson Date: Sun, 30 May 2021 21:44:21 +0000 (+0100) Subject: Break out end_process ext method on anyhow::Error X-Git-Tag: otter-0.7.0~174 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=6cffc872e16aec580e036bd0bfd0f020b3f066d7;p=otter.git Break out end_process ext method on anyhow::Error Signed-off-by: Ian Jackson --- diff --git a/src/bin/otter.rs b/src/bin/otter.rs index 4368dccd..828111a5 100644 --- a/src/bin/otter.rs +++ b/src/bin/otter.rs @@ -490,30 +490,7 @@ fn main() { env::args().next().unwrap(), &subcommand)); - call(sc, mo, subargs).unwrap_or_else(|e|{ - #[derive(Default,Debug)] struct Sol { any: bool } - impl Sol { - fn nl(&mut self) { - if self.any { eprintln!("") }; - self.any = false; - } - fn head(&mut self) { - if ! self.any { eprint!("otter: error"); } - self.any = true - } - } - let mut sol: Sol = default(); - for e in e.chain() { - let s = e.to_string(); - let long = s.len() > 80; - if long && sol.any { sol.nl() } - sol.head(); - eprint!(": {}", s); - if long { sol.nl() } - } - sol.nl(); - exit(12); - }) + call(sc, mo, subargs).unwrap_or_else(|e| e.end_process(12)); } struct Conn { diff --git a/src/utils.rs b/src/utils.rs index 70337a79..0daef636 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -644,6 +644,35 @@ impl IndexVec where I: index_vec::Idx { } +#[ext(pub)] +impl anyhow::Error { + fn end_process(self, estatus: u8) -> ! { + #[derive(Default,Debug)] struct Sol { any: bool } + impl Sol { + fn nl(&mut self) { + if self.any { eprintln!("") }; + self.any = false; + } + fn head(&mut self) { + if ! self.any { eprint!("otter: error"); } + self.any = true + } + } + let mut sol: Sol = default(); + for e in self.chain() { + let s = e.to_string(); + let long = s.len() > 80; + if long && sol.any { sol.nl() } + sol.head(); + eprint!(": {}", s); + if long { sol.nl() } + } + sol.nl(); + assert!(estatus > 0); + exit(estatus.into()); + } +} + #[throws(fmt::Error)] pub fn fmt_hex(f: &mut Formatter, buf: &[u8]) { for v in buf { write!(f, "{:02x}", v)?; }