chiark / gitweb /
Break out end_process ext method on anyhow::Error
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 30 May 2021 21:44:21 +0000 (22:44 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 1 Jun 2021 00:32:44 +0000 (01:32 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/bin/otter.rs
src/utils.rs

index 4368dccd56823d22043ce77da888adcd3dc0fe88..828111a5d6363b2eeb607ae694ff2ac18d73b125 100644 (file)
@@ -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 {
index 70337a79abe93e9a20f185932f6209139f15e5f8..0daef6369fe8f2693ad405125668f7ffb1a1b369 100644 (file)
@@ -644,6 +644,35 @@ impl<I,T> IndexVec<I,T> 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)?; }