1use crate::hint::HintedError;
2use colored::Colorize;
3
4pub trait Giveup<T, E>
6where
7 E: std::fmt::Display + Send + Sync,
8{
9 fn giveup(self, msg: &str) -> T;
23 fn hint(self, hint: &str) -> Result<T, HintedError<E>>;
30}
31
32impl<T, E> Giveup<T, E> for Result<T, E>
33where
34 E: std::fmt::Display + Send + Sync,
35{
36 fn giveup(self, msg: &str) -> T {
37 match self {
38 Ok(t) => t,
39 Err(e) => exit_gracefully(msg, &e),
40 }
41 }
42
43 fn hint(self, hint: &str) -> Result<T, HintedError<E>> {
44 match self {
45 Ok(t) => Ok(t),
46 Err(e) => Err(HintedError::from_hint(e, hint)),
47 }
48 }
49}
50
51fn exit_gracefully(
52 msg: &str,
53 error: &(dyn std::fmt::Display + Send + Sync)
54) -> ! {
55 eprintln!("{}: {}", msg.bold(), error);
56 std::process::exit(1);
57}