spring_ai_rs/ai_interface/callback/
engine.rs1use std::{error::Error, ffi::CStr};
2
3use libc::{c_int, c_void};
4
5use crate::{
6 ai_interface::{callback::command::command_data::CData, AIInterface},
7 get_callback,
8};
9
10pub fn handle_command<CD: CData>(
11 ai_id: c_int,
12 to_id: c_int,
13 command_id: c_int,
14 command_topic: c_int,
15 command_data: &mut CD,
16) -> Result<(), &'static str> {
17 let handle_command = get_callback!(ai_id, Engine_handleCommand)?;
18
19 let ret = unsafe {
20 handle_command(
21 ai_id,
22 to_id,
23 command_id,
24 command_topic,
25 command_data.c_void(),
26 )
27 };
28
29 if ret == 0 {
30 Ok(())
31 } else {
32 Err("Failed to run the command")
33 }
34}
35
36pub fn execute_command(
37 ai_id: c_int,
38 unit_id: c_int,
39 group_id: c_int,
40 command_data: *mut c_void,
41) -> Result<(), &'static str> {
42 let execute_command = get_callback!(ai_id, Engine_executeCommand)?;
43
44 if unsafe { execute_command(ai_id, unit_id, group_id, command_data) } != 0 {
45 Err("Failed to run the command")
46 } else {
47 Ok(())
48 }
49}
50
51#[derive(Debug, Clone)]
52pub struct Version {
53 major: String,
54 minor: String,
55 patch: String,
56 commits: String,
57 hash: String,
58 branch: String,
59 additional: String,
60}
61
62impl AIInterface {
63 pub fn engine_version(&self) -> Result<Version, Box<dyn Error>> {
64 let version_major = get_callback!(self.ai_id, Engine_Version_getMajor)?;
65 let version_minor = get_callback!(self.ai_id, Engine_Version_getMinor)?;
66 let version_patch = get_callback!(self.ai_id, Engine_Version_getPatchset)?;
67 let version_commits = get_callback!(self.ai_id, Engine_Version_getCommits)?;
68 let version_hash = get_callback!(self.ai_id, Engine_Version_getHash)?;
69 let version_branch = get_callback!(self.ai_id, Engine_Version_getBranch)?;
70 let version_additional = get_callback!(self.ai_id, Engine_Version_getAdditional)?;
71
72 Ok(Version {
73 major: String::from(unsafe { CStr::from_ptr(version_major(self.ai_id)) }.to_str()?),
74 minor: String::from(unsafe { CStr::from_ptr(version_minor(self.ai_id)) }.to_str()?),
75 patch: String::from(unsafe { CStr::from_ptr(version_patch(self.ai_id)) }.to_str()?),
76 commits: String::from(unsafe { CStr::from_ptr(version_commits(self.ai_id)) }.to_str()?),
77 hash: String::from(unsafe { CStr::from_ptr(version_hash(self.ai_id)) }.to_str()?),
78 branch: String::from(unsafe { CStr::from_ptr(version_branch(self.ai_id)) }.to_str()?),
79 additional: String::from(
80 unsafe { CStr::from_ptr(version_additional(self.ai_id)) }.to_str()?,
81 ),
82 })
83 }
84}