blockchain_zc_parser/
error.rs1#[cfg(feature = "std")]
4extern crate std;
5
6#[derive(Debug, Clone, PartialEq, Eq)]
8#[non_exhaustive]
9pub enum ParseError {
10 UnexpectedEof {
12 needed: usize,
14 available: usize,
16 },
17 InvalidVarInt,
19 IntegerTooLarge {
21 value: u64,
23 },
24 OversizedData {
26 size: usize,
28 max: usize,
30 },
31 UnsupportedVersion(i32),
33 MagicMismatch {
35 expected: [u8; 4],
37 got: [u8; 4],
39 },
40 InvalidSegwitFlag(
42 u8,
44 ),
45 InvalidInputCount,
47 TrailingBytes {
49 remaining: usize,
51 },
52 IncompleteTransactions {
54 expected: usize,
56 parsed: usize,
58 },
59 InvalidCoinbase,
61 InvalidCoinbaseCount {
63 count: usize,
65 },
66 #[cfg(feature = "std")]
68 Io {
69 message: std::string::String,
71 },
72}
73
74impl core::fmt::Display for ParseError {
75 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
76 match self {
77 ParseError::UnexpectedEof { needed, available } => write!(
78 f,
79 "unexpected end of input: needed {needed} bytes but only {available} available"
80 ),
81 ParseError::InvalidVarInt => write!(f, "invalid variable-length integer encoding"),
82 ParseError::IntegerTooLarge { value } => {
83 write!(f, "integer too large to fit into usize: {value}")
84 }
85 ParseError::OversizedData { size, max } => {
86 write!(f, "data size {size} exceeds maximum allowed {max}")
87 }
88 ParseError::UnsupportedVersion(v) => {
89 write!(f, "unsupported version: {v}")
90 }
91 ParseError::MagicMismatch { expected, got } => {
92 write!(f, "magic mismatch: expected {expected:02x?} got {got:02x?}")
93 }
94 ParseError::InvalidSegwitFlag(b) => {
95 write!(f, "invalid segwit flag byte: {b:#x}")
96 }
97 ParseError::InvalidInputCount => {
98 write!(f, "invalid transaction input count")
99 }
100 ParseError::TrailingBytes { remaining } => {
101 write!(f, "trailing bytes after parse: {remaining}")
102 }
103 ParseError::IncompleteTransactions { expected, parsed } => {
104 write!(
105 f,
106 "incomplete transaction parsing: expected {expected}, parsed {parsed}"
107 )
108 }
109 ParseError::InvalidCoinbase => {
110 write!(f, "coinbase txid reference must be all-zero bytes")
111 }
112 ParseError::InvalidCoinbaseCount { count } => {
113 write!(f, "invalid coinbase tx count in block: {count}")
114 }
115 #[cfg(feature = "std")]
116 ParseError::Io { message } => {
117 write!(f, "io error: {message}")
118 }
119 }
120 }
121}
122
123#[cfg(feature = "std")]
124impl std::error::Error for ParseError {}
125
126pub type ParseResult<T> = Result<T, ParseError>;
128
129#[cfg(feature = "std")]
130impl From<std::io::Error> for ParseError {
131 fn from(e: std::io::Error) -> Self {
132 ParseError::Io {
133 message: e.to_string(),
134 }
135 }
136}