saa_proto_wasm/
utils.rs

1use std::fmt::Display;
2
3use quote::ToTokens;
4use syn::{
5    parse::{Parse, ParseStream}, parse_quote, punctuated::Punctuated, Meta, Token
6};
7
8
9macro_rules! fallible_macro {
10    (
11        $(
12            #[ $( $attribute_decl:tt )* ]
13        )*
14        pub fn $macro_name:ident ( $( $params:tt )* ) -> syn::Result<$inner_return:path> {
15            $( $fn_body:tt )*
16        }
17    ) => {
18        $(
19            #[ $( $attribute_decl )* ]
20        )*
21        pub fn $macro_name ( $( $params )* ) -> $inner_return {
22            let result = move || -> ::syn::Result<_> {
23                $( $fn_body )*
24            };
25
26            match result() {
27                Ok(val) => val,
28                Err(err) => err.into_compile_error().into(),
29            }
30        }
31    }
32}
33
34
35
36macro_rules! bail {
37    ($span_src:expr, $msg:literal) => {{
38        return Err($crate::utils::error_message!($span_src, $msg));
39    }};
40}
41
42
43macro_rules! error_message {
44    ($span_src:expr, $msg:literal) => {{
45        ::syn::Error::new(::syn::spanned::Spanned::span(&{ $span_src }), $msg)
46    }};
47}
48
49#[derive(Debug, Clone)]
50pub struct Options {
51    pub crate_path: syn::Path,
52    pub no_deny : bool,
53}
54
55impl Default for Options {
56    fn default() -> Self {
57        Self {
58            crate_path: parse_quote!(::saa_schema),
59            no_deny: false,
60        }
61    }
62}
63
64impl Display for Options {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        write!(f, "crate_path: {}", self.crate_path.to_token_stream())
67    }
68}
69
70
71impl Parse for Options {
72    fn parse(input: ParseStream) -> syn::Result<Self> {
73        let mut acc = Self::default();
74        let params = Punctuated::<Meta, Token![,]>::parse_terminated(input)?;
75        for param in params {
76            let path = param.path();
77            if path.is_ident("crate") {
78                let path_as_string: syn::LitStr = syn::parse2(param.to_token_stream())?;
79                acc.crate_path = path_as_string.parse()?
80            } else if path.is_ident("no_deny") {
81                acc.no_deny = true;
82            } else {
83                bail!(param, "unknown option");
84            }
85        }
86        Ok(acc)
87    }
88}
89
90pub(crate) use bail;
91pub(crate) use error_message;
92pub(crate) use fallible_macro;