Skip to content

Commit 9cecf12

Browse files
committed
feat(#121): add DeviceType::Vlan variant and is_vlan() helper
1 parent 9fc43f2 commit 9cecf12

3 files changed

Lines changed: 23 additions & 5 deletions

File tree

nmrs/src/api/models/device.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ pub enum DeviceType {
144144
Loopback,
145145
/// Bluetooth
146146
Bluetooth,
147+
/// VLAN (802.1Q) virtual device.
148+
Vlan,
147149
/// Unknown or unsupported device type with raw code.
148150
///
149151
/// Use the methods on `DeviceType` to query capabilities of unknown device types,
@@ -208,6 +210,7 @@ impl DeviceType {
208210
Self::WifiP2P => "wifi-p2p",
209211
Self::Loopback => "loopback",
210212
Self::Bluetooth => "bluetooth",
213+
Self::Vlan => "vlan",
211214
Self::Other(code) => {
212215
crate::types::device_type_registry::connection_type_for_code(*code)
213216
.unwrap_or("generic")
@@ -224,6 +227,7 @@ impl DeviceType {
224227
Self::WifiP2P => 30,
225228
Self::Loopback => 32,
226229
Self::Bluetooth => 6,
230+
Self::Vlan => 11,
227231
Self::Other(code) => *code,
228232
}
229233
}
@@ -308,6 +312,12 @@ impl Device {
308312
pub fn is_loopback(&self) -> bool {
309313
matches!(self.device_type, DeviceType::Loopback)
310314
}
315+
316+
/// Returns `true` if this is a VLAN (802.1Q) device.
317+
#[must_use]
318+
pub fn is_vlan(&self) -> bool {
319+
matches!(self.device_type, DeviceType::Vlan)
320+
}
311321
}
312322

313323
impl Display for Device {
@@ -326,6 +336,7 @@ impl From<u32> for DeviceType {
326336
1 => DeviceType::Ethernet,
327337
2 => DeviceType::Wifi,
328338
5 => DeviceType::Bluetooth,
339+
11 => DeviceType::Vlan,
329340
30 => DeviceType::WifiP2P,
330341
32 => DeviceType::Loopback,
331342
v => DeviceType::Other(v),
@@ -361,6 +372,7 @@ impl Display for DeviceType {
361372
DeviceType::WifiP2P => write!(f, "Wi-Fi P2P"),
362373
DeviceType::Loopback => write!(f, "Loopback"),
363374
DeviceType::Bluetooth => write!(f, "Bluetooth"),
375+
DeviceType::Vlan => write!(f, "VLAN"),
364376
DeviceType::Other(v) => write!(
365377
f,
366378
"{}",

nmrs/src/api/models/tests.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::api::models::DeviceType;
1818
fn device_type_from_u32_all_variants() {
1919
assert_eq!(DeviceType::from(1), DeviceType::Ethernet);
2020
assert_eq!(DeviceType::from(2), DeviceType::Wifi);
21+
assert_eq!(DeviceType::from(11), DeviceType::Vlan);
2122
assert_eq!(DeviceType::from(30), DeviceType::WifiP2P);
2223
assert_eq!(DeviceType::from(32), DeviceType::Loopback);
2324
assert_eq!(DeviceType::from(999), DeviceType::Other(999));
@@ -26,7 +27,9 @@ fn device_type_from_u32_all_variants() {
2627

2728
#[test]
2829
fn device_type_from_u32_registry_types() {
29-
assert_eq!(DeviceType::from(11), DeviceType::Other(11));
30+
// VLAN is now a first-class variant
31+
assert_eq!(DeviceType::from(11), DeviceType::Vlan);
32+
// These still fall through to Other since they're only in the registry
3033
assert_eq!(DeviceType::from(12), DeviceType::Other(12));
3134
assert_eq!(DeviceType::from(13), DeviceType::Other(13));
3235
assert_eq!(DeviceType::from(16), DeviceType::Other(16));
@@ -39,6 +42,7 @@ fn device_type_display() {
3942
assert_eq!(format!("{}", DeviceType::Wifi), "Wi-Fi");
4043
assert_eq!(format!("{}", DeviceType::WifiP2P), "Wi-Fi P2P");
4144
assert_eq!(format!("{}", DeviceType::Loopback), "Loopback");
45+
assert_eq!(format!("{}", DeviceType::Vlan), "VLAN");
4246
assert_eq!(format!("{}", DeviceType::Other(42)), "Other(42)");
4347
}
4448

@@ -100,6 +104,7 @@ fn device_type_connection_type_str() {
100104
assert_eq!(DeviceType::Wifi.connection_type_str(), "802-11-wireless");
101105
assert_eq!(DeviceType::WifiP2P.connection_type_str(), "wifi-p2p");
102106
assert_eq!(DeviceType::Loopback.connection_type_str(), "loopback");
107+
assert_eq!(DeviceType::Vlan.connection_type_str(), "vlan");
103108
}
104109

105110
#[test]
@@ -114,6 +119,7 @@ fn device_type_connection_type_str_registry() {
114119
fn device_type_to_code() {
115120
assert_eq!(DeviceType::Ethernet.to_code(), 1);
116121
assert_eq!(DeviceType::Wifi.to_code(), 2);
122+
assert_eq!(DeviceType::Vlan.to_code(), 11);
117123
assert_eq!(DeviceType::WifiP2P.to_code(), 30);
118124
assert_eq!(DeviceType::Loopback.to_code(), 32);
119125
assert_eq!(DeviceType::Other(999).to_code(), 999);

nmrs/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,10 @@ pub use api::models::{
354354
EapMethod, EapOptions, Network, NetworkInfo, OpenVpnAuthType, OpenVpnCompression,
355355
OpenVpnConfig, OpenVpnConnectionType, OpenVpnProxy, Phase2, RadioState, SavedConnection,
356356
SavedConnectionBrief, SecurityFeatures, SettingsPatch, SettingsSummary, StateReason,
357-
TimeoutConfig, VpnConfig, VpnConfiguration, VpnConnection, VpnConnectionInfo, VpnCredentials,
358-
VpnDetails, VpnKind, VpnRoute, VpnSecretFlags, VpnType, WifiDevice, WifiKeyMgmt, WifiSecurity,
359-
WifiSecuritySummary, WireGuardConfig, WireGuardPeer, connection_state_reason_to_error,
360-
reason_to_error,
357+
TimeoutConfig, VlanConfig, VpnConfig, VpnConfiguration, VpnConnection, VpnConnectionInfo,
358+
VpnCredentials, VpnDetails, VpnKind, VpnRoute, VpnSecretFlags, VpnType, WifiDevice,
359+
WifiKeyMgmt, WifiSecurity, WifiSecuritySummary, WireGuardConfig, WireGuardPeer,
360+
connection_state_reason_to_error, reason_to_error,
361361
};
362362
pub use api::network_manager::NetworkManager;
363363
pub use api::wifi_scope::WifiScope;

0 commit comments

Comments
 (0)