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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
use crate::*;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// The bit flags to describe plugin type.
///
/// Every plugin should provide a function `plugin_type`,
/// which returns [`PluginType`].
///
/// ```ignore
/// use ayaka_bindings::*;
///
/// #[export]
/// fn plugin_type() -> PluginType {
///     PluginType::default()
/// }
/// ```
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct PluginType {
    /// The action plugin.
    /// This plugin processes the action after they are parsed.
    pub action: bool,
    /// The text plugin.
    /// The custom text commands are dealt with this type of plugin.
    pub text: Vec<String>,
    /// The line plugin.
    /// The custom line types are dealt with this type of plugin.
    pub line: Vec<String>,
    /// The game plugin.
    /// This plugin processes the game properties after it is loaded.
    pub game: bool,
}

impl PluginType {
    /// Creates a [`PluginTypeBuilder`] instance to build a [`PluginType`].
    pub fn builder() -> PluginTypeBuilder {
        PluginTypeBuilder {
            data: Self::default(),
        }
    }
}

/// The builder of [`PluginType`].
pub struct PluginTypeBuilder {
    data: PluginType,
}

impl PluginTypeBuilder {
    /// An action plugin.
    pub fn action(mut self) -> Self {
        self.data.action = true;
        self
    }

    /// A text plugin, which provides commands.
    pub fn text(mut self, cmds: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.data.text = cmds.into_iter().map(|s| s.into()).collect();
        self
    }

    /// A line plugins, which provides custom line types.
    pub fn line(mut self, cmds: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.data.line = cmds.into_iter().map(|s| s.into()).collect();
        self
    }

    /// A game plugin.
    pub fn game(mut self) -> Self {
        self.data.game = true;
        self
    }

    /// Build a [`PluginType`].
    pub fn build(self) -> PluginType {
        self.data
    }
}

/// The type of current frontend.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum FrontendType {
    /// The frontend only accepts raw texts.
    Text,
    /// The frontend renders HTML.
    Html,
    /// The frontend renders LaTeX.
    Latex,
}

/// The argument to action plugin.
///
/// Every action plugin should implement `process_action`:
/// ```ignore
/// use ayaka_bindings::*;
///
/// #[export]
/// fn process_action(mut ctx: ActionProcessContext) -> ActionProcessResult {
///     // Process the action...
///     ActionProcessResult { action: ctx.action }
/// }
/// ```
#[derive(Debug, Serialize, Deserialize)]
pub struct ActionProcessContext {
    /// The global properties of the game profile.
    pub game_props: HashMap<String, String>,
    /// The frontend type.
    pub frontend: FrontendType,
    /// The current context.
    pub ctx: RawContext,
    /// The current action.
    pub action: ActionText,
}

#[derive(Debug, Serialize)]
#[doc(hidden)]
pub struct ActionProcessContextRef<'a> {
    pub game_props: &'a HashMap<String, String>,
    pub frontend: FrontendType,
    pub ctx: &'a RawContext,
    pub action: &'a ActionText,
}

/// The result of action plugins.
/// See examples at [`ActionProcessContext`].
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct ActionProcessResult {
    /// The processed action text.
    pub action: ActionText,
}

/// The argument to text plugin.
///
/// ```ignore
/// use ayaka_bindings::*;
///
/// #[export]
/// fn plugin_type() -> PluginType {
///     PluginType::builder().text(&["hello"]).build()
/// }
///
/// #[export]
/// fn hello(_args: Vec<String>, _ctx: TextProcessContext) -> TextProcessResult {
///     let mut res = TextProcessResult::default();
///     res.line.push_back_chars("hello");
///     res
/// }
/// ```
#[derive(Debug, Serialize, Deserialize)]
pub struct TextProcessContext {
    /// The global properties of the game profile.
    pub game_props: HashMap<String, String>,
    /// The frontend type.
    pub frontend: FrontendType,
}

#[derive(Debug, Serialize)]
#[doc(hidden)]
pub struct TextProcessContextRef<'a> {
    pub game_props: &'a HashMap<String, String>,
    pub frontend: FrontendType,
}

/// The result of commands in text plugins.
/// See examples at [`TextProcessContext`].
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct TextProcessResult {
    /// The lines to append.
    pub text: ActionText,
}

/// The argument to game plugin.
///
/// Every game plugin should implement `process_game`:
/// ```ignore
/// use ayaka_bindings::*;
///
/// #[export]
/// fn process_game(mut ctx: GameProcessContext) -> GameProcessResult {
///     // Process the game...
///     GameProcessResult { props: ctx.props }
/// }
/// ```
#[derive(Debug, Serialize, Deserialize)]
pub struct GameProcessContext {
    /// The title of the game.
    pub title: String,
    /// The author of the game.
    pub author: String,
    /// The global properties of the game.
    pub props: HashMap<String, String>,
}

#[derive(Debug, Serialize)]
#[doc(hidden)]
pub struct GameProcessContextRef<'a> {
    pub title: &'a str,
    pub author: &'a str,
    pub props: &'a HashMap<String, String>,
}

/// The result of game plugins.
/// See examples at [`GameProcessContext`].
#[derive(Debug, Serialize, Deserialize)]
pub struct GameProcessResult {
    /// The updated properties.
    pub props: HashMap<String, String>,
}

/// The argument to line plugin.
///
/// ```ignore
/// use ayaka_bindings::*;
///
/// #[export]
/// fn plugin_type() -> PluginType {
///     PluginType::builder().line(&["hello"]).build()
/// }
///
/// #[export]
/// fn hello(_ctx: LineProcessContext) -> LineProcessResult {
///     let mut res = LineProcessResult::default();
///     res.locals.insert("hello".to_string(), RawValue::Str("world".to_string()));
///     res
/// }
/// ```
#[derive(Debug, Serialize, Deserialize)]
pub struct LineProcessContext {
    /// The global properties of the game profile.
    pub game_props: HashMap<String, String>,
    /// The frontend type.
    pub frontend: FrontendType,
    /// The current context.
    pub ctx: RawContext,
    /// The full properties of the custom command.
    pub props: VarMap,
}

#[derive(Debug, Serialize)]
#[doc(hidden)]
pub struct LineProcessContextRef<'a> {
    pub game_props: &'a HashMap<String, String>,
    pub frontend: FrontendType,
    pub ctx: &'a RawContext,
    pub props: &'a VarMap,
}

/// The result of commands in line plugins.
/// See examples at [`LineProcessContext`].
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct LineProcessResult {
    /// The updated variables.
    pub locals: VarMap,
    /// The temp variables.
    pub vars: VarMap,
}