cvlr_macros/
lib.rs

1use proc_macro::TokenStream;
2use quote::ToTokens;
3use syn::{parse_macro_input, parse_quote, ItemFn};
4
5mod mock;
6/// Mark a method as a CVT rule
7///
8/// # Example
9///
10/// ```
11/// use cvlr_asserts::cvlr_assert;
12/// use cvlr_macros::rule;
13/// #[rule]
14/// fn foo()  {
15///    cvlr_assert!(false);
16/// }
17/// ```
18#[proc_macro_attribute]
19pub fn rule(_attr: TokenStream, item: TokenStream) -> TokenStream {
20    let mut fn_ast = parse_macro_input!(item as ItemFn);
21    // add #[no_mangle] attribute
22    fn_ast.attrs.push(parse_quote! { #[no_mangle] });
23    // The first statement in rules is a call to the macro `cvlr_rule_location!`
24    // to automatically insert the location of the rule.
25    fn_ast
26        .block
27        .stmts
28        .insert(0, parse_quote! { cvlr::log::cvlr_rule_location!(); });
29    fn_ast
30        .block
31        .stmts
32        .push(parse_quote! { cvlr::cvlr_vacuity_check!(); });
33    fn_ast.into_token_stream().into()
34}
35
36#[proc_macro_attribute]
37pub fn mock_fn(attr: TokenStream, item: TokenStream) -> TokenStream {
38    mock::mock_fn_impl(attr, item)
39}