eth_valkyoth_protocol/withdrawal/
error.rs1use core::fmt;
2
3use eth_valkyoth_codec::{DecodeError, DecodeErrorCategory};
4
5#[non_exhaustive]
7#[derive(Clone, Copy, Debug, Eq, PartialEq)]
8pub enum WithdrawalField {
9 List,
11 Withdrawal,
13 Index,
15 ValidatorIndex,
17 Address,
19 Amount,
21}
22
23#[non_exhaustive]
25#[derive(Clone, Copy, Debug, Eq, PartialEq)]
26pub enum WithdrawalDecodeError {
27 WrongFieldCount {
29 expected: usize,
31 found: usize,
33 },
34 FieldDecode {
36 field: WithdrawalField,
38 source: DecodeError,
40 },
41 InvalidAddressLength {
43 found: usize,
45 },
46 ZeroAmount,
48}
49
50impl WithdrawalDecodeError {
51 #[must_use]
53 pub const fn code(self) -> &'static str {
54 match self {
55 Self::WrongFieldCount { .. } => "ETH_WITHDRAWAL_WRONG_FIELD_COUNT",
56 Self::FieldDecode { source, .. } => source.code(),
57 Self::InvalidAddressLength { .. } => "ETH_WITHDRAWAL_INVALID_ADDRESS_LENGTH",
58 Self::ZeroAmount => "ETH_WITHDRAWAL_ZERO_AMOUNT",
59 }
60 }
61
62 #[must_use]
64 pub const fn message(self) -> &'static str {
65 match self {
66 Self::WrongFieldCount { .. } => "withdrawal must contain exactly four RLP fields",
67 Self::FieldDecode { .. } => "withdrawal field failed bounded decoding",
68 Self::InvalidAddressLength { .. } => "withdrawal address must be exactly 20 bytes",
69 Self::ZeroAmount => "withdrawal amount must be nonzero",
70 }
71 }
72
73 #[must_use]
75 pub const fn category(self) -> WithdrawalDecodeErrorCategory {
76 match self {
77 Self::WrongFieldCount { .. } | Self::InvalidAddressLength { .. } | Self::ZeroAmount => {
78 WithdrawalDecodeErrorCategory::MalformedInput
79 }
80 Self::FieldDecode { source, .. } => match source.category() {
81 DecodeErrorCategory::ResourceExhaustion => {
82 WithdrawalDecodeErrorCategory::ResourceExhaustion
83 }
84 _ => WithdrawalDecodeErrorCategory::MalformedInput,
85 },
86 }
87 }
88}
89
90impl fmt::Display for WithdrawalDecodeError {
91 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
92 formatter.write_str(self.message())
93 }
94}
95
96#[cfg(feature = "std")]
97impl std::error::Error for WithdrawalDecodeError {}
98
99#[non_exhaustive]
101#[derive(Clone, Copy, Debug, Eq, PartialEq)]
102pub enum WithdrawalDecodeErrorCategory {
103 MalformedInput,
105 ResourceExhaustion,
107}