Skip to content

Commit a687d57

Browse files
authored
Enum Types for NetworkType, station::State (#8)
* Enum types for station state, network type. * fix typo in file name of `known_network`
1 parent af67d98 commit a687d57

File tree

6 files changed

+60
-31
lines changed

6 files changed

+60
-31
lines changed

src/known_netowk.rs renamed to src/known_network.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use std::sync::Arc;
33
use zbus::{Connection, Proxy, Result};
44
use zvariant::OwnedObjectPath;
55

6+
use crate::network::NetworkType;
7+
68
#[derive(Clone, Debug)]
79
pub struct KnownNetwork {
810
pub(crate) connection: Arc<Connection>,
@@ -39,10 +41,9 @@ impl KnownNetwork {
3941
Ok(name)
4042
}
4143

42-
pub async fn network_type(&self) -> Result<String> {
44+
pub async fn network_type(&self) -> Result<NetworkType> {
4345
let proxy = self.proxy().await?;
44-
let network_type: String = proxy.get_property("Type").await?;
45-
Ok(network_type)
46+
proxy.get_property("Type").await
4647
}
4748

4849
pub async fn hidden(&self) -> Result<bool> {

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub mod agent;
1010

1111
pub mod network;
1212

13-
pub mod known_netowk;
13+
pub mod known_network;
1414

1515
pub mod access_point;
1616

src/modes.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use std::fmt::Display;
22

3-
#[non_exhaustive]
4-
#[derive(Debug, Clone, PartialEq)]
3+
use strum::EnumString;
4+
5+
#[derive(Debug, Clone, Copy, PartialEq, EnumString)]
6+
#[strum(ascii_case_insensitive)]
57
pub enum Mode {
68
Station,
79
Ap,
@@ -15,15 +17,3 @@ impl Display for Mode {
1517
}
1618
}
1719
}
18-
19-
impl TryFrom<&str> for Mode {
20-
type Error = &'static str;
21-
22-
fn try_from(value: &str) -> Result<Self, Self::Error> {
23-
match value {
24-
"station" => Ok(Mode::Station),
25-
"ap" => Ok(Mode::Ap),
26-
_ => Err("Unknown mode"),
27-
}
28-
}
29-
}

src/network.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
use std::sync::Arc;
1+
use std::{str::FromStr, sync::Arc};
22

3+
use strum::EnumString;
34
use zbus::{Connection, Proxy, Result as ZbusResult};
4-
use zvariant::OwnedObjectPath;
5+
use zvariant::{OwnedObjectPath, OwnedValue};
56

67
use crate::{
78
device::Device,
89
error::{IWDError, network::ConnectError},
9-
known_netowk::KnownNetwork,
10+
known_network::KnownNetwork,
1011
};
1112

1213
#[derive(Clone, Debug)]
@@ -62,10 +63,9 @@ impl Network {
6263
Ok(device)
6364
}
6465

65-
pub async fn network_type(&self) -> ZbusResult<String> {
66+
pub async fn network_type(&self) -> ZbusResult<NetworkType> {
6667
let proxy = self.proxy().await?;
67-
let network_type: String = proxy.get_property("Type").await?;
68-
Ok(network_type)
68+
proxy.get_property("Type").await
6969
}
7070

7171
pub async fn known_network(&self) -> ZbusResult<Option<KnownNetwork>> {
@@ -78,3 +78,22 @@ impl Network {
7878
Ok(None)
7979
}
8080
}
81+
82+
#[derive(Debug, Clone, Copy, PartialEq, EnumString)]
83+
#[strum(ascii_case_insensitive)]
84+
pub enum NetworkType {
85+
Open,
86+
Wep,
87+
Psk,
88+
#[strum(serialize = "8021x")]
89+
Eap,
90+
}
91+
92+
impl TryFrom<OwnedValue> for NetworkType {
93+
type Error = zvariant::Error;
94+
95+
fn try_from(value: OwnedValue) -> Result<Self, Self::Error> {
96+
let state_string: String = value.try_into()?;
97+
Self::from_str(&state_string).map_err(|_| zvariant::Error::IncorrectType)
98+
}
99+
}

src/session.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
adapter::Adapter,
44
agent::{Agent, AgentManager},
55
device::Device,
6-
known_netowk::KnownNetwork,
6+
known_network::KnownNetwork,
77
station::{Station, StationDiagnostics},
88
};
99
use std::{collections::HashMap, sync::Arc};

src/station.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use std::{collections::HashMap, sync::Arc};
1+
use std::{collections::HashMap, str::FromStr, sync::Arc};
22

3-
use zvariant::{OwnedObjectPath, Value};
3+
use strum::EnumString;
4+
use zvariant::{OwnedObjectPath, OwnedValue, Value};
45

56
use zbus::{Connection, Proxy};
67

@@ -48,15 +49,14 @@ impl Station {
4849
Ok(is_scanning)
4950
}
5051

51-
pub async fn state(&self) -> zbus::Result<String> {
52+
pub async fn state(&self) -> zbus::Result<State> {
5253
let proxy = self.proxy().await?;
53-
let state: String = proxy.get_property("State").await?;
54-
Ok(state)
54+
proxy.get_property("State").await
5555
}
5656

5757
pub async fn connected_network(&self) -> zbus::Result<Option<Network>> {
5858
let state = self.state().await?;
59-
if state == "connected" {
59+
if matches!(state, State::Connected) {
6060
let proxy = self.proxy().await?;
6161
let network_path: OwnedObjectPath = proxy.get_property("ConnectedNetwork").await?;
6262
let network = Network::new(self.connection.clone(), network_path);
@@ -134,3 +134,22 @@ impl StationDiagnostics {
134134
Ok(body)
135135
}
136136
}
137+
138+
#[derive(Debug, Clone, Copy, PartialEq, EnumString)]
139+
#[strum(ascii_case_insensitive)]
140+
pub enum State {
141+
Connected,
142+
Disconnected,
143+
Connecting,
144+
Disconnecting,
145+
Roaming,
146+
}
147+
148+
impl TryFrom<OwnedValue> for State {
149+
type Error = zvariant::Error;
150+
151+
fn try_from(value: OwnedValue) -> Result<Self, Self::Error> {
152+
let state_string: String = value.try_into()?;
153+
Self::from_str(&state_string).map_err(|_| zvariant::Error::IncorrectType)
154+
}
155+
}

0 commit comments

Comments
 (0)