use std::convert::TryInto;
use std::error::Error;
use std::fmt::{self, Display};
use crate::option::ErrorPredicate;
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct FdbError {
error_code: i32,
}
pub const DATABASE_OPEN: i32 = 100;
pub const TUPLE_GET: i32 = 110;
pub const TUPLE_FROM_BYTES: i32 = 111;
pub const TUPLE_PACK_WITH_VERSIONSTAMP_NOT_FOUND: i32 = 112;
pub const TUPLE_PACK_WITH_VERSIONSTAMP_MULTIPLE_FOUND: i32 = 113;
pub const TUPLE_KEY_UTIL_STRINC_ERROR: i32 = 114;
pub const SUBSPACE_PACK_WITH_VERSIONSTAMP_PREFIX_INCOMPLETE: i32 = 120;
pub const SUBSPACE_UNPACK_KEY_MISMATCH: i32 = 121;
pub type FdbResult<T> = Result<T, FdbError>;
impl FdbError {
pub fn new(err: i32) -> FdbError {
FdbError { error_code: err }
}
pub fn code(self) -> i32 {
self.error_code
}
pub fn is_retryable(&self) -> bool {
unsafe {
fdb_sys::fdb_error_predicate(
ErrorPredicate::Retryable.code().try_into().unwrap(),
self.error_code,
) != 0
}
}
pub fn is_maybe_committed(&self) -> bool {
unsafe {
fdb_sys::fdb_error_predicate(
ErrorPredicate::MaybeCommitted.code().try_into().unwrap(),
self.error_code,
) != 0
}
}
pub fn is_retryable_not_committed(&self) -> bool {
unsafe {
fdb_sys::fdb_error_predicate(
ErrorPredicate::RetryableNotCommitted
.code()
.try_into()
.unwrap(),
self.error_code,
) != 0
}
}
pub(crate) fn layer_error(e: i32) -> bool {
(100..=999).contains(&e)
}
}
impl Error for FdbError {}
impl Display for FdbError {
fn fmt<'a>(&self, f: &mut fmt::Formatter<'a>) -> fmt::Result {
write!(f, "{:?}", self)
}
}
pub(crate) fn check(err: fdb_sys::fdb_error_t) -> FdbResult<()> {
if err == 0 {
Ok(())
} else {
Err(FdbError::new(err))
}
}