spring_ai_rs/ai_interface/callback/unit/
command_info.rs1use std::{error::Error, f32::NAN, ffi::CStr};
2
3use crate::get_callback;
4
5#[derive(Debug, Copy, Clone)]
6pub struct UnitCurrentCommand {
7 pub ai_id: i32,
8 pub unit_id: i32,
9 pub current_command_index: i32,
10}
11
12#[derive(Debug, Clone)]
13pub struct UnitCurrentCommandAll {
14 id: i32,
15 options: i16,
16 tag: i32,
17 timeout: i32,
18 parameters: Vec<f32>,
19}
20
21const MAX_PARAMETERS: usize = 32;
22
23impl UnitCurrentCommand {
24 pub fn id(&self) -> Result<i32, Box<dyn Error>> {
25 let get_current_command_id_func = get_callback!(self.ai_id, Unit_CurrentCommand_getId)?;
26
27 Ok(unsafe {
28 get_current_command_id_func(self.ai_id, self.unit_id, self.current_command_index)
29 })
30 }
31
32 pub fn options(&self) -> Result<i16, Box<dyn Error>> {
33 let get_current_command_options_func =
34 get_callback!(self.ai_id, Unit_CurrentCommand_getOptions)?;
35
36 Ok(unsafe {
37 get_current_command_options_func(self.ai_id, self.unit_id, self.current_command_index)
38 })
39 }
40
41 pub fn tag(&self) -> Result<i32, Box<dyn Error>> {
42 let get_current_command_tag_func = get_callback!(self.ai_id, Unit_CurrentCommand_getTag)?;
43
44 Ok(unsafe {
45 get_current_command_tag_func(self.ai_id, self.unit_id, self.current_command_index)
46 })
47 }
48
49 pub fn timeout(&self) -> Result<i32, Box<dyn Error>> {
50 let get_current_command_timeout_func =
51 get_callback!(self.ai_id, Unit_CurrentCommand_getTimeOut)?;
52
53 Ok(unsafe {
54 get_current_command_timeout_func(self.ai_id, self.unit_id, self.current_command_index)
55 })
56 }
57
58 pub fn parameters(&self) -> Result<Vec<f32>, Box<dyn Error>> {
59 let get_current_command_parameters_func =
60 get_callback!(self.ai_id, Unit_CurrentCommand_getParams)?;
61
62 let mut ret = [NAN; MAX_PARAMETERS];
63 unsafe {
64 get_current_command_parameters_func(
65 self.ai_id,
66 self.unit_id,
67 self.current_command_index,
68 ret.as_mut_ptr(),
69 MAX_PARAMETERS as i32,
70 )
71 };
72
73 Ok(ret.iter().cloned().filter(|&f| f.is_finite()).collect())
74 }
75
76 pub fn all(&self) -> Result<UnitCurrentCommandAll, Box<dyn Error>> {
77 Ok(UnitCurrentCommandAll {
78 id: self.id()?,
79 options: self.options()?,
80 tag: self.tag()?,
81 timeout: self.timeout()?,
82 parameters: self.parameters()?,
83 })
84 }
85}
86
87#[derive(Debug, Copy, Clone)]
88pub struct UnitSupportedCommand {
89 pub ai_id: i32,
90 pub unit_id: i32,
91 pub supported_command_index: i32,
92}
93
94#[derive(Debug, Clone)]
95pub struct UnitSupportedCommandAll {
96 id: i32,
97 name: String,
98 tooltip: String,
99 is_show_unique: bool,
100 is_disabled: bool,
101 parameters: String,
102}
103
104impl UnitSupportedCommand {
105 pub fn id(&self) -> Result<i32, Box<dyn Error>> {
106 let get_supported_command_id_func = get_callback!(self.ai_id, Unit_SupportedCommand_getId)?;
107
108 Ok(unsafe {
109 get_supported_command_id_func(self.ai_id, self.unit_id, self.supported_command_index)
110 })
111 }
112
113 pub fn name(&self) -> Result<String, Box<dyn Error>> {
114 let get_supported_command_name_func =
115 get_callback!(self.ai_id, Unit_SupportedCommand_getName)?;
116
117 Ok(String::from(
118 unsafe {
119 CStr::from_ptr(get_supported_command_name_func(
120 self.ai_id,
121 self.unit_id,
122 self.supported_command_index,
123 ))
124 }
125 .to_str()?,
126 ))
127 }
128
129 pub fn tooltip(&self) -> Result<String, Box<dyn Error>> {
130 let get_supported_command_tooltip_func =
131 get_callback!(self.ai_id, Unit_SupportedCommand_getToolTip)?;
132
133 Ok(String::from(
134 unsafe {
135 CStr::from_ptr(get_supported_command_tooltip_func(
136 self.ai_id,
137 self.unit_id,
138 self.supported_command_index,
139 ))
140 }
141 .to_str()?,
142 ))
143 }
144
145 pub fn is_show_unique(&self) -> Result<bool, Box<dyn Error>> {
146 let get_suppored_command_is_show_unique_func =
147 get_callback!(self.ai_id, Unit_SupportedCommand_isShowUnique)?;
148
149 Ok(unsafe {
150 get_suppored_command_is_show_unique_func(
151 self.ai_id,
152 self.unit_id,
153 self.supported_command_index,
154 )
155 })
156 }
157
158 pub fn is_disabled(&self) -> Result<bool, Box<dyn Error>> {
159 let get_supported_command_is_disabled_func =
160 get_callback!(self.ai_id, Unit_SupportedCommand_isDisabled)?;
161
162 Ok(unsafe {
163 get_supported_command_is_disabled_func(
164 self.ai_id,
165 self.unit_id,
166 self.supported_command_index,
167 )
168 })
169 }
170
171 pub fn parameters(&self) -> Result<String, Box<dyn Error>> {
172 let get_supported_command_parameters_func =
173 get_callback!(self.ai_id, Unit_SupportedCommand_getParams)?;
174
175 let s = String::with_capacity(MAX_PARAMETERS);
176 let ptr = &mut (s.as_ptr() as *const i8);
177 unsafe {
178 get_supported_command_parameters_func(
179 self.ai_id,
180 self.unit_id,
181 self.supported_command_index,
182 ptr as *mut *const i8,
183 MAX_PARAMETERS as i32,
184 )
185 };
186
187 Ok(String::from(
188 unsafe { CStr::from_ptr(*ptr) }.to_str().unwrap_or(""),
189 ))
190 }
191
192 pub fn all(&self) -> Result<UnitSupportedCommandAll, Box<dyn Error>> {
193 Ok(UnitSupportedCommandAll {
194 id: self.id()?,
195 name: self.name()?,
196 tooltip: self.tooltip()?,
197 is_show_unique: self.is_show_unique()?,
198 is_disabled: self.is_disabled()?,
199 parameters: self.parameters()?,
200 })
201 }
202}