serial_unit_testing/
error.rs

1/*
2 * File: src/error.rs
3 * Date: 11.11.2019
4 * Author: MarkAtk
5 *
6 * MIT License
7 *
8 * Copyright (c) 2019 MarkAtk
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy of
11 * this software and associated documentation files (the "Software"), to deal in
12 * the Software without restriction, including without limitation the rights to
13 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
14 * of the Software, and to permit persons to whom the Software is furnished to do
15 * so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in all
18 * copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29use std::error;
30use std::fmt;
31use std::io;
32use std::num;
33use std::str;
34use std::convert::From;
35use serialport;
36
37pub type Result<T> = std::result::Result<T, Error>;
38
39#[derive(Debug)]
40pub enum Error {
41    Io(io::Error),
42    Serial(serialport::Error),
43    Num(num::ParseIntError),
44    Utf8(str::Utf8Error),
45    Other
46}
47
48impl Error {
49    pub fn is_timeout(&self) -> bool {
50        match *self {
51            Error::Io(ref e) if e.kind() == io::ErrorKind::TimedOut => true,
52            _ => false
53        }
54    }
55}
56
57impl fmt::Display for Error {
58    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59        match *self {
60            Error::Io(ref cause) => write!(f, "I/O Error: {}", cause),
61            Error::Serial(ref cause) => write!(f, "Serial Error: {}", cause),
62            Error::Num(ref cause) => write!(f, "Byte Parse Error: {}", cause),
63            Error::Utf8(ref cause) => write!(f, "String Parse Error: {}", cause),
64            Error::Other => write!(f, "Unknown error")
65        }
66    }
67}
68
69impl error::Error for Error {
70    fn cause(&self) -> Option<&dyn error::Error> {
71        match *self {
72            Error::Io(ref cause) => Some(cause),
73            Error::Serial(ref cause) => Some(cause),
74            Error::Num(ref cause) => Some(cause),
75            Error::Utf8(ref cause) => Some(cause),
76            Error::Other => None
77        }
78    }
79}
80
81impl From<io::Error> for Error {
82    fn from(cause: io::Error) -> Error {
83        Error::Io(cause)
84    }
85}
86
87impl From<serialport::Error> for Error {
88    fn from(cause: serialport::Error) -> Error {
89        match cause.kind() {
90            serialport::ErrorKind::Io(_) => Error::Io(io::Error::from(cause)),
91            _ => Error::Serial(cause)
92        }
93    }
94}
95
96impl From<num::ParseIntError> for Error {
97    fn from(cause: num::ParseIntError) -> Error {
98        Error::Num(cause)
99    }
100}
101
102impl From<str::Utf8Error> for Error {
103    fn from(cause: str::Utf8Error) -> Error {
104        Error::Utf8(cause)
105    }
106}