grit/
error.rs

1use std::{ffi, fmt};
2
3use crate::{git, trunk};
4
5#[derive(Debug)]
6pub enum Error {
7    /// An unrecognized command line argument was supplied.
8    Arg(ffi::OsString),
9
10    /// Git produced unexpected output. (If Git itself is not found, we panic.)
11    Git(git::Error),
12
13    /// A branch name was expected as an argument, but was not provided.
14    /// <https://en.wikipedia.org/wiki/Argument_Clinic>
15    BranchName,
16
17    /// No local trunk branch could be identified. This can happen if a command
18    /// is run outside of any git repo, or if the repo has no local branch
19    /// matching any recognized trunk name.
20    Trunk(trunk::Error),
21
22    /// The Git working copy has uncommitted changes.
23    Unclean,
24}
25
26impl From<git::Error> for Error {
27    fn from(value: git::Error) -> Self {
28        Error::Git(value)
29    }
30}
31
32impl From<trunk::Error> for Error {
33    fn from(value: trunk::Error) -> Self {
34        Error::Trunk(value)
35    }
36}
37
38impl fmt::Display for Error {
39    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40        match self {
41            Error::Arg(arg) => write!(f, "{}: unexpected argument", arg.display()),
42            Error::Git(err) => err.fmt(f),
43            Error::BranchName => "expected a branch name".fmt(f),
44            Error::Trunk(err) => err.fmt(f),
45            Error::Unclean => "working copy is unclean".fmt(f),
46        }
47    }
48}
49
50pub type Result<T> = std::result::Result<T, Error>;