use thiserror::Error;
pub type DigStoreResult<T> = Result<T, DigStoreError>;
#[derive(Debug, Error)]
pub enum DigStoreError {
#[error("invalid store size: {0}")]
InvalidSize(String),
#[error("size-proof mismatch: .dig is {actual_bytes} bytes (bucket {actual_k}) but the store anchored bucket {anchored_k}; discarding")]
SizeProofMismatch {
anchored_k: u8,
actual_k: u8,
actual_bytes: u64,
},
#[error("invalid URN or identifier: {0}")]
InvalidUrn(String),
#[error("on-chain proof failed: {0}")]
Proof(String),
#[error("capsule error: {0}")]
Capsule(String),
#[error("spend-build error: {0}")]
Spend(String),
}
impl From<dig_merkle::MerkleError> for DigStoreError {
fn from(error: dig_merkle::MerkleError) -> Self {
match error {
dig_merkle::MerkleError::InvalidSize(message) => DigStoreError::InvalidSize(message),
other => DigStoreError::Spend(other.to_string()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn merkle_invalid_size_maps_to_invalid_size() {
let err: DigStoreError = dig_merkle::MerkleError::InvalidSize("too big".into()).into();
match err {
DigStoreError::InvalidSize(message) => assert_eq!(message, "too big"),
other => panic!("expected InvalidSize, got {other:?}"),
}
}
#[test]
fn other_merkle_errors_map_to_spend() {
let err: DigStoreError = dig_merkle::MerkleError::NotDataStore.into();
assert!(matches!(err, DigStoreError::Spend(_)));
}
#[test]
fn size_proof_mismatch_displays_detail() {
let err = DigStoreError::SizeProofMismatch {
anchored_k: 7,
actual_k: 6,
actual_bytes: 64,
};
assert!(err.to_string().contains("size-proof mismatch"));
}
}