behaviortree-derive 0.5.0

Derive macros for behaviortree
Documentation
// Copyright © 2025 Stephan Kunz
//! Derive macros for [`Behavior`](crate)s.
//! There are 4 derive macros available:
//! - Action
//! - Condition
//! - Control
//! - Decorator
//!
//! On struct level there are the attributes
//! - `#[behavior(no_create)]`: will suppress the derive of create_fn(...)
//! - `#[behavior(no_register)]`: will suppress the derive of register()
//! - `#[behavior(no_register_with)]`: will suppress the derive of register_with(...)
//!
//! On field level there are the attributes
//! - `#[behavior(parameter)]`: defines a field as a parameter for `create_fn(...)` and `register_with(...)`
//!   The values must be given when using the `register_with(...)` method.
//!   A parameter must implement `Clone`.
//! - `#[behavior(default = <Expression>)]`: defines a default value for a field.
//!   'Expression' can be any Rust expression that creates an appropriate value out of nothing.
//!
//! # Usage
//! Example uses the derive macro `Action`, the others work respectively.
//! ```no_test
//! #[derive(Action)]
//! struct MyAction {
//!     // specific elements
//!     ...
//! }
//!
//! impl MyAction {
//!     // your specific implementations
//!     ...
//! }
//! ```
//!
//! # Errors
//! - if attributes are used in a wrong way
//!
//! # Panics
//! - if used on enums or unions or functions

use quote::quote;

mod derive_behavior;

use derive_behavior::derive_behavior_impl;
use proc_macro::TokenStream;

/// Derive macro for an [`Action`] type `Behavior`, [`usage`](https://docs.rs/behaviortree-derive).
#[proc_macro_derive(Action, attributes(behavior))]
pub fn derive_action(input: TokenStream) -> TokenStream {
	derive_behavior_impl(
		input.into(),
		quote! { behaviortree_core::behavior_kind::BehaviorKind::Action },
	)
	.into()
}

/// Derive macro for an [`Condition`] type `Behavior`, [`usage`](https://docs.rs/behaviortree-derive).
#[proc_macro_derive(Condition, attributes(behavior))]
pub fn derive_condition(input: TokenStream) -> TokenStream {
	derive_behavior_impl(
		input.into(),
		quote! { behaviortree_core::behavior_kind::BehaviorKind::Condition },
	)
	.into()
}

/// Derive macro for an [`Control`] type `Behavior`, [`usage`](https://docs.rs/behaviortree-derive).
#[proc_macro_derive(Control, attributes(behavior))]
pub fn derive_control(input: TokenStream) -> TokenStream {
	derive_behavior_impl(
		input.into(),
		quote! { behaviortree_core::behavior_kind::BehaviorKind::Control },
	)
	.into()
}

/// Derive macro for an [`Decorator`] type `Behavior`, [`usage`](https://docs.rs/behaviortree-derive).
#[proc_macro_derive(Decorator, attributes(behavior))]
pub fn derive_decorator(input: TokenStream) -> TokenStream {
	derive_behavior_impl(
		input.into(),
		quote! { behaviortree_core::behavior_kind::BehaviorKind::Decorator },
	)
	.into()
}