use std::fmt;
#[derive(Debug)]
pub enum ExportError {
Io(std::io::Error),
#[cfg(feature = "csv_export")]
Csv(csv::Error),
#[cfg(feature = "json_export")]
Json(serde_json::Error),
#[cfg(feature = "couchdb")]
CouchDb(couch_rs::error::CouchError),
FeatureNotAvailable(String),
}
impl fmt::Display for ExportError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ExportError::Io(err) => write!(f, "I/O error: {err}"),
#[cfg(feature = "csv_export")]
ExportError::Csv(err) => write!(f, "CSV error: {err}"),
#[cfg(feature = "json_export")]
ExportError::Json(err) => write!(f, "JSON error: {err}"),
#[cfg(feature = "couchdb")]
ExportError::CouchDb(err) => write!(f, "CouchDB error: {err}"),
ExportError::FeatureNotAvailable(msg) => write!(f, "Feature not available: {msg}"),
}
}
}
impl std::error::Error for ExportError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
ExportError::Io(err) => Some(err),
#[cfg(feature = "csv_export")]
ExportError::Csv(err) => Some(err),
#[cfg(feature = "json_export")]
ExportError::Json(err) => Some(err),
#[cfg(feature = "couchdb")]
ExportError::CouchDb(err) => Some(err),
_ => None,
}
}
}
impl From<std::io::Error> for ExportError {
fn from(err: std::io::Error) -> Self {
ExportError::Io(err)
}
}
#[cfg(feature = "csv_export")]
impl From<csv::Error> for ExportError {
fn from(err: csv::Error) -> Self {
ExportError::Csv(err)
}
}
#[cfg(feature = "json_export")]
impl From<serde_json::Error> for ExportError {
fn from(err: serde_json::Error) -> Self {
ExportError::Json(err)
}
}
#[cfg(feature = "couchdb")]
impl From<couch_rs::error::CouchError> for ExportError {
fn from(err: couch_rs::error::CouchError) -> Self {
ExportError::CouchDb(err)
}
}
pub type ExportResult<T> = Result<T, ExportError>;