shia/
errors.rs

1//! Custom errors for BEEF parsing, validation, and serialization.
2use thiserror::Error;
3use std::io;
4use anyhow::Error as AnyhowError;
5use hex::FromHexError;
6/// Core error type for Shia operations.
7#[derive(Error, Debug)]
8pub enum ShiaError {
9    /// IO-related errors during read/write.
10    #[error("IO error: {0}")]
11    Io(#[from] io::Error),
12    /// Invalid compact VarInt encoding.
13    #[error("Invalid VarInt")]
14    InvalidVarInt,
15    /// Invalid leaf flags in BUMP (must be 0,1,2).
16    #[error("Invalid flags: {0}")]
17    InvalidFlags(u8),
18    /// BEEF version mismatch (must be 4022206465).
19    #[error("Invalid version")]
20    InvalidVersion,
21    /// General verification failure (e.g., bad Merkle root, fee imbalance).
22    #[error("Verification failed: {0}")]
23    Verification(String),
24    /// Atomic mode includes unrelated transactions (BRC-95 violation).
25    #[error("Atomic mismatch: unrelated tx")]
26    AtomicMismatch,
27    /// Missing sibling hash in Merkle path.
28    #[error("Missing sibling in BUMP")]
29    MissingSibling,
30    /// Leaf TX hash not found in BUMP level 0.
31    #[error("Leaf not found in BUMP")]
32    LeafNotFound,
33    /// Script evaluation failed during input validation.
34    #[error("Script evaluation failed: {0}")]
35    ScriptEval(String),
36    /// Invalid tree height in BUMP (>64).
37    #[error("Invalid tree height: {0}")]
38    InvalidTreeHeight(u8),
39    /// BUMP merge mismatch (heights, roots, or conflicting leaves).
40    #[error("BUMP merge mismatch: {0}")]
41    MergeMismatch(&'static str),
42    /// Parse error (e.g., extra bytes, invalid format).
43    #[error("Parse error: {0}")]
44    Parse(&'static str),
45}
46impl From<AnyhowError> for ShiaError {
47    fn from(err: AnyhowError) -> Self {
48        ShiaError::Verification(err.to_string())
49    }
50}
51impl From<FromHexError> for ShiaError {
52    fn from(err: FromHexError) -> Self {
53        ShiaError::Verification(err.to_string())
54    }
55}
56/// Convenience type alias for Results.
57pub type Result<T> = std::result::Result<T, ShiaError>;