use crate::{db::session::DbSession, error::Error, traits::CanisterKind};
use candid::CandidType;
use icydb_core as core;
use serde::Deserialize;
#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
pub enum IntegrityCheckError {
Database(Error),
Job(core::db::IntegrityJobError),
Uninspectable(core::db::IntegrityAuthorityDiagnostic),
}
#[cfg(feature = "sql")]
#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
pub enum SqlIntegrityError {
Integrity(IntegrityCheckError),
Sql(Error),
}
impl From<core::db::IntegrityDeepError> for IntegrityCheckError {
fn from(error: core::db::IntegrityDeepError) -> Self {
match error {
core::db::IntegrityDeepError::Job(error) => Self::Job(error),
core::db::IntegrityDeepError::Internal(error) => Self::Database(error.into()),
core::db::IntegrityDeepError::Uninspectable(diagnostic) => {
Self::Uninspectable(diagnostic)
}
}
}
}
#[cfg(feature = "sql")]
impl From<core::db::SqlIntegrityError> for SqlIntegrityError {
fn from(error: core::db::SqlIntegrityError) -> Self {
match error {
core::db::SqlIntegrityError::Integrity(error) => {
Self::Integrity(IntegrityCheckError::from(error))
}
core::db::SqlIntegrityError::Sql(error) => Self::Sql(error.into()),
}
}
}
impl<C: CanisterKind> DbSession<C> {
pub fn execute_admin_integrity(
&self,
request: core::db::IntegrityCheckRequest,
owner: core::db::IntegrityJobOwner,
) -> Result<core::db::IntegrityCheckResult, IntegrityCheckError> {
self.inner
.execute_admin_integrity(request, owner)
.map_err(IntegrityCheckError::from)
}
#[cfg(feature = "sql")]
pub fn execute_admin_integrity_sql(
&self,
sql: &str,
owner: core::db::IntegrityJobOwner,
) -> Result<core::db::IntegrityCheckResult, SqlIntegrityError> {
self.inner
.execute_admin_integrity_sql(sql, owner)
.map_err(SqlIntegrityError::from)
}
}