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
use std::{
error::Error as StdError,
ffi::{IntoStringError, NulError},
fmt,
result::Result as StdResult,
str::Utf8Error,
};
#[derive(Debug)]
pub enum Error {
IntoString(IntoStringError),
NulByte(NulError),
Null,
Utf8(Utf8Error),
}
impl From<IntoStringError> for Error {
fn from(err: IntoStringError) -> Self {
Self::IntoString(err)
}
}
impl From<NulError> for Error {
fn from(err: NulError) -> Self {
Self::NulByte(err)
}
}
impl From<Utf8Error> for Error {
fn from(err: Utf8Error) -> Self {
Self::Utf8(err)
}
}
impl fmt::Display for Error {
fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result {
use self::Error::*;
match self {
IntoString(ref err) => write!(out, "string conversion error: {}", err),
NulByte(ref err) => write!(out, "nul byte error: {}", err),
Null => write!(out, "got a NULL pointer"),
Utf8(ref err) => write!(out, "UTF-8 error: {}", err),
}
}
}
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
use self::Error::*;
Some(match self {
IntoString(ref err) => err,
NulByte(ref err) => err,
Utf8(ref err) => err,
_ => return None,
})
}
}
pub type Result<T> = StdResult<T, Error>;