use appcore_sync::SyncError;
use std::fmt;
pub type SqliteSyncResult<T> = Result<T, SqliteSyncError>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SqliteSyncError {
InvalidConfiguration(&'static str),
UnsafePath,
UpdateRequired,
DatabaseOperation,
IntegrityFailed,
CapacityExceeded(&'static str),
CorruptRecord(&'static str),
}
impl SqliteSyncError {
pub(crate) fn database(_error: rusqlite::Error) -> Self {
Self::DatabaseOperation
}
pub(crate) fn sync(self) -> SyncError {
SyncError::ReplicationFailed(self.to_string())
}
}
impl fmt::Display for SqliteSyncError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidConfiguration(reason) => {
write!(formatter, "invalid SQLite sync configuration: {reason}")
}
Self::UnsafePath => formatter.write_str("SQLite sync path is unsafe"),
Self::UpdateRequired => formatter.write_str("NO MORE SUPPORTED PLEASE UPDATE"),
Self::DatabaseOperation => formatter.write_str("SQLite sync operation failed"),
Self::IntegrityFailed => formatter.write_str("SQLite sync integrity check failed"),
Self::CapacityExceeded(resource) => {
write!(formatter, "SQLite sync {resource} capacity exceeded")
}
Self::CorruptRecord(kind) => write!(formatter, "corrupt SQLite sync {kind} record"),
}
}
}
impl std::error::Error for SqliteSyncError {}