Skip to main content

behaviortree_derive/
lib.rs

1// Copyright © 2025 Stephan Kunz
2//! Derive macros for [`Behavior`](crate)s.
3//! There are 4 derive macros available:
4//! - Action
5//! - Condition
6//! - Control
7//! - Decorator
8//!
9//! On struct level there are the attributes
10//! - `#[behavior(no_create)]`: will suppress the derive of create_fn(...)
11//! - `#[behavior(no_register)]`: will suppress the derive of register()
12//! - `#[behavior(no_register_with)]`: will suppress the derive of register_with(...)
13//!
14//! On field level there are the attributes
15//! - `#[behavior(parameter)]`: defines a field as a parameter for `create_fn(...)` and `register_with(...)`
16//!   The values must be given when using the `register_with(...)` method.
17//!   A parameter must implement `Clone`.
18//! - `#[behavior(default = <Expression>)]`: defines a default value for a field.
19//!   'Expression' can be any Rust expression that creates an appropriate value out of nothing.
20//!
21//! # Usage
22//! Example uses the derive macro `Action`, the others work respectively.
23//! ```no_test
24//! #[derive(Action)]
25//! struct MyAction {
26//!     // specific elements
27//!     ...
28//! }
29//!
30//! impl MyAction {
31//!     // your specific implementations
32//!     ...
33//! }
34//! ```
35//!
36//! # Errors
37//! - if attributes are used in a wrong way
38//!
39//! # Panics
40//! - if used on enums or unions or functions
41
42use quote::quote;
43
44mod derive_behavior;
45
46use derive_behavior::derive_behavior_impl;
47use proc_macro::TokenStream;
48
49/// Derive macro for an [`Action`] type `Behavior`, [`usage`](https://docs.rs/behaviortree-derive).
50#[proc_macro_derive(Action, attributes(behavior))]
51pub fn derive_action(input: TokenStream) -> TokenStream {
52	derive_behavior_impl(
53		input.into(),
54		quote! { behaviortree_core::behavior_kind::BehaviorKind::Action },
55	)
56	.into()
57}
58
59/// Derive macro for an [`Condition`] type `Behavior`, [`usage`](https://docs.rs/behaviortree-derive).
60#[proc_macro_derive(Condition, attributes(behavior))]
61pub fn derive_condition(input: TokenStream) -> TokenStream {
62	derive_behavior_impl(
63		input.into(),
64		quote! { behaviortree_core::behavior_kind::BehaviorKind::Condition },
65	)
66	.into()
67}
68
69/// Derive macro for an [`Control`] type `Behavior`, [`usage`](https://docs.rs/behaviortree-derive).
70#[proc_macro_derive(Control, attributes(behavior))]
71pub fn derive_control(input: TokenStream) -> TokenStream {
72	derive_behavior_impl(
73		input.into(),
74		quote! { behaviortree_core::behavior_kind::BehaviorKind::Control },
75	)
76	.into()
77}
78
79/// Derive macro for an [`Decorator`] type `Behavior`, [`usage`](https://docs.rs/behaviortree-derive).
80#[proc_macro_derive(Decorator, attributes(behavior))]
81pub fn derive_decorator(input: TokenStream) -> TokenStream {
82	derive_behavior_impl(
83		input.into(),
84		quote! { behaviortree_core::behavior_kind::BehaviorKind::Decorator },
85	)
86	.into()
87}