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
use std::{str::Utf8Error, string::FromUtf8Error};
pub trait Error: Sized {
fn insufficient_bytes() -> Self;
fn invalid_data() -> Self;
#[inline]
fn utf8_err(_: Utf8Error) -> Self {
Self::invalid_data()
}
#[inline]
fn from_utf8_err(err: FromUtf8Error) -> Self {
Self::utf8_err(err.utf8_error())
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum ErrorKind {
InvalidData,
InsufficientBytes,
Utf8Error(Utf8Error),
FromUtf8Error(FromUtf8Error),
}
impl std::fmt::Display for ErrorKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(self, f)
}
}
impl std::error::Error for ErrorKind {}
impl Error for ErrorKind {
fn insufficient_bytes() -> Self {
Self::InsufficientBytes
}
fn invalid_data() -> Self {
Self::InvalidData
}
fn utf8_err(err: Utf8Error) -> Self {
Self::Utf8Error(err)
}
fn from_utf8_err(err: FromUtf8Error) -> Self {
Self::FromUtf8Error(err)
}
}
impl Error for () {
#[inline]
fn insufficient_bytes() -> Self {}
#[inline]
fn invalid_data() -> Self {}
}