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
//! A macro checks like "behavior" in elixir language.
mod inner;
use syn::{parse_macro_input, ItemTrait};
/// Check whether the modules have functions with the same signature as the counterparts of behavior trait.
/// ## Example
///
/// ```rust
/// #[behavior::behavior(modules(en, ja))]
/// trait Behavior {
/// fn greeting() -> &'static str;
/// }
///
/// mod en {
/// pub fn greeting() -> &'static str {
/// "hello"
/// }
/// }
///
/// mod ja {
/// pub fn greeting() -> &'static str {
/// "こんにちは"
/// }
/// }
///
/// #[cfg(feature = "en")]
/// pub use en::*;
///
/// #[cfg(all(not(feature = "en"), feature = "ja"))]
/// pub use ja::*;
/// ```
#[proc_macro_attribute]
pub fn behavior(
args: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let input_trait = parse_macro_input!(item as ItemTrait);
inner::behavior_inner(args.into(), &input_trait)
.unwrap_or_else(|e| e.into_compile_error())
.into()
}