Skip to content

Commit 2582421

Browse files
committed
tacd: split main() function into init() and run() functions
This will simplify error handling in init() later on, because we can use the ? operator to handle Err Results. Signed-off-by: Leonard Göhrs <[email protected]>
1 parent 8a9f784 commit 2582421

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

src/main.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,11 @@ use regulators::Regulators;
5050
use setup_mode::SetupMode;
5151
use system::System;
5252
use temperatures::Temperatures;
53-
use ui::{setup_display, Ui, UiResources};
53+
use ui::{setup_display, Display, Ui, UiResources};
5454
use usb_hub::UsbHub;
5555
use watchdog::Watchdog;
5656

57-
#[async_std::main]
58-
async fn main() -> Result<()> {
59-
env_logger::init();
60-
61-
// Show a splash screen very early on
62-
let display = setup_display();
63-
57+
async fn init() -> Result<(Ui, HttpServer, Option<Watchdog>)> {
6458
// The BrokerBuilder collects topics that should be exported via the
6559
// MQTT/REST APIs.
6660
// The topics are also used to pass around data inside the tacd.
@@ -122,9 +116,6 @@ async fn main() -> Result<()> {
122116
// in the web interface.
123117
journal::serve(&mut http_server.server);
124118

125-
// Expose the display as a .png on the web server
126-
ui::serve_display(&mut http_server.server, display.screenshooter());
127-
128119
// Set up the user interface for the hardware display on the TAC.
129120
// The different screens receive updates via the topics provided in
130121
// the UiResources struct.
@@ -153,6 +144,18 @@ async fn main() -> Result<()> {
153144
// and expose the topics via HTTP and MQTT-over-websocket.
154145
bb.build(&mut http_server.server);
155146

147+
Ok((ui, http_server, watchdog))
148+
}
149+
150+
async fn run(
151+
ui: Ui,
152+
mut http_server: HttpServer,
153+
watchdog: Option<Watchdog>,
154+
display: Display,
155+
) -> Result<()> {
156+
// Expose the display as a .png on the web server
157+
ui::serve_display(&mut http_server.server, display.screenshooter());
158+
156159
log::info!("Setup complete. Handling requests");
157160

158161
// Run until the user interface, http server or (if selected) the watchdog
@@ -172,3 +175,14 @@ async fn main() -> Result<()> {
172175

173176
Ok(())
174177
}
178+
179+
#[async_std::main]
180+
async fn main() -> Result<()> {
181+
env_logger::init();
182+
183+
// Show a splash screen very early on
184+
let display = setup_display();
185+
186+
let (ui, http_server, watchdog) = init().await?;
187+
run(ui, http_server, watchdog, display).await
188+
}

0 commit comments

Comments
 (0)