1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use std::path::PathBuf;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, derive_more::From)]
pub enum Error {
    Cli(clap::Error),
    InvalidRepoPath(PathBuf),
    IO(std::io::Error),
    ChildExit(String, Option<i32>),
    LogInit(log::SetLoggerError),
}

impl Error {
    pub fn exit(self) -> ! {
        use Error::*;

        match self {
            Cli(e) => e.exit(),
            InvalidRepoPath(p) => log::error!("invalid repository path; {:?}", p.display()),
            IO(e) => log::error!("{}", e),
            ChildExit(cmd, code) => {
                log::error!("child exited with error code {:?}", code);
                log::error!("child command: {}", cmd);
            }
            LogInit(e) => log::error!("failed to initialize logging; {}", e),
        }

        std::process::exit(1);
    }
}