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
82
83
84
85
86
87
88
89
use std::convert::From;
use std::ffi::{OsStr, OsString};
use std::fmt::Display;
use std::io::Error as IoError;
#[allow(unused_imports)]
use std::io::ErrorKind;
#[derive(Debug)]
pub enum Error {
Io(IoError),
#[cfg(feature = "http")]
Http {
code: u16,
message: String,
},
}
pub type Result<T> = std::result::Result<T, Error>;
impl Error {
pub(crate) fn to_os_string(&self, path: &OsStr) -> OsString {
let mut str = OsString::new();
str.push("Error opening ");
str.push(path);
str.push(": ");
str.push(self.to_string());
str
}
pub(crate) fn io<E>(kind: ErrorKind, error: E) -> Error
where
E: Into<Box<dyn std::error::Error + Send + Sync>>,
{
Self::Io(IoError::new(kind, error))
}
}
impl From<IoError> for Error {
fn from(err: IoError) -> Self {
Error::Io(err)
}
}
impl From<Error> for IoError {
fn from(err: Error) -> Self {
match err {
Error::Io(err) => err,
#[cfg(feature = "http")]
Error::Http { code: 404, message } => IoError::new(ErrorKind::NotFound, message),
#[cfg(feature = "http")]
Error::Http { code: 403, message } => {
IoError::new(ErrorKind::PermissionDenied, message)
}
#[cfg(feature = "http")]
Error::Http { .. } => IoError::new(ErrorKind::Other, err.to_string()),
}
}
}
impl std::error::Error for Error {}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
match self {
Error::Io(err) => err.fmt(f),
#[cfg(feature = "http")]
Error::Http { code, message } => write!(f, "{}: {}", code, message),
}
}
}
#[cfg(unix)]
pub(crate) fn seek_error() -> IoError {
IoError::from_raw_os_error(libc::ESPIPE)
}
#[cfg(not(unix))]
pub(crate) fn seek_error() -> IoError {
IoError::new(ErrorKind::NotFound, "Cannot seek on stream")
}