1use serde::Deserialize;
2use std::fmt;
3
4#[cfg(test)]
5#[path = "tests.rs"]
6mod tests;
7
8#[derive(Debug, Deserialize)]
10#[serde(rename_all = "camelCase")]
11pub struct PaginatedResponse<T> {
12 pub total_count: usize,
13 pub data: Vec<T>,
14}
15
16#[derive(Debug, Deserialize)]
18pub struct LegacyResponse<T> {
19 pub meta: LegacyMeta,
20 pub data: Vec<T>,
21}
22
23#[derive(Debug, Deserialize)]
24pub struct LegacyMeta {
25 pub rc: String,
26 pub msg: Option<String>,
27}
28
29#[derive(Debug, Deserialize)]
31pub struct Site {
32 pub id: String,
33}
34
35#[derive(Debug, Deserialize)]
37#[serde(rename_all = "camelCase")]
38pub struct Client {
39 #[serde(alias = "macAddress")]
40 pub mac_address: Option<String>,
41 #[serde(alias = "ipAddress")]
42 pub ip_address: Option<String>,
43 pub name: Option<String>,
44 pub hostname: Option<String>,
45 #[serde(alias = "type")]
46 pub client_type: Option<String>,
47}
48
49impl Client {
50 pub fn display_name(&self) -> &str {
51 self.name
52 .as_deref()
53 .or(self.hostname.as_deref())
54 .unwrap_or("-")
55 }
56
57 pub fn clean_name(&self) -> String {
58 let name = self.display_name();
59 strip_mac_suffix(name, self.mac_address.as_deref())
60 }
61}
62
63#[derive(Debug, Deserialize)]
65pub struct LegacyClient {
66 #[serde(rename = "_id")]
67 pub id: String,
68 pub mac: Option<String>,
69 pub ip: Option<String>,
70 pub hostname: Option<String>,
71 pub name: Option<String>,
72 #[serde(default)]
73 pub is_wired: bool,
74 #[serde(default)]
75 pub blocked: bool,
76 #[serde(default)]
77 pub fixed_ap_enabled: bool,
78 pub fixed_ap_mac: Option<String>,
79 pub uptime: Option<u64>,
80 pub tx_bytes: Option<u64>,
81 pub rx_bytes: Option<u64>,
82 pub signal: Option<i32>,
83 pub ap_mac: Option<String>,
84 #[serde(rename = "essid")]
85 pub ssid: Option<String>,
86}
87
88impl LegacyClient {
89 pub fn display_name(&self) -> &str {
90 self.name
91 .as_deref()
92 .or(self.hostname.as_deref())
93 .unwrap_or("-")
94 }
95
96 pub fn clean_name(&self) -> String {
97 let name = self.display_name();
98 strip_mac_suffix(name, self.mac.as_deref())
99 }
100}
101
102#[derive(Debug, Deserialize)]
104#[serde(rename_all = "camelCase")]
105pub struct Device {
106 pub mac_address: Option<String>,
107 pub ip_address: Option<String>,
108 pub name: Option<String>,
109 pub model: Option<String>,
110 pub state: Option<String>,
111 pub firmware_version: Option<String>,
112}
113
114#[derive(Debug, Deserialize)]
116pub struct LegacyDevice {
117 pub mac: Option<String>,
118 pub ip: Option<String>,
119 pub name: Option<String>,
120 pub model: Option<String>,
121 #[serde(rename = "type")]
122 pub device_type: Option<String>,
123 pub state: Option<u32>,
124 pub version: Option<String>,
125 pub uptime: Option<u64>,
126 pub num_sta: Option<u32>,
127 #[serde(default)]
128 pub upgradable: bool,
129 pub upgrade_to_firmware: Option<String>,
130}
131
132impl LegacyDevice {
133 pub fn state_str(&self) -> &str {
134 match self.state {
135 Some(1) => "ONLINE",
136 Some(0) => "OFFLINE",
137 Some(2) => "ADOPTING",
138 Some(4) => "UPGRADING",
139 Some(5) => "PROVISIONING",
140 _ => "UNKNOWN",
141 }
142 }
143}
144
145#[derive(Debug, Deserialize)]
147#[serde(rename_all = "camelCase")]
148pub struct Network {
149 pub name: Option<String>,
150 #[serde(default)]
151 pub enabled: bool,
152 pub vlan_id: Option<u16>,
153 #[serde(default)]
154 pub default: bool,
155}
156
157#[derive(Debug, Deserialize)]
159pub struct HealthSubsystem {
160 pub subsystem: String,
161 pub status: Option<String>,
162 pub num_sta: Option<u32>,
163 pub num_ap: Option<u32>,
164 #[serde(rename = "num_sw")]
165 pub num_switches: Option<u32>,
166 pub wan_ip: Option<String>,
167 pub isp_name: Option<String>,
168}
169
170#[derive(Debug, Deserialize)]
172pub struct SysInfo {
173 pub hostname: Option<String>,
174 pub version: Option<String>,
175 pub timezone: Option<String>,
176 pub uptime: Option<u64>,
177}
178
179#[derive(Debug, Deserialize)]
181#[serde(rename_all = "camelCase")]
182pub struct HostSystem {
183 pub device_state: Option<String>,
184 pub name: Option<String>,
185}
186
187impl HostSystem {
188 pub fn update_available(&self) -> bool {
189 self.device_state.as_deref() == Some("updateAvailable")
190 }
191}
192
193pub fn strip_mac_suffix(name: &str, mac: Option<&str>) -> String {
196 if let Some(mac) = mac {
197 let clean_mac = normalize_mac(mac);
198 if clean_mac.len() >= 4 {
200 let last4 = &clean_mac[clean_mac.len() - 4..];
201 let suffix = format!(" {}:{}", &last4[..2], &last4[2..]);
202 if let Some(stripped) = name.strip_suffix(&suffix) {
203 return stripped.to_string();
204 }
205 let suffix_no_colon = format!(" {last4}");
207 if let Some(stripped) = name.strip_suffix(&suffix_no_colon) {
208 return stripped.to_string();
209 }
210 }
211 }
212 name.to_string()
213}
214
215pub fn normalize_mac(mac: &str) -> String {
216 mac.to_lowercase().replace([':', '-'], "")
217}
218
219pub fn format_mac(mac: &str) -> String {
220 let clean = normalize_mac(mac);
221 if clean.len() != 12 {
222 return mac.to_string();
223 }
224 format!(
225 "{}:{}:{}:{}:{}:{}",
226 &clean[0..2],
227 &clean[2..4],
228 &clean[4..6],
229 &clean[6..8],
230 &clean[8..10],
231 &clean[10..12]
232 )
233}
234
235pub fn format_bytes(bytes: u64) -> String {
236 const KB: u64 = 1024;
237 const MB: u64 = KB * 1024;
238 const GB: u64 = MB * 1024;
239
240 if bytes >= GB {
241 format!("{:.1} GB", bytes as f64 / GB as f64)
242 } else if bytes >= MB {
243 format!("{:.1} MB", bytes as f64 / MB as f64)
244 } else if bytes >= KB {
245 format!("{:.1} KB", bytes as f64 / KB as f64)
246 } else {
247 format!("{bytes} B")
248 }
249}
250
251pub fn format_uptime(seconds: u64) -> String {
252 let days = seconds / 86400;
253 let hours = (seconds % 86400) / 3600;
254 let minutes = (seconds % 3600) / 60;
255
256 if days > 0 {
257 format!("{days}d {hours}h {minutes}m")
258 } else if hours > 0 {
259 format!("{hours}h {minutes}m")
260 } else {
261 format!("{minutes}m")
262 }
263}
264
265#[derive(Debug, Deserialize)]
267pub struct Event {
268 pub key: Option<String>,
269 pub msg: Option<String>,
270 pub subsystem: Option<String>,
271 pub time: Option<u64>,
272 pub datetime: Option<String>,
273}
274
275#[derive(Debug, Deserialize)]
277pub struct PortEntry {
278 pub port_idx: Option<u32>,
279 pub name: Option<String>,
280 pub media: Option<String>,
281 #[serde(default)]
282 pub up: bool,
283 pub speed: Option<u32>,
284 #[serde(default)]
285 pub full_duplex: bool,
286 #[serde(default)]
287 pub poe_enable: bool,
288 pub poe_power: Option<f64>,
289 #[serde(default)]
290 pub port_poe: bool,
291 pub tx_bytes: Option<u64>,
292 pub rx_bytes: Option<u64>,
293}
294
295#[derive(Debug, Deserialize)]
297pub struct DeviceWithPorts {
298 pub mac: Option<String>,
299 pub name: Option<String>,
300 pub model: Option<String>,
301 #[serde(default)]
302 pub port_table: Vec<PortEntry>,
303}
304
305#[derive(Debug)]
307pub enum ApiError {
308 Http(reqwest::Error),
309 Api { status: u16, message: String },
310 NotFound(String),
311 Auth(String),
312 Other(String),
313}
314
315impl fmt::Display for ApiError {
316 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
317 match self {
318 ApiError::Http(e) => {
319 let msg = e.to_string();
320 write!(f, "HTTP error: {e}")?;
321 if msg.contains("connect") || msg.contains("Connection refused") {
322 write!(
323 f,
324 "\n Hint: Check that the host is reachable and the URL is correct"
325 )?;
326 } else if msg.contains("dns") || msg.contains("resolve") {
327 write!(
328 f,
329 "\n Hint: Could not resolve hostname. Check the host value"
330 )?;
331 } else if msg.contains("timed out") || msg.contains("timeout") {
332 write!(f, "\n Hint: Request timed out. Is the controller running?")?;
333 } else if msg.contains("certificate") || msg.contains("SSL") {
334 write!(
335 f,
336 "\n Hint: TLS/certificate error. The CLI accepts self-signed certs by default"
337 )?;
338 }
339 Ok(())
340 }
341 ApiError::Api { status, message } => write!(f, "API error ({status}): {message}"),
342 ApiError::NotFound(msg) => write!(f, "Not found: {msg}"),
343 ApiError::Auth(msg) => {
344 write!(f, "Authentication error: {msg}")?;
345 write!(
346 f,
347 "\n Hint: Check your API key. Generate one in UniFi Settings > API"
348 )
349 }
350 ApiError::Other(msg) => write!(f, "{msg}"),
351 }
352 }
353}
354
355impl std::error::Error for ApiError {}
356
357impl From<reqwest::Error> for ApiError {
358 fn from(e: reqwest::Error) -> Self {
359 if e.status()
360 .is_some_and(|s| s.as_u16() == 401 || s.as_u16() == 403)
361 {
362 ApiError::Auth(e.to_string())
363 } else if e.status().is_some_and(|s| s.as_u16() == 404) {
364 ApiError::NotFound(e.to_string())
365 } else {
366 ApiError::Http(e)
367 }
368 }
369}