pub use turnframe_core::error::StoreError;
pub type StoreResult<T> = Result<T, StoreError>;
pub mod codes {
pub const IDENTITY_MISMATCH: &str = "turnframe.store.identity_mismatch";
pub const INVALID_RECORD: &str = "turnframe.store.invalid_record";
pub const BUNDLE_ACCOUNT_MISMATCH: &str = "turnframe.store.bundle_account_mismatch";
}
#[must_use]
pub fn identity_mismatch() -> StoreError {
StoreError::Other {
code: codes::IDENTITY_MISMATCH.to_owned(),
}
}
#[must_use]
pub fn invalid_record() -> StoreError {
StoreError::Other {
code: codes::INVALID_RECORD.to_owned(),
}
}
#[must_use]
pub fn bundle_account_mismatch() -> StoreError {
StoreError::Other {
code: codes::BUNDLE_ACCOUNT_MISMATCH.to_owned(),
}
}
#[must_use]
pub fn has_code(error: &StoreError, code: &str) -> bool {
matches!(error, StoreError::Other { code: actual } if actual == code)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn code_helpers_round_trip() {
assert!(has_code(&identity_mismatch(), codes::IDENTITY_MISMATCH));
assert!(has_code(&invalid_record(), codes::INVALID_RECORD));
assert!(has_code(
&bundle_account_mismatch(),
codes::BUNDLE_ACCOUNT_MISMATCH
));
assert!(!has_code(&StoreError::NotFound, codes::INVALID_RECORD));
assert!(!has_code(&invalid_record(), codes::IDENTITY_MISMATCH));
}
#[test]
fn display_carries_codes_only() {
assert_eq!(
invalid_record().to_string(),
"store failure turnframe.store.invalid_record"
);
}
}