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
use std::{error::Error, ffi::CStr};

use crate::get_callback;

const MAX_PARAMETERS: usize = 32;

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

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

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

        Ok(unsafe {
            get_supported_command_id_func(self.ai_id, self.group_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, Group_SupportedCommand_getName)?;

        Ok(String::from(
            unsafe {
                CStr::from_ptr(get_supported_command_name_func(
                    self.ai_id,
                    self.group_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, Group_SupportedCommand_getToolTip)?;

        Ok(String::from(
            unsafe {
                CStr::from_ptr(get_supported_command_tooltip_func(
                    self.ai_id,
                    self.group_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, Group_SupportedCommand_isShowUnique)?;

        Ok(unsafe {
            get_suppored_command_is_show_unique_func(
                self.ai_id,
                self.group_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, Group_SupportedCommand_isDisabled)?;

        Ok(unsafe {
            get_supported_command_is_disabled_func(
                self.ai_id,
                self.group_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, Group_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.group_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<GroupSupportedCommandAll, Box<dyn Error>> {
        Ok(GroupSupportedCommandAll {
            id: self.id()?,
            name: self.name()?,
            tooltip: self.tooltip()?,
            is_show_unique: self.is_show_unique()?,
            is_disabled: self.is_disabled()?,
            parameters: self.parameters()?,
        })
    }
}