behaviortree 0.7.4

A #![no_std] compatible behavior tree library similar to 'BehaviorTree.CPP'.
Documentation
// Copyright © 2025 Stephan Kunz
//! [`Script`] [`Action`] implementation.

// region:      --- modules
use crate::{
	self as behaviortree, Action, EMPTY_STR,
	behavior::{Behavior, BehaviorData, BehaviorResult, BehaviorState},
	input_port,
	port::PortList,
	port_list,
	tree::BehaviorTreeElementList,
};
use alloc::{
	boxed::Box,
	string::{String, ToString},
};
use tinyscript::SharedRuntime;
//endregion:    --- modules

// region:		--- globals
/// Port name literals
const CODE: &str = "code";
// endregion:	--- globals

/// The `Script` behavior returns Success or Failure depending on the result of the scripted code.
///
/// The behavior is gated behind feature `script` and it is a Groot2 behavior.
#[derive(Action, Default)]
#[behavior(groot2)]
pub struct Script;

#[async_trait::async_trait]
impl Behavior for Script {
	async fn tick(
		&mut self,
		behavior: &mut BehaviorData,
		_children: &mut BehaviorTreeElementList,
		runtime: &SharedRuntime,
	) -> BehaviorResult {
		let code = behavior.get::<String>(CODE)?;
		let value = runtime.lock().run(&code, behavior)?;

		let state = if value.is_bool() {
			let val = bool::try_from(value)?;
			if val { BehaviorState::Success } else { BehaviorState::Failure }
		} else {
			BehaviorState::Success
		};

		Ok(state)
	}

	fn provided_ports() -> PortList {
		port_list![input_port!(
			String,
			CODE,
			EMPTY_STR,
			"Piece of code that can be parsed."
		)]
	}
}