|
1 | | -use std::{collections::HashMap, sync::Arc}; |
| 1 | +use std::{collections::HashMap, str::FromStr, sync::Arc}; |
2 | 2 |
|
3 | | -use zvariant::{OwnedObjectPath, Value}; |
| 3 | +use strum::EnumString; |
| 4 | +use zvariant::{OwnedObjectPath, OwnedValue, Value}; |
4 | 5 |
|
5 | 6 | use zbus::{Connection, Proxy}; |
6 | 7 |
|
@@ -48,15 +49,14 @@ impl Station { |
48 | 49 | Ok(is_scanning) |
49 | 50 | } |
50 | 51 |
|
51 | | - pub async fn state(&self) -> zbus::Result<String> { |
| 52 | + pub async fn state(&self) -> zbus::Result<State> { |
52 | 53 | let proxy = self.proxy().await?; |
53 | | - let state: String = proxy.get_property("State").await?; |
54 | | - Ok(state) |
| 54 | + proxy.get_property("State").await |
55 | 55 | } |
56 | 56 |
|
57 | 57 | pub async fn connected_network(&self) -> zbus::Result<Option<Network>> { |
58 | 58 | let state = self.state().await?; |
59 | | - if state == "connected" { |
| 59 | + if matches!(state, State::Connected) { |
60 | 60 | let proxy = self.proxy().await?; |
61 | 61 | let network_path: OwnedObjectPath = proxy.get_property("ConnectedNetwork").await?; |
62 | 62 | let network = Network::new(self.connection.clone(), network_path); |
@@ -134,3 +134,22 @@ impl StationDiagnostics { |
134 | 134 | Ok(body) |
135 | 135 | } |
136 | 136 | } |
| 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