Skip to main content

yact/
error.rs

1/*
2 * Copyright 2023, 2024, 2025, 2026 Nelson Penn
3 *
4 * This file is part of Yet Another Commit Transformer.
5 *
6 * Yet Another Commit Transformer is free software: you can redistribute it
7 * and/or modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, either version 3 of the License,
9 * or (at your option) any later version.
10 *
11 * Yet Another Commit Transformer is distributed in the hope that it will be
12 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14 * Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * Yet Another Commit Transformer. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20use git2::{ErrorClass, ErrorCode};
21use semver::{Version, VersionReq};
22
23#[derive(Debug)]
24pub enum Error {
25    ConfigurationNotFound,
26    ConfigurationParseError(toml::de::Error),
27    ConfigurationEncodingError(std::str::Utf8Error),
28    UnableToDetermineYactPath,
29    PreCommitHookAlreadyExists,
30    InvalidYactVersion(VersionReq, Version),
31    InvalidGlob(String),
32    RepositoryNotFound,
33    RepositoryIsBare,
34
35    /// An error was returned from `libgit2`.
36    GitError(git2::Error),
37
38    /// One of the transformers encountered an error.
39    TransformerError(String),
40
41    /// Unexpected std::io::Error
42    IoError(std::io::Error),
43
44    /// No other errors, but the resulting index was empty.
45    ///
46    /// The commit should be aborted.
47    EmptyIndex,
48}
49
50impl From<toml::de::Error> for Error {
51    fn from(err: toml::de::Error) -> Self {
52        Self::ConfigurationParseError(err)
53    }
54}
55
56impl From<git2::Error> for Error {
57    fn from(err: git2::Error) -> Self {
58        if matches!(err.class(), ErrorClass::Repository)
59            && matches!(err.code(), ErrorCode::NotFound)
60        {
61            Self::RepositoryNotFound
62        } else {
63            Self::GitError(err)
64        }
65    }
66}
67
68impl From<std::io::Error> for Error {
69    fn from(value: std::io::Error) -> Self {
70        Self::IoError(value)
71    }
72}
73
74impl From<String> for Error {
75    fn from(err: String) -> Self {
76        Self::TransformerError(err)
77    }
78}
79
80impl From<std::str::Utf8Error> for Error {
81    fn from(err: std::str::Utf8Error) -> Self {
82        Self::ConfigurationEncodingError(err)
83    }
84}