A focused Docker Compose management web application.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: wrap err macro

Brooke b7d5e9b8 23cc08e4

+42
+1
packages/macros/.gitignore
··· 1 + /target
+11
packages/macros/Cargo.toml
··· 1 + [package] 2 + name = "luminary-macros" 3 + edition = "2021" 4 + 5 + [dependencies] 6 + proc-macro2 = "1.0" 7 + quote = "1.0.40" 8 + syn = "2.0.101" 9 + 10 + [lib] 11 + proc-macro = true
+30
packages/macros/src/lib.rs
··· 1 + use proc_macro::TokenStream; 2 + use proc_macro2::TokenStream as TokenStream2; 3 + use quote::quote; 4 + use syn::{parse_macro_input, ItemFn}; 5 + 6 + /// A macro to wrap the body of a function in `wrap_err`. This is useful for reducing boilerplate when using `eyre`. 7 + #[proc_macro_attribute] 8 + pub fn wrap_err(attr: TokenStream, item: TokenStream) -> TokenStream { 9 + let func = parse_macro_input!(item as ItemFn); 10 + let attr: TokenStream2 = attr.into(); 11 + let visibility = &func.vis; 12 + 13 + // Create variables for quoting 14 + let asyncness = &func.sig.asyncness; 15 + let output = &func.sig.output; 16 + let signature = &func.sig; 17 + let block = &func.block; 18 + 19 + let wait = match asyncness.is_some() { 20 + true => Some(quote! { .await }), 21 + false => None, 22 + }; 23 + 24 + return quote! { 25 + #visibility #signature { 26 + (#asyncness move || #output #block)()#wait.wrap_err(#attr) 27 + } 28 + } 29 + .into(); 30 + }