Skip to content

Commit dc802bd

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 52de8ae commit dc802bd

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
@@ -318,7 +318,7 @@ impl Rauc {
318318
bb: &mut BrokerBuilder,
319319
wtb: &mut WatchedTasksBuilder,
320320
_conn: &Arc<Connection>,
321-
) -> Self {
321+
) -> Result<Self> {
322322
let inst = Self::setup_topics(bb);
323323

324324
inst.operation.set("idle".to_string());
@@ -336,17 +336,17 @@ impl Rauc {
336336
inst.channels.clone(),
337337
inst.slot_status.clone(),
338338
),
339-
);
339+
)?;
340340

341-
inst
341+
Ok(inst)
342342
}
343343

344344
#[cfg(not(feature = "demo_mode"))]
345345
pub fn new(
346346
bb: &mut BrokerBuilder,
347347
wtb: &mut WatchedTasksBuilder,
348348
conn: &Arc<Connection>,
349-
) -> Self {
349+
) -> Result<Self> {
350350
let inst = Self::setup_topics(bb);
351351

352352
let conn_task = conn.clone();
@@ -455,7 +455,7 @@ impl Rauc {
455455
break Ok(());
456456
}
457457
}
458-
});
458+
})?;
459459

460460
let conn_task = conn.clone();
461461
let progress = inst.progress.clone();
@@ -477,7 +477,7 @@ impl Rauc {
477477
}
478478

479479
Ok(())
480-
});
480+
})?;
481481

482482
let conn_task = conn.clone();
483483
let last_error = inst.last_error.clone();
@@ -499,7 +499,7 @@ impl Rauc {
499499
}
500500

501501
Ok(())
502-
});
502+
})?;
503503

504504
let conn_task = conn.clone();
505505
let (mut install_stream, _) = inst.install.clone().subscribe_unbounded();
@@ -519,7 +519,7 @@ impl Rauc {
519519
}
520520

521521
Ok(())
522-
});
522+
})?;
523523

524524
// Reload the channel list on request
525525
let (reload_stream, _) = inst.reload.clone().subscribe_unbounded();
@@ -532,9 +532,9 @@ impl Rauc {
532532
inst.channels.clone(),
533533
inst.slot_status.clone(),
534534
),
535-
);
535+
)?;
536536

537-
inst
537+
Ok(inst)
538538
}
539539
}
540540

0 commit comments

Comments
 (0)