use serde::Serialize;
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct Network {
pub constrained: bool,
pub expensive: bool,
pub interface: Interface,
}
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "lowercase")]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "schema", schemars(rename_all = "lowercase"))]
#[cfg_attr(
feature = "schema",
schemars(description = "Primary interface kind reported by NWPathMonitor for the current path. `other` is reported when the path uses none of the well-known kinds (e.g. unknown VPN).")
)]
pub enum Interface {
Wifi,
Cellular,
Wired,
Loopback,
Other,
}
#[cfg(target_os = "macos")]
impl Network {
pub fn collect() -> Self {
use macstate_sys::nwpath::{snapshot, InterfaceType};
use std::time::Duration;
let Some(snap) = snapshot(Duration::from_secs(2)) else {
return Self {
constrained: false,
expensive: false,
interface: Interface::Other,
};
};
let interface = match snap.interface {
Some(InterfaceType::Wifi) => Interface::Wifi,
Some(InterfaceType::Cellular) => Interface::Cellular,
Some(InterfaceType::WiredEthernet) => Interface::Wired,
Some(InterfaceType::Loopback) => Interface::Loopback,
_ => Interface::Other,
};
Self {
constrained: snap.constrained,
expensive: snap.expensive,
interface,
}
}
}
#[cfg(not(target_os = "macos"))]
impl Network {
pub fn collect() -> Self {
Self {
constrained: false,
expensive: false,
interface: Interface::Other,
}
}
}