use chia_wallet_sdk::driver::DriverError;
use thiserror::Error;
pub type MerkleResult<T> = Result<T, MerkleError>;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum MerkleError {
#[error("chia driver error: {0}")]
Driver(#[from] DriverError),
#[error("signature calculation failed: {0}")]
Signer(String),
#[error("failed to parse DataLayer coin: {0}")]
Parse(String),
#[error("coin is not a DataLayer singleton")]
NotDataStore,
#[error("missing lineage proof for DataLayer coin")]
MissingLineage,
#[error("missing hint on DataLayer coin")]
MissingHint,
#[error("delegation permission denied: {0}")]
Permission(String),
#[error("unsupported owner: {0}")]
UnsupportedOwner(&'static str),
#[error("the supplied owner key does not control this store: it does not curry to the store's current owner puzzle hash")]
NotTheOwner,
#[error("chain precondition failed: {0}")]
Chain(String),
#[error("no coins supplied to spend")]
EmptyCoins,
#[error("invalid size bucket: {0}")]
InvalidSize(String),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn display_messages_are_descriptive() {
assert_eq!(
MerkleError::NotDataStore.to_string(),
"coin is not a DataLayer singleton"
);
assert_eq!(
MerkleError::MissingLineage.to_string(),
"missing lineage proof for DataLayer coin"
);
assert_eq!(
MerkleError::MissingHint.to_string(),
"missing hint on DataLayer coin"
);
assert_eq!(
MerkleError::Parse("bad".into()).to_string(),
"failed to parse DataLayer coin: bad"
);
assert_eq!(
MerkleError::Permission("writer cannot admin".into()).to_string(),
"delegation permission denied: writer cannot admin"
);
assert_eq!(
MerkleError::Signer("boom".into()).to_string(),
"signature calculation failed: boom"
);
assert_eq!(
MerkleError::Chain("wrong launcher".into()).to_string(),
"chain precondition failed: wrong launcher"
);
assert_eq!(
MerkleError::EmptyCoins.to_string(),
"no coins supplied to spend"
);
assert_eq!(
MerkleError::InvalidSize("exponent 11 exceeds 10".into()).to_string(),
"invalid size bucket: exponent 11 exceeds 10"
);
}
#[test]
fn wraps_driver_errors_via_from() {
let driver = DriverError::InvalidSingletonStruct;
let err: MerkleError = driver.into();
assert!(matches!(err, MerkleError::Driver(_)));
assert!(err.to_string().starts_with("chia driver error:"));
}
}