Skip to main content

waveless_commons/
runtime.rs

1// Waveless
2// Copyright (C) 2026 Oscar Alvarez Gonzalez
3
4use crate::*;
5
6use tokio::runtime::Builder;
7
8/// A Tokio runtime creator and an exit handler to prettify success and error contexts
9/// TODO: Replace this with custom error types.
10pub fn handle_main<T>(main_fn: T) -> Result<()>
11where
12    T: AsyncFn() -> Result<CompactString>,
13{
14    color_eyre::install()?;
15
16    let runtime = Builder::new_multi_thread()
17        .enable_all()
18        .thread_stack_size(128 * 1024 * 1024)
19        .build()
20        .unwrap();
21
22    runtime.block_on(async {
23        match main_fn().await {
24            Ok(res) => {
25                if !res.is_empty() {
26                    element! {
27                    View(
28                        padding_left: 1,
29                        padding_right: 1,
30                        border_style: BorderStyle::Round,
31                        border_color: iocraft::Color::Green,
32                    ) {
33                        MixedText(align: TextAlign::Left, contents: vec![
34                            MixedTextContent::new("✅ "),
35                            MixedTextContent::new("SUCCESS: ").color(iocraft::Color::Green).weight(Weight::Bold),
36                            MixedTextContent::new(res).color(iocraft::Color::White),
37                        ])
38                    }
39                }
40                .print();
41                }
42            }
43            Err(err) => return Err(err)
44        }
45        Ok(())
46    })
47}