Skip to main content

chirpstack_api/
common.rs

1use std::error::Error;
2use std::fmt;
3use std::str::FromStr;
4
5include!(concat!(env!("OUT_DIR"), "/common/common.rs"));
6#[cfg(feature = "json")]
7include!(concat!(env!("OUT_DIR"), "/common/common.serde.rs"));
8
9#[allow(clippy::from_over_into)]
10impl Into<String> for FType {
11    fn into(self) -> String {
12        match self {
13            FType::JoinRequest => "JoinRequest",
14            FType::JoinAccept => "JoinAccept",
15            FType::UnconfirmedDataUp => "UnconfirmedDataUp",
16            FType::UnconfirmedDataDown => "UnconfirmedDataDown",
17            FType::ConfirmedDataUp => "ConfirmedDataUp",
18            FType::ConfirmedDataDown => "ConfirmedDataDown",
19            FType::RejoinRequest => "RejoinRequest",
20            FType::Proprietary => "Proprietary",
21        }
22        .to_string()
23    }
24}
25
26#[allow(clippy::from_over_into)]
27impl Into<String> for Region {
28    fn into(self) -> String {
29        match self {
30            Region::Eu868 => "EU868",
31            Region::Us915 => "US915",
32            Region::Cn779 => "CN779",
33            Region::Eu433 => "EU433",
34            Region::Au915 => "AU915",
35            Region::Cn470 => "CN470",
36            Region::As923 => "AS923",
37            Region::As9232 => "AS923_2",
38            Region::As9233 => "AS923_3",
39            Region::As9234 => "AS923_4",
40            Region::Kr920 => "KR920",
41            Region::In865 => "IN865",
42            Region::Ru864 => "RU864",
43            Region::Ism2400 => "ISM2400",
44        }
45        .to_string()
46    }
47}
48
49impl FromStr for Region {
50    type Err = Box<dyn Error>;
51
52    fn from_str(s: &str) -> Result<Self, Box<dyn Error>> {
53        Ok(match s {
54            "EU868" => Region::Eu868,
55            "US915" => Region::Us915,
56            "CN779" => Region::Cn779,
57            "EU433" => Region::Eu433,
58            "AU915" => Region::Au915,
59            "CN470" => Region::Cn470,
60            "AS923" => Region::As923,
61            "AS923_2" => Region::As9232,
62            "AS923_3" => Region::As9233,
63            "AS923_4" => Region::As9234,
64            "KR920" => Region::Kr920,
65            "IN865" => Region::In865,
66            "RU864" => Region::Ru864,
67            "ISM2400" => Region::Ism2400,
68            _ => {
69                return Err("invalid region".into());
70            }
71        })
72    }
73}
74
75#[allow(clippy::from_over_into)]
76impl Into<String> for MacVersion {
77    fn into(self) -> String {
78        match self {
79            MacVersion::Lorawan100 => "1.0.0",
80            MacVersion::Lorawan101 => "1.0.1",
81            MacVersion::Lorawan102 => "1.0.2",
82            MacVersion::Lorawan103 => "1.0.3",
83            MacVersion::Lorawan104 => "1.0.4",
84            MacVersion::Lorawan110 => "1.1.0",
85        }
86        .to_string()
87    }
88}
89
90impl fmt::Display for MacVersion {
91    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92        let s: String = (*self).into();
93        write!(f, "{}", s)
94    }
95}
96
97impl FromStr for MacVersion {
98    type Err = Box<dyn Error>;
99
100    fn from_str(s: &str) -> Result<Self, Box<dyn Error>> {
101        Ok(match s {
102            "1.0.0" => MacVersion::Lorawan100,
103            "1.0.1" => MacVersion::Lorawan101,
104            "1.0.2" => MacVersion::Lorawan102,
105            "1.0.3" => MacVersion::Lorawan103,
106            "1.0.4" => MacVersion::Lorawan104,
107            "1.1.0" => MacVersion::Lorawan110,
108            _ => {
109                return Err("invalid mac-version".into());
110            }
111        })
112    }
113}
114
115#[allow(clippy::from_over_into)]
116impl Into<String> for RegParamsRevision {
117    fn into(self) -> String {
118        match self {
119            RegParamsRevision::A => "A",
120            RegParamsRevision::B => "B",
121            RegParamsRevision::Rp002100 => "RP002_1.0.0",
122            RegParamsRevision::Rp002101 => "RP002_1.0.1",
123            RegParamsRevision::Rp002102 => "RP002_1.0.2",
124            RegParamsRevision::Rp002103 => "RP002_1.0.3",
125            RegParamsRevision::Rp002104 => "RP002_1.0.4",
126            RegParamsRevision::Rp002105 => "RP002_1.0.5",
127        }
128        .to_string()
129    }
130}
131
132impl FromStr for RegParamsRevision {
133    type Err = Box<dyn Error>;
134
135    fn from_str(s: &str) -> Result<Self, Box<dyn Error>> {
136        Ok(match s {
137            "A" => RegParamsRevision::A,
138            "B" => RegParamsRevision::B,
139            "RP002_1.0.0" => RegParamsRevision::Rp002100,
140            "RP002_1.0.1" => RegParamsRevision::Rp002101,
141            "RP002_1.0.2" => RegParamsRevision::Rp002102,
142            "RP002_1.0.3" => RegParamsRevision::Rp002103,
143            "RP002_1.0.4" => RegParamsRevision::Rp002104,
144            "RP002_1.0.5" => RegParamsRevision::Rp002105,
145            _ => {
146                return Err("invalid reg param revision".into());
147            }
148        })
149    }
150}