tokio_dl_stream_to_disk/
error.rs1use std::error::Error as StdError;
2use std::io::{Error as IOError, ErrorKind as IOErrorKind};
3use std::fmt;
4
5#[derive(Debug)]
6pub enum ErrorKind {
7 FileExists,
8 DirectoryMissing,
9 PermissionDenied,
10 InvalidResponse,
11 IO(IOError),
12 Other(Box<dyn StdError>),
13}
14
15#[derive(Debug)]
16pub struct Error {
17 kind: ErrorKind,
18}
19
20impl Error {
21 pub fn new(k: ErrorKind) -> Error {
22 Error {
23 kind: k,
24 }
25 }
26
27 pub fn kind(&self) -> &ErrorKind {
28 &self.kind
29 }
30
31 pub fn into_inner_io(self) -> Option<IOError> {
32 match self.kind {
33 ErrorKind::FileExists => None,
34 ErrorKind::DirectoryMissing => None,
35 ErrorKind::PermissionDenied => None,
36 ErrorKind::InvalidResponse => None,
37 ErrorKind::IO(err) => Some(err),
38 ErrorKind::Other(_) => None,
39 }
40 }
41
42 pub fn into_inner_other(self) -> Option<Box<dyn StdError>> {
43 match self.kind {
44 ErrorKind::FileExists => None,
45 ErrorKind::DirectoryMissing => None,
46 ErrorKind::PermissionDenied => None,
47 ErrorKind::InvalidResponse => None,
48 ErrorKind::IO(_) => None,
49 ErrorKind::Other(err) => Some(err),
50 }
51 }
52}
53
54impl From<IOError> for Error {
55 fn from(err: IOError) -> Error {
56 if err.kind() == IOErrorKind::PermissionDenied {
57 Error {
58 kind: ErrorKind::PermissionDenied,
59 }
60 } else {
61 Error {
62 kind: ErrorKind::IO(err),
63 }
64 }
65 }
66}
67
68impl From<Box<dyn StdError>> for Error {
69 fn from(err: Box<dyn StdError>) -> Error {
70 Error {
71 kind: ErrorKind::Other(err),
72 }
73 }
74}
75
76impl fmt::Display for Error {
77 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
78 match self.kind() {
79 ErrorKind::FileExists => write!(f, "File already exists"),
80 ErrorKind::DirectoryMissing => write!(f, "Destination path provided is not a valid directory"),
81 ErrorKind::PermissionDenied => write!(f, "Cannot create file: permission denied"),
82 ErrorKind::InvalidResponse => write!(f, "Invalid response from the remote host"),
83 ErrorKind::IO(err) => err.fmt(f),
84 ErrorKind::Other(err) => err.fmt(f),
85 }
86 }
87}