1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use crate::message::{DATA_MAX_LEN, SEQUENCE_LEN, TOPIC_MAX_LEN};
use bitcoin::consensus;
use core::fmt;

pub type Result<T> = core::result::Result<T, Error>;

#[derive(Debug)]
pub enum Error {
    InvalidMutlipartLength(usize),
    InvalidTopic(usize, [u8; TOPIC_MAX_LEN]),
    InvalidDataLength(usize),
    InvalidSequenceLength(usize),
    InvalidSequenceMessageLength(usize),
    InvalidSequenceMessageLabel(u8),
    Invalid256BitHashLength(usize),
    BitcoinDeserialization(consensus::encode::Error),
    Zmq(zmq::Error),
}

impl From<zmq::Error> for Error {
    #[inline]
    fn from(value: zmq::Error) -> Self {
        Self::Zmq(value)
    }
}

impl From<consensus::encode::Error> for Error {
    #[inline]
    fn from(value: consensus::encode::Error) -> Self {
        Self::BitcoinDeserialization(value)
    }
}

impl fmt::Display for Error {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidMutlipartLength(len) => {
                write!(f, "invalid multipart message length: {len} (expected 3)")
            }
            Self::InvalidTopic(len, topic) => {
                write!(
                    f,
                    "invalid message topic '{}'{}",
                    String::from_utf8_lossy(&topic[0..*len]),
                    if *len > TOPIC_MAX_LEN {
                        " (truncated)"
                    } else {
                        ""
                    }
                )
            }
            Self::InvalidDataLength(len) => {
                write!(f, "data too long ({len} > {DATA_MAX_LEN})")
            }
            Self::InvalidSequenceLength(len) => {
                write!(
                    f,
                    "invalid sequence length: {len} (expected {SEQUENCE_LEN})"
                )
            }
            Self::InvalidSequenceMessageLength(len) => {
                write!(f, "invalid message length {len} of message type 'sequence'")
            }
            Self::InvalidSequenceMessageLabel(label) => {
                write!(
                    f,
                    "invalid label '{}' (0x{:02x}) of message type 'sequence'",
                    *label as char, label
                )
            }
            Self::Invalid256BitHashLength(len) => {
                write!(f, "invalid hash length: {len} (expected 32)")
            }

            Self::BitcoinDeserialization(e) => {
                write!(f, "bitcoin consensus deserialization error: {e}")
            }
            Self::Zmq(e) => write!(f, "ZMQ Error: {e}"),
        }
    }
}

impl std::error::Error for Error {
    #[inline]
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        Some(match self {
            Self::BitcoinDeserialization(e) => e,
            Self::Zmq(e) => e,
            Self::InvalidMutlipartLength(_)
            | Self::InvalidTopic(_, _)
            | Self::InvalidDataLength(_)
            | Self::InvalidSequenceLength(_)
            | Self::InvalidSequenceMessageLength(_)
            | Self::InvalidSequenceMessageLabel(_)
            | Self::Invalid256BitHashLength(_) => return None,
        })
    }
}