Skip to main content

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::io;
15use std::num::ParseIntError;
16use std::str::Utf8Error;
17use std::{fmt, result};
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) => {
37                write!(f, "{}", message)
38            }
39            &PasswordUtilsError::ParseError(ref message) => write!(f, "{}", message),
40        }
41    }
42}
43
44impl From<hyper::Error> for PasswordUtilsError {
45    fn from(err: hyper::Error) -> PasswordUtilsError {
46        PasswordUtilsError::CommunicationWithThirdPartyApiError(format!("{:?}", err))
47    }
48}
49
50impl From<InvalidUri> for PasswordUtilsError {
51    fn from(err: InvalidUri) -> PasswordUtilsError {
52        PasswordUtilsError::CommunicationWithThirdPartyApiError(format!("{:?}", err))
53    }
54}
55
56impl From<Utf8Error> for PasswordUtilsError {
57    fn from(err: Utf8Error) -> PasswordUtilsError {
58        PasswordUtilsError::ParseError(format!("{:?}", err))
59    }
60}
61
62impl From<FromHexError> for PasswordUtilsError {
63    fn from(err: FromHexError) -> PasswordUtilsError {
64        PasswordUtilsError::ParseError(format!("{:?}", err))
65    }
66}
67
68impl From<ParseIntError> for PasswordUtilsError {
69    fn from(err: ParseIntError) -> PasswordUtilsError {
70        PasswordUtilsError::ParseError(format!("{:?}", err))
71    }
72}
73
74impl From<io::Error> for PasswordUtilsError {
75    fn from(err: io::Error) -> PasswordUtilsError {
76        PasswordUtilsError::General(format!("{:?}", err))
77    }
78}