Skip to main content

open_net/api/net_status/
network_status.rs

1use std::fmt;
2
3/// Current host-level network reachability reported by the monitor.
4///
5/// Evidence differs by platform: Windows uses Network List Manager's Internet
6/// connectivity flags; macOS uses netwatch's default-route and IP-capability
7/// facts; Linux checks operational interfaces and default routes. These are
8/// host observations, not probes of a particular server, DNS service, or URL.
9/// Always handle errors from the actual network operation.
10///
11/// An absent `NetworkSnapshot::reachability` means no current observation, not
12/// `Unavailable`. `Stopped`, `Starting`, `Failed`, and `Closed` describe the
13/// monitor's lifecycle and must not be converted into a network disconnection.
14#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15pub enum NetworkStatus {
16    /// The platform observation does not establish host network availability.
17    /// This does not imply that every local or remote endpoint is unreachable.
18    Unavailable,
19    /// The platform observation indicates host network availability.
20    /// A request can still fail because of DNS, TLS, a firewall, or the server.
21    Available,
22}
23
24impl NetworkStatus {
25    /// The variant name as a static string (e.g. `"Available"`).
26    ///
27    /// Used both by the [`fmt::Display`] implementation and as a stable,
28    /// human-readable label when forwarding the status to logging.
29    pub const fn name(&self) -> &'static str {
30        match self {
31            NetworkStatus::Unavailable => "Unavailable",
32            NetworkStatus::Available => "Available",
33        }
34    }
35}
36
37impl fmt::Display for NetworkStatus {
38    /// Renders the variant name, so `to_string()` yields `"Available"` /
39    /// `"Unavailable"` rather than a structural form.
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        f.write_str(self.name())
42    }
43}