Skip to content

Commit b372594

Browse files
committed
exit properly when no device is not found
1 parent 199a992 commit b372594

File tree

6 files changed

+103
-64
lines changed

6 files changed

+103
-64
lines changed

Cargo.lock

Lines changed: 55 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ serde = { version = "1", features = ["derive"] }
2525
toml = { version = "0.9" }
2626
clap = { version = "4", features = ["derive", "cargo"] }
2727
anyhow = "1"
28-
iwdrs = "0.2.5"
28+
iwdrs = "0.2.6"
2929
chrono = "0.4"
3030
log = "0.4"
3131
env_logger = "0.11"

src/adapter.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ impl Adapter {
2828
pub async fn new(session: Arc<Session>, config: Arc<Config>) -> Result<Self> {
2929
let adapter = session
3030
.adapters()
31-
.await
32-
.unwrap()
31+
.await?
3332
.pop()
3433
.context("No adapter found")?;
3534

src/device.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use anyhow::Context;
12
use anyhow::Result;
23
use std::sync::Arc;
34

@@ -31,7 +32,7 @@ pub struct Device {
3132

3233
impl Device {
3334
pub async fn new(session: Arc<Session>) -> Result<Self> {
34-
let device = session.devices().await.unwrap().pop().unwrap();
35+
let device = session.devices().await?.pop().context("No device found")?;
3536
let name = device.name().await?;
3637
let address = device.address().await?;
3738
let mode = device.get_mode().await?;

src/mode/ap.rs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::{Result, anyhow};
1+
use anyhow::{Context, Result};
22
use std::sync::{Arc, atomic::AtomicBool};
33

44
use iwdrs::session::Session;
@@ -49,11 +49,10 @@ impl AccessPoint {
4949
pub async fn new(session: Arc<Session>) -> Result<Self> {
5050
let iwd_access_point = session
5151
.access_points()
52-
.await
53-
.unwrap()
52+
.await?
5453
.pop()
55-
.ok_or(anyhow!("no ap found"))?;
56-
let iwd_access_point_diagnostic = session.access_points_diagnostics().await.unwrap().pop();
54+
.context("no ap found")?;
55+
let iwd_access_point_diagnostic = session.access_points_diagnostics().await?.pop();
5756

5857
let has_started = iwd_access_point.has_started().await?;
5958
let name = iwd_access_point.name().await?;
@@ -217,13 +216,13 @@ impl AccessPoint {
217216
}
218217

219218
pub async fn refresh(&mut self) -> Result<()> {
220-
let iwd_access_point = self.session.access_points().await.unwrap().pop().unwrap();
221-
let iwd_access_point_diagnostic = self
219+
let iwd_access_point = self
222220
.session
223-
.access_points_diagnostics()
224-
.await
225-
.unwrap()
226-
.pop();
221+
.access_points()
222+
.await?
223+
.pop()
224+
.context("No AP found")?;
225+
let iwd_access_point_diagnostic = self.session.access_points_diagnostics().await?.pop();
227226

228227
self.has_started = iwd_access_point.has_started().await?;
229228
self.name = iwd_access_point.name().await?;
@@ -247,7 +246,12 @@ impl AccessPoint {
247246
}
248247

249248
pub async fn scan(&self, sender: UnboundedSender<Event>) -> Result<()> {
250-
let iwd_access_point = self.session.access_points().await.unwrap().pop().unwrap();
249+
let iwd_access_point = self
250+
.session
251+
.access_points()
252+
.await?
253+
.pop()
254+
.context("No AP found")?;
251255
match iwd_access_point.scan().await {
252256
Ok(()) => Notification::send(
253257
"Start Scanning".to_string(),
@@ -261,7 +265,12 @@ impl AccessPoint {
261265
}
262266

263267
pub async fn start(&self, sender: UnboundedSender<Event>) -> Result<()> {
264-
let iwd_access_point = self.session.access_points().await.unwrap().pop().unwrap();
268+
let iwd_access_point = self
269+
.session
270+
.access_points()
271+
.await?
272+
.pop()
273+
.context("No AP found")?;
265274
match iwd_access_point
266275
.start(self.ssid.value(), self.psk.value())
267276
.await
@@ -280,7 +289,12 @@ impl AccessPoint {
280289
}
281290

282291
pub async fn stop(&self, sender: UnboundedSender<Event>) -> Result<()> {
283-
let iwd_access_point = self.session.access_points().await.unwrap().pop().unwrap();
292+
let iwd_access_point = self
293+
.session
294+
.access_points()
295+
.await?
296+
.pop()
297+
.context("No AP found")?;
284298
match iwd_access_point.stop().await {
285299
Ok(()) => {
286300
Notification::send("AP Stopped".to_string(), NotificationLevel::Info, &sender)?

0 commit comments

Comments
 (0)