Skip to content

Commit 212c4fc

Browse files
committed
watched_tasks: let spawn_task return Result instead of panicking
Signed-off-by: Leonard Göhrs <l.goehrs@pengutronix.de>
1 parent 7a1e35b commit 212c4fc

33 files changed

+243
-196
lines changed

src/adc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl Adc {
227227

228228
time.set(Timestamp::now());
229229
}
230-
});
230+
})?;
231231

232232
Ok(adc)
233233
}

src/backlight.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl Backlight {
6262
}
6363

6464
Ok(())
65-
});
65+
})?;
6666

6767
Ok(Self { brightness })
6868
}

src/broker.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// with this program; if not, write to the Free Software Foundation, Inc.,
1616
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717

18+
use anyhow::Result;
1819
use async_std::sync::Arc;
1920
use serde::{de::DeserializeOwned, Serialize};
2021

@@ -115,11 +116,13 @@ impl BrokerBuilder {
115116
/// Finish building the broker
116117
///
117118
/// This consumes the builder so that no new topics can be registered
118-
pub fn build(self, wtb: &mut WatchedTasksBuilder, server: &mut tide::Server<()>) {
119+
pub fn build(self, wtb: &mut WatchedTasksBuilder, server: &mut tide::Server<()>) -> Result<()> {
119120
let topics = Arc::new(self.topics);
120121

121-
persistence::register(wtb, topics.clone());
122+
persistence::register(wtb, topics.clone())?;
122123
rest::register(server, topics.clone());
123124
mqtt_conn::register(server, topics);
125+
126+
Ok(())
124127
}
125128
}

src/broker/persistence.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ async fn save_on_change(
148148
Ok(())
149149
}
150150

151-
pub fn register(wtb: &mut WatchedTasksBuilder, topics: Arc<Vec<Arc<dyn AnyTopic>>>) {
151+
pub fn register(wtb: &mut WatchedTasksBuilder, topics: Arc<Vec<Arc<dyn AnyTopic>>>) -> Result<()> {
152152
load(&topics).unwrap();
153153

154154
let (tx, rx) = unbounded();
@@ -157,5 +157,5 @@ pub fn register(wtb: &mut WatchedTasksBuilder, topics: Arc<Vec<Arc<dyn AnyTopic>
157157
topic.subscribe_as_bytes(tx.clone(), false);
158158
}
159159

160-
wtb.spawn_task("persistence-save", save_on_change(topics, rx));
160+
wtb.spawn_task("persistence-save", save_on_change(topics, rx))
161161
}

src/dbus.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::watched_tasks::WatchedTasksBuilder;
2323

2424
#[cfg(feature = "demo_mode")]
2525
mod zb {
26-
pub(super) type Result<T> = std::result::Result<T, ()>;
26+
pub(super) use anyhow::Result;
2727

2828
pub struct Connection;
2929
pub struct ConnectionBuilder;
@@ -82,21 +82,18 @@ impl DbusSession {
8282
wtb: &mut WatchedTasksBuilder,
8383
led_dut: Arc<Topic<BlinkPattern>>,
8484
led_uplink: Arc<Topic<BlinkPattern>>,
85-
) -> Self {
85+
) -> anyhow::Result<Self> {
8686
let tacd = Tacd::new();
8787

88-
let conn_builder = ConnectionBuilder::system()
89-
.unwrap()
90-
.name("de.pengutronix.tacd")
91-
.unwrap();
88+
let conn_builder = ConnectionBuilder::system()?.name("de.pengutronix.tacd")?;
9289

93-
let conn = Arc::new(tacd.serve(conn_builder).build().await.unwrap());
90+
let conn = Arc::new(tacd.serve(conn_builder).build().await?);
9491

95-
Self {
96-
hostname: Hostname::new(bb, wtb, &conn),
97-
network: Network::new(bb, wtb, &conn, led_dut, led_uplink),
98-
rauc: Rauc::new(bb, wtb, &conn),
99-
systemd: Systemd::new(bb, wtb, &conn).await,
100-
}
92+
Ok(Self {
93+
hostname: Hostname::new(bb, wtb, &conn)?,
94+
network: Network::new(bb, wtb, &conn, led_dut, led_uplink)?,
95+
rauc: Rauc::new(bb, wtb, &conn)?,
96+
systemd: Systemd::new(bb, wtb, &conn).await?,
97+
})
10198
}
10299
}

src/dbus/hostname.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
// with this program; if not, write to the Free Software Foundation, Inc.,
1616
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717

18+
use anyhow::Result;
19+
use async_std::sync::Arc;
20+
1821
#[cfg(not(feature = "demo_mode"))]
1922
use async_std::stream::StreamExt;
2023

2124
#[cfg(not(feature = "demo_mode"))]
2225
use zbus::Connection;
2326

24-
use async_std::sync::Arc;
25-
2627
use crate::broker::{BrokerBuilder, Topic};
2728
use crate::watched_tasks::WatchedTasksBuilder;
2829

@@ -34,18 +35,22 @@ pub struct Hostname {
3435

3536
impl Hostname {
3637
#[cfg(feature = "demo_mode")]
37-
pub fn new<C>(bb: &mut BrokerBuilder, _wtb: &mut WatchedTasksBuilder, _conn: C) -> Self {
38-
Self {
38+
pub fn new<C>(
39+
bb: &mut BrokerBuilder,
40+
_wtb: &mut WatchedTasksBuilder,
41+
_conn: C,
42+
) -> Result<Self> {
43+
Ok(Self {
3944
hostname: bb.topic_ro("/v1/tac/network/hostname", Some("lxatac".into())),
40-
}
45+
})
4146
}
4247

4348
#[cfg(not(feature = "demo_mode"))]
4449
pub fn new(
4550
bb: &mut BrokerBuilder,
4651
wtb: &mut WatchedTasksBuilder,
4752
conn: &Arc<Connection>,
48-
) -> Self {
53+
) -> Result<Self> {
4954
let hostname = bb.topic_ro("/v1/tac/network/hostname", None);
5055

5156
let conn = conn.clone();
@@ -67,8 +72,8 @@ impl Hostname {
6772
}
6873

6974
Ok(())
70-
});
75+
})?;
7176

72-
Self { hostname }
77+
Ok(Self { hostname })
7378
}
7479
}

src/dbus/networkmanager.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
// with this program; if not, write to the Free Software Foundation, Inc.,
1616
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717

18+
use anyhow::Result;
1819
use async_std;
1920
use async_std::sync::Arc;
20-
2121
use serde::{Deserialize, Serialize};
2222

2323
use crate::broker::{BrokerBuilder, Topic};
@@ -39,7 +39,7 @@ mod manager;
3939
// Put them inside a mod so we do not have to decorate each one with
4040
#[cfg(not(feature = "demo_mode"))]
4141
mod optional_includes {
42-
pub(super) use anyhow::{anyhow, Result};
42+
pub(super) use anyhow::anyhow;
4343
pub(super) use async_std::stream::StreamExt;
4444
pub(super) use async_std::task::sleep;
4545
pub(super) use futures::{future::FutureExt, select};
@@ -249,7 +249,7 @@ impl Network {
249249
_conn: C,
250250
_led_dut: Arc<Topic<BlinkPattern>>,
251251
_led_uplink: Arc<Topic<BlinkPattern>>,
252-
) -> Self {
252+
) -> Result<Self> {
253253
let this = Self::setup_topics(bb);
254254

255255
this.bridge_interface.set(vec![String::from("192.168.1.1")]);
@@ -262,7 +262,7 @@ impl Network {
262262
carrier: true,
263263
});
264264

265-
this
265+
Ok(this)
266266
}
267267

268268
#[cfg(not(feature = "demo_mode"))]
@@ -272,27 +272,27 @@ impl Network {
272272
conn: &Arc<Connection>,
273273
led_dut: Arc<Topic<BlinkPattern>>,
274274
led_uplink: Arc<Topic<BlinkPattern>>,
275-
) -> Self {
275+
) -> Result<Self> {
276276
let this = Self::setup_topics(bb);
277277

278278
let conn_task = conn.clone();
279279
let dut_interface = this.dut_interface.clone();
280280
wtb.spawn_task("link-dut-update", async move {
281281
handle_link_updates(&conn_task, dut_interface, "dut", led_dut).await
282-
});
282+
})?;
283283

284284
let conn_task = conn.clone();
285285
let uplink_interface = this.uplink_interface.clone();
286286
wtb.spawn_task("link-uplink-update", async move {
287287
handle_link_updates(&conn_task, uplink_interface, "uplink", led_uplink).await
288-
});
288+
})?;
289289

290290
let conn_task = conn.clone();
291291
let bridge_interface = this.bridge_interface.clone();
292292
wtb.spawn_task("ip-tac-bridge-update", async move {
293293
handle_ipv4_updates(&conn_task, bridge_interface, "tac-bridge").await
294-
});
294+
})?;
295295

296-
this
296+
Ok(this)
297297
}
298298
}

src/dbus/rauc.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ impl Rauc {
282282
bb: &mut BrokerBuilder,
283283
wtb: &mut WatchedTasksBuilder,
284284
_conn: &Arc<Connection>,
285-
) -> Self {
285+
) -> Result<Self> {
286286
let inst = Self::setup_topics(bb);
287287

288288
inst.operation.set("idle".to_string());
@@ -299,17 +299,17 @@ impl Rauc {
299299
inst.channels.clone(),
300300
inst.slot_status.clone(),
301301
),
302-
);
302+
)?;
303303

304-
inst
304+
Ok(inst)
305305
}
306306

307307
#[cfg(not(feature = "demo_mode"))]
308308
pub fn new(
309309
bb: &mut BrokerBuilder,
310310
wtb: &mut WatchedTasksBuilder,
311311
conn: &Arc<Connection>,
312-
) -> Self {
312+
) -> Result<Self> {
313313
let inst = Self::setup_topics(bb);
314314

315315
let conn_task = conn.clone();
@@ -408,7 +408,7 @@ impl Rauc {
408408
break Ok(());
409409
}
410410
}
411-
});
411+
})?;
412412

413413
let conn_task = conn.clone();
414414
let progress = inst.progress.clone();
@@ -430,7 +430,7 @@ impl Rauc {
430430
}
431431

432432
Ok(())
433-
});
433+
})?;
434434

435435
let conn_task = conn.clone();
436436
let last_error = inst.last_error.clone();
@@ -452,7 +452,7 @@ impl Rauc {
452452
}
453453

454454
Ok(())
455-
});
455+
})?;
456456

457457
let conn_task = conn.clone();
458458
let (mut install_stream, _) = inst.install.clone().subscribe_unbounded();
@@ -472,7 +472,7 @@ impl Rauc {
472472
}
473473

474474
Ok(())
475-
});
475+
})?;
476476

477477
// Reload the channel list on request
478478
let (reload_stream, _) = inst.reload.clone().subscribe_unbounded();
@@ -484,8 +484,8 @@ impl Rauc {
484484
inst.channels.clone(),
485485
inst.slot_status.clone(),
486486
),
487-
);
487+
)?;
488488

489-
inst
489+
Ok(inst)
490490
}
491491
}

0 commit comments

Comments
 (0)