spring_ai_rs/ai_interface/callback/unit/
command_info.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use std::{error::Error, f32::NAN, ffi::CStr};

use crate::get_callback;

#[derive(Debug, Copy, Clone)]
pub struct UnitCurrentCommand {
    pub ai_id: i32,
    pub unit_id: i32,
    pub current_command_index: i32,
}

#[derive(Debug, Clone)]
pub struct UnitCurrentCommandAll {
    id: i32,
    options: i16,
    tag: i32,
    timeout: i32,
    parameters: Vec<f32>,
}

const MAX_PARAMETERS: usize = 32;

impl UnitCurrentCommand {
    pub fn id(&self) -> Result<i32, Box<dyn Error>> {
        let get_current_command_id_func = get_callback!(self.ai_id, Unit_CurrentCommand_getId)?;

        Ok(unsafe {
            get_current_command_id_func(self.ai_id, self.unit_id, self.current_command_index)
        })
    }

    pub fn options(&self) -> Result<i16, Box<dyn Error>> {
        let get_current_command_options_func =
            get_callback!(self.ai_id, Unit_CurrentCommand_getOptions)?;

        Ok(unsafe {
            get_current_command_options_func(self.ai_id, self.unit_id, self.current_command_index)
        })
    }

    pub fn tag(&self) -> Result<i32, Box<dyn Error>> {
        let get_current_command_tag_func = get_callback!(self.ai_id, Unit_CurrentCommand_getTag)?;

        Ok(unsafe {
            get_current_command_tag_func(self.ai_id, self.unit_id, self.current_command_index)
        })
    }

    pub fn timeout(&self) -> Result<i32, Box<dyn Error>> {
        let get_current_command_timeout_func =
            get_callback!(self.ai_id, Unit_CurrentCommand_getTimeOut)?;

        Ok(unsafe {
            get_current_command_timeout_func(self.ai_id, self.unit_id, self.current_command_index)
        })
    }

    pub fn parameters(&self) -> Result<Vec<f32>, Box<dyn Error>> {
        let get_current_command_parameters_func =
            get_callback!(self.ai_id, Unit_CurrentCommand_getParams)?;

        let mut ret = [NAN; MAX_PARAMETERS];
        unsafe {
            get_current_command_parameters_func(
                self.ai_id,
                self.unit_id,
                self.current_command_index,
                ret.as_mut_ptr(),
                MAX_PARAMETERS as i32,
            )
        };

        Ok(ret.iter().cloned().filter(|&f| f.is_finite()).collect())
    }

    pub fn all(&self) -> Result<UnitCurrentCommandAll, Box<dyn Error>> {
        Ok(UnitCurrentCommandAll {
            id: self.id()?,
            options: self.options()?,
            tag: self.tag()?,
            timeout: self.timeout()?,
            parameters: self.parameters()?,
        })
    }
}

#[derive(Debug, Copy, Clone)]
pub struct UnitSupportedCommand {
    pub ai_id: i32,
    pub unit_id: i32,
    pub supported_command_index: i32,
}

#[derive(Debug, Clone)]
pub struct UnitSupportedCommandAll {
    id: i32,
    name: String,
    tooltip: String,
    is_show_unique: bool,
    is_disabled: bool,
    parameters: String,
}

impl UnitSupportedCommand {
    pub fn id(&self) -> Result<i32, Box<dyn Error>> {
        let get_supported_command_id_func = get_callback!(self.ai_id, Unit_SupportedCommand_getId)?;

        Ok(unsafe {
            get_supported_command_id_func(self.ai_id, self.unit_id, self.supported_command_index)
        })
    }

    pub fn name(&self) -> Result<String, Box<dyn Error>> {
        let get_supported_command_name_func =
            get_callback!(self.ai_id, Unit_SupportedCommand_getName)?;

        Ok(String::from(
            unsafe {
                CStr::from_ptr(get_supported_command_name_func(
                    self.ai_id,
                    self.unit_id,
                    self.supported_command_index,
                ))
            }
            .to_str()?,
        ))
    }

    pub fn tooltip(&self) -> Result<String, Box<dyn Error>> {
        let get_supported_command_tooltip_func =
            get_callback!(self.ai_id, Unit_SupportedCommand_getToolTip)?;

        Ok(String::from(
            unsafe {
                CStr::from_ptr(get_supported_command_tooltip_func(
                    self.ai_id,
                    self.unit_id,
                    self.supported_command_index,
                ))
            }
            .to_str()?,
        ))
    }

    pub fn is_show_unique(&self) -> Result<bool, Box<dyn Error>> {
        let get_suppored_command_is_show_unique_func =
            get_callback!(self.ai_id, Unit_SupportedCommand_isShowUnique)?;

        Ok(unsafe {
            get_suppored_command_is_show_unique_func(
                self.ai_id,
                self.unit_id,
                self.supported_command_index,
            )
        })
    }

    pub fn is_disabled(&self) -> Result<bool, Box<dyn Error>> {
        let get_supported_command_is_disabled_func =
            get_callback!(self.ai_id, Unit_SupportedCommand_isDisabled)?;

        Ok(unsafe {
            get_supported_command_is_disabled_func(
                self.ai_id,
                self.unit_id,
                self.supported_command_index,
            )
        })
    }

    pub fn parameters(&self) -> Result<String, Box<dyn Error>> {
        let get_supported_command_parameters_func =
            get_callback!(self.ai_id, Unit_SupportedCommand_getParams)?;

        let s = String::with_capacity(MAX_PARAMETERS);
        let ptr = &mut (s.as_ptr() as *const i8);
        unsafe {
            get_supported_command_parameters_func(
                self.ai_id,
                self.unit_id,
                self.supported_command_index,
                ptr as *mut *const i8,
                MAX_PARAMETERS as i32,
            )
        };

        Ok(String::from(
            unsafe { CStr::from_ptr(*ptr) }.to_str().unwrap_or(""),
        ))
    }

    pub fn all(&self) -> Result<UnitSupportedCommandAll, Box<dyn Error>> {
        Ok(UnitSupportedCommandAll {
            id: self.id()?,
            name: self.name()?,
            tooltip: self.tooltip()?,
            is_show_unique: self.is_show_unique()?,
            is_disabled: self.is_disabled()?,
            parameters: self.parameters()?,
        })
    }
}