use std::fmt;
#[derive(Debug)]
pub enum VectorError {
Db(String),
Nostr(String),
Crypto(String),
Network(String),
Io(std::io::Error),
NotInitialized(String),
Other(String),
}
impl fmt::Display for VectorError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
VectorError::Db(msg) => write!(f, "Database error: {}", msg),
VectorError::Nostr(msg) => write!(f, "Nostr error: {}", msg),
VectorError::Crypto(msg) => write!(f, "Crypto error: {}", msg),
VectorError::Network(msg) => write!(f, "Network error: {}", msg),
VectorError::Io(err) => write!(f, "I/O error: {}", err),
VectorError::NotInitialized(msg) => write!(f, "Not initialized: {}", msg),
VectorError::Other(msg) => write!(f, "{}", msg),
}
}
}
impl std::error::Error for VectorError {}
impl From<String> for VectorError {
fn from(s: String) -> Self {
VectorError::Other(s)
}
}
impl From<&str> for VectorError {
fn from(s: &str) -> Self {
VectorError::Other(s.to_string())
}
}
impl From<std::io::Error> for VectorError {
fn from(err: std::io::Error) -> Self {
VectorError::Io(err)
}
}
impl From<rusqlite::Error> for VectorError {
fn from(err: rusqlite::Error) -> Self {
VectorError::Db(err.to_string())
}
}
impl From<nostr_sdk::client::Error> for VectorError {
fn from(err: nostr_sdk::client::Error) -> Self {
VectorError::Nostr(err.to_string())
}
}
impl From<reqwest::Error> for VectorError {
fn from(err: reqwest::Error) -> Self {
VectorError::Network(err.to_string())
}
}
pub type Result<T> = std::result::Result<T, VectorError>;
impl From<VectorError> for String {
fn from(err: VectorError) -> String {
err.to_string()
}
}