hamlib_client/
error.rs

1/*
2 * Copyright (C) 2024 Luca Cireddu <sardylan@gmail.com>
3 *
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License as published by the Free Software
6 * Foundation, version 3.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License along with
13 * this program. If not, see <https://www.gnu.org/licenses/>.
14 *
15 */
16
17use std::fmt;
18use std::fmt::{Display, Formatter};
19use std::io::Error;
20use std::string::FromUtf8Error;
21
22#[derive(Debug)]
23pub enum RigCtlError {
24    ConnectionError(String),
25    AlreadyConnected,
26    RawDataError(String),
27    ResponseParsing(String),
28    CommunicationTimeout,
29}
30
31impl Display for RigCtlError {
32    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
33        match self {
34            RigCtlError::ConnectionError(message) => { write!(f, "Connection error: {}", message) }
35            RigCtlError::AlreadyConnected => { write!(f, "Already connected") }
36            RigCtlError::RawDataError(message) => { write!(f, "Raw data error: {}", message) }
37            RigCtlError::ResponseParsing(message) => { write!(f, "Response parsing error: {}", message) }
38            RigCtlError::CommunicationTimeout => { write!(f, "Communication timeout") }
39        }
40    }
41}
42
43impl From<Error> for RigCtlError {
44    fn from(value: Error) -> Self {
45        RigCtlError::ConnectionError(value.to_string())
46    }
47}
48
49impl From<FromUtf8Error> for RigCtlError {
50    fn from(value: FromUtf8Error) -> Self {
51        RigCtlError::RawDataError(value.to_string())
52    }
53}