use std::fmt;
use std::net::Ipv4Addr;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub enum IpType {
None,
#[default]
Ipv4,
Ipv6,
Ipv4v6,
Any,
}
impl IpType {
#[must_use]
pub const fn from_raw(value: u32) -> Self {
match value {
1 => Self::Ipv4,
2 => Self::Ipv6,
4 => Self::Ipv4v6,
u32::MAX => Self::Any,
_ => Self::None,
}
}
#[must_use]
pub const fn as_raw(self) -> u32 {
match self {
Self::None => 0,
Self::Ipv4 => 1,
Self::Ipv6 => 2,
Self::Ipv4v6 => 4,
Self::Any => u32::MAX,
}
}
#[must_use]
pub const fn is_dual_stack(self) -> bool {
matches!(self, Self::Ipv4v6)
}
}
impl From<u32> for IpType {
fn from(value: u32) -> Self {
Self::from_raw(value)
}
}
impl fmt::Display for IpType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
Self::None => "none",
Self::Ipv4 => "ipv4",
Self::Ipv6 => "ipv6",
Self::Ipv4v6 => "ipv4v6",
Self::Any => "any",
};
f.write_str(s)
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Ip4Config {
pub method: String,
pub address: Option<Ipv4Addr>,
pub prefix: u32,
pub gateway: Option<Ipv4Addr>,
pub dns: Vec<Ipv4Addr>,
pub mtu: Option<u32>,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct BearerStats {
pub rx_bytes: u64,
pub tx_bytes: u64,
pub duration_seconds: u32,
pub attempts: u32,
pub failed_attempts: u32,
pub total_duration_seconds: u32,
pub total_rx_bytes: u64,
pub total_tx_bytes: u64,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Bearer {
pub path: String,
pub interface: String,
pub connected: bool,
pub ip4_config: Option<Ip4Config>,
pub stats: BearerStats,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BearerConfig {
pub apn: String,
pub ip_type: IpType,
pub user: Option<String>,
pub password: Option<String>,
pub allow_roaming: bool,
}
impl BearerConfig {
pub fn new(apn: impl Into<String>) -> Self {
Self {
apn: apn.into(),
ip_type: IpType::Ipv4,
user: None,
password: None,
allow_roaming: false,
}
}
#[must_use]
pub fn with_ip_type(mut self, ip_type: IpType) -> Self {
self.ip_type = ip_type;
self
}
#[must_use]
pub fn with_user(mut self, user: impl Into<String>) -> Self {
self.user = Some(user.into());
self
}
#[must_use]
pub fn with_password(mut self, password: impl Into<String>) -> Self {
self.password = Some(password.into());
self
}
#[must_use]
pub fn with_allow_roaming(mut self, allow: bool) -> Self {
self.allow_roaming = allow;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ip_type_round_trip() {
for variant in [
IpType::None,
IpType::Ipv4,
IpType::Ipv6,
IpType::Ipv4v6,
IpType::Any,
] {
assert_eq!(IpType::from_raw(variant.as_raw()), variant);
}
}
#[test]
fn ip_type_default_is_ipv4() {
assert_eq!(IpType::default(), IpType::Ipv4);
}
#[test]
fn ip_type_unknown_maps_to_none() {
assert_eq!(IpType::from_raw(99), IpType::None);
}
#[test]
fn ip_type_dual_stack_predicate() {
assert!(IpType::Ipv4v6.is_dual_stack());
assert!(!IpType::Ipv4.is_dual_stack());
assert!(!IpType::Any.is_dual_stack());
}
#[test]
fn bearer_config_new_has_defaults() {
let cfg = BearerConfig::new("hologram");
assert_eq!(cfg.apn, "hologram");
assert_eq!(cfg.ip_type, IpType::Ipv4);
assert!(cfg.user.is_none());
assert!(cfg.password.is_none());
assert!(!cfg.allow_roaming);
}
#[test]
fn bearer_config_builders_set_fields() {
let cfg = BearerConfig::new("internet")
.with_user("u")
.with_password("p")
.with_ip_type(IpType::Ipv4v6)
.with_allow_roaming(true);
assert_eq!(cfg.user.as_deref(), Some("u"));
assert_eq!(cfg.password.as_deref(), Some("p"));
assert_eq!(cfg.ip_type, IpType::Ipv4v6);
assert!(cfg.allow_roaming);
}
#[test]
fn bearer_stats_default_is_zeroed() {
let stats = BearerStats::default();
assert_eq!(stats.rx_bytes, 0);
assert_eq!(stats.tx_bytes, 0);
assert_eq!(stats.duration_seconds, 0);
assert_eq!(stats.total_rx_bytes, 0);
}
#[test]
fn ip4_config_default_is_empty() {
let cfg = Ip4Config::default();
assert!(cfg.method.is_empty());
assert!(cfg.address.is_none());
assert_eq!(cfg.prefix, 0);
assert!(cfg.dns.is_empty());
}
}