1use serde::Serialize;
2
3#[derive(Debug, Clone, Serialize)]
4pub struct Network {
5 pub constrained: bool,
6 pub expensive: bool,
7 pub interface: Interface,
8}
9
10#[derive(Debug, Clone, Copy, Serialize)]
11#[serde(rename_all = "lowercase")]
12pub enum Interface {
13 Wifi,
14 Cellular,
15 Wired,
16 Loopback,
17 Other,
18}
19
20#[cfg(target_os = "macos")]
21impl Network {
22 pub fn collect() -> Self {
23 use macstate_sys::nwpath::{snapshot, InterfaceType};
24 use std::time::Duration;
25
26 let Some(snap) = snapshot(Duration::from_secs(2)) else {
29 return Self {
30 constrained: false,
31 expensive: false,
32 interface: Interface::Other,
33 };
34 };
35
36 let interface = match snap.interface {
37 Some(InterfaceType::Wifi) => Interface::Wifi,
38 Some(InterfaceType::Cellular) => Interface::Cellular,
39 Some(InterfaceType::WiredEthernet) => Interface::Wired,
40 Some(InterfaceType::Loopback) => Interface::Loopback,
41 _ => Interface::Other,
42 };
43
44 Self {
45 constrained: snap.constrained,
46 expensive: snap.expensive,
47 interface,
48 }
49 }
50}
51
52#[cfg(not(target_os = "macos"))]
53impl Network {
54 pub fn collect() -> Self {
55 Self {
56 constrained: false,
57 expensive: false,
58 interface: Interface::Other,
59 }
60 }
61}