Macro manyhow::function

source ·
macro_rules! function {
    (#[as_dummy] $input:expr, $impl:expr $(,)?) => { ... };
    ($input:expr, $impl:expr $(,)?) => { ... };
}
Expand description

Handles function like proc_macro implementation

Takes any TokenStream for input and returns any TokenStream. If #[as_dummy] is specified on input, it will be used as default dummy code on error. body takes a FunctionMacroHandler with one TokenStream or type implementing Parse parameter and returns a TokenStream or type implementing ToTokens. And an optional &mut Emitter and a &mut TokenStream for storing a dummy output.

use manyhow::{function, Emitter, Result};
use proc_macro2::TokenStream;
function!(input, |input: syn::Item,
                  dummy: &mut TokenStream,
                  emitter: &mut Emitter|
 -> Result<syn::ItemImpl> {
    // ..
});

Note: When #[as_dummy] is specified on the input, the dummy: &mut TokenStream will be initialized with input. To override assign a new TokenStream:

use proc_macro_utils::assert_tokens;
use manyhow::{function, Result, SilentError};
use proc_macro2::TokenStream;
use quote::{quote, ToTokens};

let input = quote!(some input);
let output: TokenStream = function!(
    #[as_dummy] input,
    |input: TokenStream, dummy: &mut TokenStream|
     -> Result<TokenStream, SilentError> {
        assert_tokens!(dummy.to_token_stream(), {
            some input
        });
        *dummy = quote! {
            another input
        };
        // ..
        Err(SilentError)
    },
);

assert_tokens! {output, {another input}};