1use crate::term::{ERR_RED_BOLD, TERM_ERR, TERM_OUT};
2
3use std::{fmt::Display, io, process::exit};
4
5pub trait CliError: Display + Sized {
6 fn color(self) -> Self {
7 self
8 }
9
10 fn code(&self) -> i32 {
12 1
13 }
14
15 fn print(self) -> io::Result<()> {
16 TERM_ERR.write_str(&format!("{}: ", ERR_RED_BOLD.apply_to("error").to_string()))?;
17 TERM_ERR.write_line(self.color().to_string().as_str())?;
18 TERM_ERR.flush()
19 }
20}
21
22pub fn finish<T>(result: Result<(), T>)
23where
24 T: CliError,
25{
26 let code = if let Some(e) = result.err() {
27 let code = e.code();
28
29 e.print().unwrap();
30 code
31 } else {
32 0
33 };
34
35 TERM_ERR.flush().unwrap();
36 TERM_OUT.flush().unwrap();
37
38 exit(code);
39}