crates/brain/src/error.rs 2.8 KiB raw
1
//! Error handling for the `brain` binary.
2
3
use std::io;
4
5
use rustik_diff::DecodeError;
6
use rustik_repo::RepoError;
7
8
/// Top-level error type for the binary.
9
#[derive(Debug)]
10
pub enum Error {
11
    /// I/O failed.
12
    Io(io::Error),
13
    /// A Git plumbing command failed, carrying its diagnostic.
14
    Git(String),
15
    /// A Jujutsu command failed, carrying its diagnostic.
16
    Jj(String),
17
    /// A hidden-tree merge encountered a content conflict.
18
    MergeConflict(String),
19
    /// The persisted review state belongs to another resolved range.
20
    StaleReviewState,
21
    /// Persisted review state is not valid.
22
    InvalidReviewState,
23
    /// Diff parsing failed.
24
    Parse(DecodeError),
25
    /// Highlighting asset setup failed, carrying the backend message.
26
    Highlight(String),
27
    /// The command-line arguments were invalid, carrying a usage message.
28
    Usage(String),
29
    /// A requested comment id does not exist in the selected review.
30
    CommentNotFound(u64),
31
    /// The review range contained no changes to review.
32
    Empty,
33
}
34
35
impl From<io::Error> for Error {
36
    fn from(error: io::Error) -> Self {
37
        Self::Io(error)
38
    }
39
}
40
41
impl From<DecodeError> for Error {
42
    fn from(error: DecodeError) -> Self {
43
        Self::Parse(error)
44
    }
45
}
46
47
impl From<lexopt::Error> for Error {
48
    fn from(error: lexopt::Error) -> Self {
49
        Self::Usage(error.to_string())
50
    }
51
}
52
53
impl From<RepoError> for Error {
54
    fn from(error: RepoError) -> Self {
55
        match error {
56
            RepoError::Io(e) => Self::Io(e),
57
            RepoError::Git(msg) => Self::Git(msg),
58
            RepoError::Jj(msg) => Self::Jj(msg),
59
            RepoError::MergeConflict(msg) => Self::MergeConflict(msg),
60
            RepoError::PatchApply(msg) => Self::Git(msg),
61
        }
62
    }
63
}
64
65
impl std::fmt::Display for Error {
66
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67
        match self {
68
            Self::Io(error) => write!(f, "{error}"),
69
            Self::Git(message) => write!(f, "git: {message}"),
70
            Self::Jj(message) => write!(f, "jj: {message}"),
71
            Self::MergeConflict(message) => write!(f, "merge conflict: {message}"),
72
            Self::StaleReviewState => {
73
                f.write_str("stored review state is for a different resolved range")
74
            }
75
            Self::InvalidReviewState => {
76
                f.write_str("stored review state is invalid; reset the review to continue")
77
            }
78
            Self::Parse(error) => write!(f, "{error}"),
79
            Self::Highlight(error) => write!(f, "{error}"),
80
            Self::Usage(message) => write!(f, "{message}"),
81
            Self::CommentNotFound(id) => write!(f, "comment {id} was not found"),
82
            Self::Empty => write!(f, "no changes to review between base and target"),
83
        }
84
    }
85
}
86
87
impl std::error::Error for Error {}