hs_hackathon_macros/
lib.rs

1use proc_macro::TokenStream;
2use quote::quote;
3use syn::{parse_macro_input, ItemFn};
4
5/// Attribute macro for your solutions `async fn main` to setup loggig, tokio, verifies the
6/// environment etc
7#[proc_macro_attribute]
8pub fn main(_: TokenStream, input: TokenStream) -> TokenStream {
9    let main = parse_macro_input!(input as ItemFn);
10
11    let sig = main.sig;
12    let block = main.block;
13
14    quote! {
15        #[::hs_hackathon::prelude::tokio::main]
16        #sig {
17            {
18                use hs_hackathon::prelude::{tracing, tracing_subscriber};
19                use tracing::level_filters::LevelFilter;
20                use tracing_subscriber::EnvFilter;
21
22                let filter = EnvFilter::builder()
23                    .with_default_directive(LevelFilter::DEBUG.into())
24                    .from_env().expect("internal error: failed to setup tracing");
25
26                tracing_subscriber::fmt().with_env_filter(filter).init();
27
28                #[cfg(target_os = "linux")]
29                if cfg!(debug_assertions) {
30                    panic!("running in debug mode is not supported on the pi due to performance restrictions");
31                }
32
33                #[cfg(target_os = "linux")]
34                {
35                    const DRONE_SERVICE: &str = "drone-wifi";
36
37                    let status = std::process::Command::new("systemctl")
38                        .arg("is-active")
39                        .arg(DRONE_SERVICE)
40                        .status()
41                        .expect("failed to verify the wifi of the drones wifi");
42
43                    assert!(status.success(), "drone wifi systemd service has a unexpected status");
44                    tracing::info!("network setup: success");
45                }
46            }
47
48            #block
49        }
50    }
51    .into()
52}