use thiserror::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ErrorKind {
Input,
NotFound,
Conflict,
Runtime,
Config,
}
#[derive(Error, Debug)]
pub enum BondError {
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("SQLite error: {0}")]
Sqlite(#[from] rusqlite::Error),
#[error("Serialization error: {0}")]
Serde(#[from] serde_json::Error),
#[error("Bond already exists")]
AlreadyExists,
#[error("Target already exists: {0}")]
TargetExists(String),
#[error("Bond not found: {0}")]
NotFound(String),
#[error("Invalid path: {0}")]
InvalidPath(String),
#[error("config error: {0}")]
Config(String),
#[error("ambiguous identifier '{0}': use more characters")]
AmbiguousId(String),
#[error("invalid timestamp: {0}")]
InvalidTimestamp(String),
}
impl BondError {
pub fn kind(&self) -> ErrorKind {
match self {
Self::InvalidPath(_) | Self::InvalidTimestamp(_) | Self::AmbiguousId(_) => {
ErrorKind::Input
}
Self::NotFound(_) => ErrorKind::NotFound,
Self::AlreadyExists | Self::TargetExists(_) => ErrorKind::Conflict,
Self::Io(_) | Self::Sqlite(_) | Self::Serde(_) => ErrorKind::Runtime,
Self::Config(_) => ErrorKind::Config,
}
}
}