use std::io;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum BackupError {
#[error("I/O error: {0}")]
Io(#[from] io::Error),
#[error("serialization error: {0}")]
Serialization(String),
#[error("deserialization error: {0}")]
Deserialization(String),
#[error("invalid backup format: {0}")]
InvalidFormat(String),
#[error("unsupported backup version: {0}")]
UnsupportedVersion(u32),
#[error("transaction error: {0}")]
Transaction(#[from] manifoldb_core::TransactionError),
#[error("storage error: {0}")]
Storage(#[from] manifoldb_storage::engine::StorageError),
#[error("integrity check failed: {0}")]
IntegrityError(String),
#[error("incomplete backup: {0}")]
Incomplete(String),
#[error("record type mismatch: expected {expected}, got {actual}")]
RecordTypeMismatch {
expected: String,
actual: String,
},
#[error("duplicate record: {0}")]
DuplicateRecord(String),
#[error("missing reference: {0}")]
MissingReference(String),
#[error("malformed record at line {line}: {message}")]
MalformedRecord {
line: u64,
message: String,
},
}
impl BackupError {
pub fn serialization(err: serde_json::Error) -> Self {
Self::Serialization(err.to_string())
}
pub fn deserialization(err: serde_json::Error) -> Self {
Self::Deserialization(err.to_string())
}
pub fn invalid_format(msg: impl Into<String>) -> Self {
Self::InvalidFormat(msg.into())
}
pub fn integrity(msg: impl Into<String>) -> Self {
Self::IntegrityError(msg.into())
}
pub fn incomplete(msg: impl Into<String>) -> Self {
Self::Incomplete(msg.into())
}
pub fn malformed_record(line: u64, msg: impl Into<String>) -> Self {
Self::MalformedRecord { line, message: msg.into() }
}
}
pub type BackupResult<T> = Result<T, BackupError>;
impl From<crate::Error> for BackupError {
fn from(err: crate::Error) -> Self {
match err {
crate::Error::Transaction(e) => Self::Transaction(e),
crate::Error::Storage(e) => Self::Storage(e),
other => Self::Serialization(other.to_string()),
}
}
}