sheave_core/byte_buffer/
insufficient_buffer_length.rs

1use std::{
2    error::Error,
3    fmt::{
4        Display,
5        Formatter,
6        Result as FormatResult
7    },
8    io::{
9        Error as IOError,
10        ErrorKind
11    }
12};
13
14/// An error that buffer has been empty during decoding chunks.
15#[derive(Debug)]
16pub struct InsufficientBufferLength {
17    expected: usize,
18    actual: usize
19}
20
21impl InsufficientBufferLength {
22    /// Constructs this error.
23    pub fn new(expected: usize, actual: usize) -> Self {
24        Self { expected, actual }
25    }
26}
27
28impl Display for InsufficientBufferLength {
29    fn fmt(&self, f: &mut Formatter<'_>) -> FormatResult {
30        writeln!(f, "Buffer length is insufficient. expected: {}, actual: {}", self.expected, self.actual)
31    }
32}
33
34impl Error for InsufficientBufferLength {}
35
36/// A utility function of constructing an `InsufficientBufferLength` error.
37pub fn insufficient_buffer_length(expected: usize, actual: usize) -> IOError {
38    IOError::new(
39        ErrorKind::InvalidInput,
40        InsufficientBufferLength {
41            expected,
42            actual
43        }
44    )
45}