crates/diff/src/bin/rustik-diff/error.rs 1.4 KiB raw
1
//! Error handling for the pager binary.
2
3
use std::io;
4
5
use rustik_diff::DecodeError;
6
use rustik_highlight as syntax;
7
use rustik_repo::RepoError;
8
9
/// Binary error type.
10
#[derive(Debug)]
11
pub(super) enum Error {
12
    /// I/O failed.
13
    Io(io::Error),
14
    /// Git returned a failing exit status.
15
    Git(i32),
16
    /// Repository discovery or Git plumbing failed.
17
    Repo(RepoError),
18
    /// Diff parsing failed.
19
    Parse(DecodeError),
20
    /// Syntax highlighting setup failed.
21
    Highlight(syntax::Error),
22
}
23
24
impl From<io::Error> for Error {
25
    fn from(error: io::Error) -> Self {
26
        Self::Io(error)
27
    }
28
}
29
30
impl From<DecodeError> for Error {
31
    fn from(error: DecodeError) -> Self {
32
        Self::Parse(error)
33
    }
34
}
35
36
impl From<syntax::Error> for Error {
37
    fn from(error: syntax::Error) -> Self {
38
        Self::Highlight(error)
39
    }
40
}
41
42
impl From<RepoError> for Error {
43
    fn from(error: RepoError) -> Self {
44
        Self::Repo(error)
45
    }
46
}
47
48
impl std::fmt::Display for Error {
49
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50
        match self {
51
            Self::Io(error) => write!(f, "{error}"),
52
            Self::Git(code) => write!(f, "git diff exited with status {code}"),
53
            Self::Repo(error) => write!(f, "{error}"),
54
            Self::Parse(error) => write!(f, "{error}"),
55
            Self::Highlight(error) => write!(f, "{error}"),
56
        }
57
    }
58
}
59
60
impl std::error::Error for Error {}