sheave_core/byte_buffer/
insufficient_buffer_length.rs

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
use std::{
    error::Error,
    fmt::{
        Display,
        Formatter,
        Result as FormatResult
    },
    io::{
        Error as IOError,
        ErrorKind
    }
};

/// An error that buffer has been empty during decoding chunks.
#[derive(Debug)]
pub struct InsufficientBufferLength {
    expected: usize,
    actual: usize
}

impl InsufficientBufferLength {
    /// Constructs this error.
    pub fn new(expected: usize, actual: usize) -> Self {
        Self { expected, actual }
    }
}

impl Display for InsufficientBufferLength {
    fn fmt(&self, f: &mut Formatter<'_>) -> FormatResult {
        writeln!(f, "Buffer length is insufficient. expected: {}, actual: {}", self.expected, self.actual)
    }
}

impl Error for InsufficientBufferLength {}

/// A utility function of constructing an `InsufficientBufferLength` error.
pub fn insufficient_buffer_length(expected: usize, actual: usize) -> IOError {
    IOError::new(
        ErrorKind::InvalidInput,
        InsufficientBufferLength {
            expected,
            actual
        }
    )
}