Skip to main content

yact/
error.rs

1/*
2 * Copyright 2023, 2024, 2025 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    InvalidYactVersion(VersionReq, Version),
29    InvalidGlob(String),
30    RepositoryNotFound,
31    RepositoryIsBare,
32
33    /// An error was returned from `libgit2`.
34    GitError(git2::Error),
35
36    /// One of the transformers encountered an error.
37    TransformerError(String),
38
39    /// Unexpected std::io::Error
40    IoError(std::io::Error),
41
42    /// No other errors, but the resulting index was empty.
43    ///
44    /// The commit should be aborted.
45    EmptyIndex,
46}
47
48impl From<toml::de::Error> for Error {
49    fn from(err: toml::de::Error) -> Self {
50        Self::ConfigurationParseError(err)
51    }
52}
53
54impl From<git2::Error> for Error {
55    fn from(err: git2::Error) -> Self {
56        if matches!(err.class(), ErrorClass::Repository)
57            && matches!(err.code(), ErrorCode::NotFound)
58        {
59            Self::RepositoryNotFound
60        } else {
61            Self::GitError(err)
62        }
63    }
64}
65
66impl From<std::io::Error> for Error {
67    fn from(value: std::io::Error) -> Self {
68        Self::IoError(value)
69    }
70}
71
72impl From<String> for Error {
73    fn from(err: String) -> Self {
74        Self::TransformerError(err)
75    }
76}
77
78impl From<std::str::Utf8Error> for Error {
79    fn from(err: std::str::Utf8Error) -> Self {
80        Self::ConfigurationEncodingError(err)
81    }
82}