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
use std::fmt::{Display, Formatter};

/// Errors generated by Dryoc.
///
/// Most errors just contain a message as to what went wrong.
/// I/O errors are forwarded through.
#[derive(Debug)]
pub enum Error {
    /// An internal Dryoc error.
    Message(String),

    /// Some I/O problem occurred.
    Io(std::io::Error),

    /// Unable to convert data from slice.
    FromSlice(core::array::TryFromSliceError),
}

impl From<String> for Error {
    fn from(message: String) -> Self {
        Error::Message(message)
    }
}

impl From<&str> for Error {
    fn from(message: &str) -> Self {
        Error::Message(message.into())
    }
}

impl From<std::io::Error> for Error {
    fn from(error: std::io::Error) -> Self {
        Error::Io(error)
    }
}

impl From<core::array::TryFromSliceError> for Error {
    fn from(error: core::array::TryFromSliceError) -> Self {
        Error::FromSlice(error)
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::Message(message) => f.write_str(message),
            Error::Io(err) => write!(f, "I/O error: {}", err),
            Error::FromSlice(err) => write!(f, "From slice error: {}", err),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::Message(_) => None,
            Error::Io(err) => Some(err),
            Error::FromSlice(err) => Some(err),
        }
    }
}

macro_rules! dryoc_error {
    ($msg:expr) => {{ crate::error::Error::from(format!("{}, from {}:{}", $msg, file!(), line!())) }};
}

macro_rules! validate {
    ($min:expr, $max:expr, $value:expr, $name:literal) => {
        if $value < $min {
            return Err(dryoc_error!(format!(
                "{} value of {} less than minimum {}",
                $name, $value, $min
            )));
        } else if $value > $max {
            return Err(dryoc_error!(format!(
                "{} value of {} greater than minimum {}",
                $name, $value, $max
            )));
        }
    };
}