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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use {
    std::{
        fmt,
        io
    }
};

#[derive(Debug)]
pub struct Error {
    kind: ErrorKind
}

#[derive(Debug)]
pub enum ErrorKind {
    InvalidChar,
    InvalidEnumVariant,
    InvalidUtf8,
    InvalidSystemTime,
    ZeroNonZero,
    OutOfRangeLength,
    OutOfRangeUsize,
    UnexpectedEndOfInput,
    UnexpectedEndOfOutputBuffer,
    InputBufferIsTooSmall {
        actual_size: usize,
        expected_size: usize
    },
    OutputBufferIsTooSmall {
        actual_size: usize,
        expected_size: usize
    },

    LengthIsNotTheSameAsLengthAttribute {
        field_name: &'static str
    },
    ExpectedConstant {
        constant: &'static [u8]
    },

    IoError( io::Error )
}

impl Error {
    #[inline]
    fn new( kind: ErrorKind ) -> Self {
        Error { kind }
    }

    pub fn custom( message: impl fmt::Display ) -> Self {
        // The LLVM optimizer doesn't like us adding a new variant,
        // so instead we reuse the `IoError` one.
        Error {
            kind: ErrorKind::IoError( io::Error::new( io::ErrorKind::Other, message.to_string() ) )
        }
    }

    #[inline]
    pub(crate) fn from_io_error( error: io::Error ) -> Self {
        Error {
            kind: ErrorKind::IoError( error )
        }
    }
}

impl From< Error > for io::Error {
    fn from( error: Error ) -> Self {
        if let ErrorKind::IoError( error ) = error.kind {
            return error;
        }

        let is_eof = error.is_eof();
        let kind = if is_eof {
            io::ErrorKind::UnexpectedEof
        } else {
            io::ErrorKind::InvalidData
        };

        io::Error::new( kind, format!( "{}", error ) )
    }
}

#[inline]
pub fn get_error_kind( error: &Error ) -> &ErrorKind {
    &error.kind
}

impl fmt::Display for Error {
    fn fmt( &self, fmt: &mut fmt::Formatter ) -> fmt::Result {
        match self.kind {
            ErrorKind::InvalidChar => write!( fmt, "out of range char" ),
            ErrorKind::InvalidEnumVariant => write!( fmt, "invalid enum variant" ),
            ErrorKind::InvalidUtf8 => write!( fmt, "encountered invalid utf-8" ),
            ErrorKind::InvalidSystemTime => write!( fmt, "encountered invalid system time object" ),
            ErrorKind::ZeroNonZero => write!( fmt, "a field which is supposed to be non-zero is zero" ),
            ErrorKind::OutOfRangeLength => write!( fmt, "out of range length" ),
            ErrorKind::OutOfRangeUsize => write!( fmt, "value cannot fit into an usize on this architecture" ),
            ErrorKind::UnexpectedEndOfInput => write!( fmt, "unexpected end of input" ),
            ErrorKind::UnexpectedEndOfOutputBuffer => write!( fmt, "unexpected end of output buffer" ),
            ErrorKind::InputBufferIsTooSmall { actual_size, expected_size } => write!( fmt, "input buffer is too small; expected at least {} bytes, got {}", expected_size, actual_size ),
            ErrorKind::OutputBufferIsTooSmall { actual_size, expected_size } => write!( fmt, "output buffer is too small; expected at least {} bytes, got {}", expected_size, actual_size ),
            ErrorKind::LengthIsNotTheSameAsLengthAttribute { field_name } => write!( fmt, "the length of '{}' is not the same as its 'length' attribute", field_name ),
            ErrorKind::ExpectedConstant { constant } => write!( fmt, "expected a predefined {} bytes(s) long constant", constant.len() ),
            ErrorKind::IoError( ref error ) => write!( fmt, "{}", error )
        }
    }
}

impl std::error::Error for Error {
    fn source( &self ) -> Option< &(dyn std::error::Error + 'static) > {
        match self.kind {
            ErrorKind::IoError( ref error ) => Some( error ),
            _ => None
        }
    }
}

pub trait IsEof {
    fn is_eof( &self ) -> bool;
}

impl IsEof for Error {
    fn is_eof( &self ) -> bool {
        match self.kind {
            ErrorKind::UnexpectedEndOfInput |
            ErrorKind::UnexpectedEndOfOutputBuffer => true,
            ErrorKind::IoError( ref error ) => error.kind() == std::io::ErrorKind::UnexpectedEof,
            _ => false
        }
    }
}

#[cold]
pub fn error_invalid_string_utf8< T >( _: std::string::FromUtf8Error ) -> T where T: From< Error > {
    T::from( Error::new( ErrorKind::InvalidUtf8 ) )
}

#[cold]
pub fn error_invalid_str_utf8< T >( _: std::str::Utf8Error ) -> T where T: From< Error > {
    T::from( Error::new( ErrorKind::InvalidUtf8 ) )
}

#[cold]
pub fn error_length_is_not_the_same_as_length_attribute< T >( field_name: &'static str ) -> T where T: From< Error > {
    T::from( Error::new( ErrorKind::LengthIsNotTheSameAsLengthAttribute { field_name } ) )
}

#[cold]
pub fn error_out_of_range_length< T >() -> T where T: From< Error > {
    T::from( Error::new( ErrorKind::OutOfRangeLength ) )
}

#[cold]
pub fn error_invalid_enum_variant< T >() -> T where T: From< Error > {
    T::from( Error::new( ErrorKind::InvalidEnumVariant ) )
}

#[cold]
pub fn error_out_of_range_char< T >() -> T where T: From< Error > {
    T::from( Error::new( ErrorKind::InvalidChar ) )
}

#[cold]
pub fn error_too_big_usize_for_this_architecture< T >() -> T where T: From< Error > {
    T::from( Error::new( ErrorKind::OutOfRangeUsize ) )
}

#[cold]
pub fn error_end_of_input< T >() -> T where T: From< Error > {
    T::from( Error::new( ErrorKind::UnexpectedEndOfInput ) )
}

#[cold]
pub fn error_end_of_output_buffer< T >() -> T where T: From< Error > {
    T::from( Error::new( ErrorKind::UnexpectedEndOfOutputBuffer ) )
}

#[cold]
pub fn error_input_buffer_is_too_small< T >( actual_size: usize, expected_size: usize ) -> T where T: From< Error > {
    T::from( Error::new( ErrorKind::InputBufferIsTooSmall { actual_size, expected_size } ) )
}

#[cold]
pub fn error_output_buffer_is_too_small< T >( actual_size: usize, expected_size: usize ) -> T where T: From< Error > {
    T::from( Error::new( ErrorKind::OutputBufferIsTooSmall { actual_size, expected_size } ) )
}

#[cold]
pub fn error_zero_non_zero< T >() -> T where T: From< Error > {
    T::from( Error::new( ErrorKind::ZeroNonZero ) )
}

#[cold]
pub fn error_invalid_system_time< T >() -> T where T: From< Error > {
    T::from( Error::new( ErrorKind::InvalidSystemTime ) )
}

#[cold]
pub fn error_expected_constant< T >( constant: &'static [u8] ) -> T where T: From< Error > {
    T::from( Error::new( ErrorKind::ExpectedConstant { constant } ) )
}