use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Git command failed: {0}")]
Git(String),
#[error("Configuration error: {0}")]
Config(String),
#[error("API provider error: {0}")]
Provider(String),
#[error("Project root not found")]
ProjectRootNotFound,
#[error("Git directory not found in project")]
GitDirectoryNotFound,
#[error("Branch operation failed: {0}")]
Branch(String),
#[error("Hook execution failed: {0}")]
Hook(String),
#[error("Authentication error: {0}")]
Auth(String),
#[error("Network error: {0}")]
Network(String),
#[error("JSON parsing error: {0}")]
Json(String),
#[error("Regex error: {0}")]
Regex(#[from] regex::Error),
#[error("{0}")]
Other(String),
}
pub type Result<T> = std::result::Result<T, Error>;
impl Error {
pub fn msg<S: Into<String>>(msg: S) -> Self {
Error::Other(msg.into())
}
pub fn git<S: Into<String>>(msg: S) -> Self {
Error::Git(msg.into())
}
pub fn config<S: Into<String>>(msg: S) -> Self {
Error::Config(msg.into())
}
pub fn branch<S: Into<String>>(msg: S) -> Self {
Error::Branch(msg.into())
}
pub fn hook<S: Into<String>>(msg: S) -> Self {
Error::Hook(msg.into())
}
pub fn auth<S: Into<String>>(msg: S) -> Self {
Error::Auth(msg.into())
}
pub fn provider<S: Into<String>>(msg: S) -> Self {
Error::Provider(msg.into())
}
pub fn network<S: Into<String>>(msg: S) -> Self {
Error::Network(msg.into())
}
}
impl From<json5::Error> for Error {
fn from(err: json5::Error) -> Self {
Error::Config(err.to_string())
}
}
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Self {
Error::Json(err.to_string())
}
}
impl From<keyring::Error> for Error {
fn from(err: keyring::Error) -> Self {
Error::Auth(err.to_string())
}
}
impl From<reqwest::Error> for Error {
fn from(err: reqwest::Error) -> Self {
Error::Network(err.to_string())
}
}
impl From<std::env::VarError> for Error {
fn from(err: std::env::VarError) -> Self {
Error::Other(err.to_string())
}
}
impl From<std::string::FromUtf8Error> for Error {
fn from(err: std::string::FromUtf8Error) -> Self {
Error::Other(err.to_string())
}
}