hamlib_client/
mode.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 crate::adif;
18use crate::error::RigCtlError;
19use std::fmt;
20use std::fmt::{Display, Formatter};
21use std::str::FromStr;
22
23#[derive(Debug, Copy, Clone, PartialEq, Eq)]
24pub enum Mode {
25    USB,
26    LSB,
27    CW,
28    CWR,
29    RTTY,
30    RTTYR,
31    AM,
32    FM,
33    WFM,
34    AMS,
35    PKTLSB,
36    PKTUSB,
37    PKTFM,
38    ECSSUSB,
39    ECSSLSB,
40    FAX,
41    SAM,
42    SAL,
43    SAH,
44    DSB,
45}
46
47impl Display for Mode {
48    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
49        match self {
50            Mode::USB => write!(f, "USB"),
51            Mode::LSB => write!(f, "LSB"),
52            Mode::CW => write!(f, "CW"),
53            Mode::CWR => write!(f, "CWR"),
54            Mode::RTTY => write!(f, "RTTY"),
55            Mode::RTTYR => write!(f, "RTTYR"),
56            Mode::AM => write!(f, "AM"),
57            Mode::FM => write!(f, "FM"),
58            Mode::WFM => write!(f, "WFM"),
59            Mode::AMS => write!(f, "AMS"),
60            Mode::PKTLSB => write!(f, "PKTLSB"),
61            Mode::PKTUSB => write!(f, "PKTUSB"),
62            Mode::PKTFM => write!(f, "PKTFM"),
63            Mode::ECSSUSB => write!(f, "ECSSUSB"),
64            Mode::ECSSLSB => write!(f, "ECSSLSB"),
65            Mode::FAX => write!(f, "FAX"),
66            Mode::SAM => write!(f, "SAM"),
67            Mode::SAL => write!(f, "SAL"),
68            Mode::SAH => write!(f, "SAH"),
69            Mode::DSB => write!(f, "DSB"),
70        }
71    }
72}
73
74impl FromStr for Mode {
75    type Err = RigCtlError;
76
77    fn from_str(s: &str) -> Result<Self, Self::Err> {
78        match s {
79            "USB" => Ok(Mode::USB),
80            "LSB" => Ok(Mode::LSB),
81            "CW" => Ok(Mode::CW),
82            "CWR" => Ok(Mode::CWR),
83            "RTTY" => Ok(Mode::RTTY),
84            "RTTYR" => Ok(Mode::RTTYR),
85            "AM" => Ok(Mode::AM),
86            "FM" => Ok(Mode::FM),
87            "WFM" => Ok(Mode::WFM),
88            "AMS" => Ok(Mode::AMS),
89            "PKTLSB" => Ok(Mode::PKTLSB),
90            "PKTUSB" => Ok(Mode::PKTUSB),
91            "PKTFM" => Ok(Mode::PKTFM),
92            "ECSSUSB" => Ok(Mode::ECSSUSB),
93            "ECSSLSB" => Ok(Mode::ECSSLSB),
94            "FAX" => Ok(Mode::FAX),
95            "SAM" => Ok(Mode::SAM),
96            "SAL" => Ok(Mode::SAL),
97            "SAH" => Ok(Mode::SAH),
98            "DSB" => Ok(Mode::DSB),
99            _ => Err(RigCtlError::RawDataError(format!(
100                "Unable to parse Mode with string \"{}\"",
101                &s
102            ))),
103        }
104    }
105}
106
107impl From<Mode> for adif::Mode {
108    fn from(value: Mode) -> Self {
109        match value {
110            Mode::USB => Self::SSB,
111            Mode::LSB => Self::SSB,
112            Mode::CW => Self::CW,
113            Mode::CWR => Self::CW,
114            Mode::RTTY => Self::RTTY,
115            Mode::RTTYR => Self::RTTY,
116            Mode::AM => Self::AM,
117            Mode::FM => Self::FM,
118            Mode::WFM => Self::FM,
119            Mode::AMS => Self::AM,
120            Mode::PKTLSB => Self::PKT,
121            Mode::PKTUSB => Self::PKT,
122            Mode::PKTFM => Self::PKT,
123            Mode::ECSSUSB => Self::AM,
124            Mode::ECSSLSB => Self::AM,
125            Mode::FAX => Self::FAX,
126            Mode::SAM => Self::AM,
127            Mode::SAL => Self::AM,
128            Mode::SAH => Self::AM,
129            Mode::DSB => Self::AM,
130        }
131    }
132}