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
//! Common types used throughout the rest of the crate
use std::borrow::Cow;
use std::fmt::{Debug, Display, Formatter};

/// General result type used by a decoder instance
pub type DecoderResult<T> = Result<T, DecoderError>;

/// Enumeration of different decoder errors
#[derive(Debug, Copy, Clone)]
pub enum DecoderErrorCode {
    /// Something went pear-shaped in the underlying stream
    StreamFailure,
    /// Detected an invalid byte sequence
    InvalidByteSequence,
    /// The end of the input has been reached
    EndOfInput,
}

/// Structure for encoding errors
#[derive(Debug, Clone)]
pub struct DecoderError {
    /// The error code
    pub code: DecoderErrorCode,

    /// Associated error message
    pub message: Cow<'static, str>,
}

impl Display for DecoderError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "Code: {:?}, Message: {}", self.code, self.message)
    }
}

/// Helper macro for generating errors
#[macro_export]
macro_rules! decoder_error {
    ($code : expr, $msg : expr) => {
        DecoderError {
            code: $code,
            message: $msg.into()
        }
    }
}

#[macro_export]
macro_rules! end_of_input {
    () => {
        Err(DecoderError {
            code: DecoderErrorCode::EndOfInput,
            message: "end of input reached".into()
        })
    }
}


#[macro_export]
macro_rules! invalid_byte_sequence {
    () => {
        Err(DecoderError {
            code: DecoderErrorCode::InvalidByteSequence,
            message: "invalid byte sequence".into()
        })
    }
}