rs_password_utils/
errors.rs

1// Copyright 2020 astonbitecode
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14use std::{fmt, result};
15use std::num::ParseIntError;
16use std::str::Utf8Error;
17use std::io;
18
19use hex::FromHexError;
20use hyper;
21use hyper::http::uri::InvalidUri;
22
23pub type Result<T> = result::Result<T, PasswordUtilsError>;
24
25#[derive(Debug, PartialEq, Eq, Clone)]
26pub enum PasswordUtilsError {
27    General(String),
28    CommunicationWithThirdPartyApiError(String),
29    ParseError(String),
30}
31
32impl fmt::Display for PasswordUtilsError {
33    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
34        match self {
35            &PasswordUtilsError::General(ref message) => write!(f, "{}", message),
36            &PasswordUtilsError::CommunicationWithThirdPartyApiError(ref message) => write!(f, "{}", message),
37            &PasswordUtilsError::ParseError(ref message) => write!(f, "{}", message),
38        }
39    }
40}
41
42impl From<hyper::Error> for PasswordUtilsError {
43    fn from(err: hyper::Error) -> PasswordUtilsError {
44        PasswordUtilsError::CommunicationWithThirdPartyApiError(format!("{:?}", err))
45    }
46}
47
48impl From<InvalidUri> for PasswordUtilsError {
49    fn from(err: InvalidUri) -> PasswordUtilsError {
50        PasswordUtilsError::CommunicationWithThirdPartyApiError(format!("{:?}", err))
51    }
52}
53
54impl From<Utf8Error> for PasswordUtilsError {
55    fn from(err: Utf8Error) -> PasswordUtilsError {
56        PasswordUtilsError::ParseError(format!("{:?}", err))
57    }
58}
59
60impl From<FromHexError> for PasswordUtilsError {
61    fn from(err: FromHexError) -> PasswordUtilsError {
62        PasswordUtilsError::ParseError(format!("{:?}", err))
63    }
64}
65
66impl From<ParseIntError> for PasswordUtilsError {
67    fn from(err: ParseIntError) -> PasswordUtilsError {
68        PasswordUtilsError::ParseError(format!("{:?}", err))
69    }
70}
71
72impl From<io::Error> for PasswordUtilsError {
73    fn from(err: io::Error) -> PasswordUtilsError {
74        PasswordUtilsError::General(format!("{:?}", err))
75    }
76}