1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, ItemFn};

/// Attribute macro for your solutions `async fn main` to setup loggig, tokio, verifies the
/// environment etc
#[proc_macro_attribute]
pub fn main(_: TokenStream, input: TokenStream) -> TokenStream {
    let main = parse_macro_input!(input as ItemFn);

    let sig = main.sig;
    let block = main.block;

    quote! {
        #[::hs_hackathon::prelude::tokio::main]
        #sig {
            {
                use hs_hackathon::prelude::{tracing, tracing_subscriber};
                use tracing::level_filters::LevelFilter;
                use tracing_subscriber::EnvFilter;

                let filter = EnvFilter::builder()
                    .with_default_directive(LevelFilter::DEBUG.into())
                    .from_env().expect("internal error: failed to setup tracing");

                tracing_subscriber::fmt().with_env_filter(filter).init();

                #[cfg(target_os = "linux")]
                if cfg!(debug_assertions) {
                    panic!("running in debug mode is not supported on the pi due to performance restirctions");
                }

                #[cfg(target_os = "linux")]
                {
                    const DRONE_SERVICE: &str = "drone-wifi";

                    let status = std::process::Command::new("systemctl")
                        .arg("is-active")
                        .arg(DRONE_SERVICE)
                        .status()
                        .expect("failed to verify the wifi of the drones wifi");

                    assert!(status.success(), "drone wifi systemd service has a unexpected status");
                    tracing::info!("network setup: success");
                }
            }

            #block
        }
    }
    .into()
}