gplay_client/
error.rs

1use std::error::Error as StdError;
2use std::fmt;
3use std::io::Error as IOError;
4
5#[derive(Debug)]
6pub enum ErrorKind {
7    FileExists,
8    DirectoryExists,
9    DirectoryMissing,
10    InvalidApp,
11    Authentication,
12    TermsOfService,
13    PermissionDenied,
14    InvalidResponse,
15    LoginRequired,
16    IO(IOError),
17    Str(String),
18    Other(Box<dyn StdError>),
19}
20
21#[derive(Debug)]
22pub struct Error {
23    kind: ErrorKind,
24}
25
26impl Error {
27    pub fn new(k: ErrorKind) -> Error {
28        Error { kind: k }
29    }
30
31    pub fn kind(&self) -> &ErrorKind {
32        &self.kind
33    }
34}
35
36impl From<IOError> for Error {
37    fn from(err: IOError) -> Error {
38        Error {
39            kind: ErrorKind::IO(err),
40        }
41    }
42}
43
44impl From<Box<dyn StdError>> for Error {
45    fn from(err: Box<dyn StdError>) -> Error {
46        Error {
47            kind: ErrorKind::Other(err),
48        }
49    }
50}
51
52impl From<&str> for Error {
53    fn from(err: &str) -> Error {
54        Error {
55            kind: ErrorKind::Str(err.to_string()),
56        }
57    }
58}
59
60impl From<String> for Error {
61    fn from(err: String) -> Error {
62        Error {
63            kind: ErrorKind::Str(err),
64        }
65    }
66}
67
68impl StdError for Error {}
69
70impl fmt::Display for Error {
71    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
72        match self.kind() {
73            ErrorKind::FileExists => write!(f, "File already exists"),
74            ErrorKind::InvalidApp => write!(f, "Invalid app response"),
75            ErrorKind::DirectoryExists => write!(f, "Directory already exists"),
76            ErrorKind::DirectoryMissing => write!(f, "Destination path provided is not a valid directory"),
77            ErrorKind::Authentication => write!(f, "Could not authenticate with Google. Please provide a new oAuth token."),
78            ErrorKind::TermsOfService => write!(f, "Must accept Google Play Terms of Service before proceeding."),
79            ErrorKind::PermissionDenied => write!(f, "Cannot create file: permission denied"),
80            ErrorKind::InvalidResponse => write!(f, "Invalid response from the remote host"),
81            ErrorKind::LoginRequired => write!(f, "Logging in is required for this action"),
82            ErrorKind::IO(err) => err.fmt(f),
83            ErrorKind::Str(err) => err.fmt(f),
84            ErrorKind::Other(err) => err.fmt(f),
85        }
86    }
87}