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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
use serde::{Deserialize, Serialize};
use crate::{Resource, SerializedData};
use yy_typings::{ViewPath, ViewPathLocation};

/// The type of command to give, pertaining to each of the general areas the YyBoss can give.
///
/// All commands return an [`Output`] with a `Command` tag except [`Shutdown`], which will return
/// an output with a [`Shutdown`] tag on it instead, after which the server will shutdown.
///
/// [`Output`]: ../output/enum.Output.html
/// [`Shutdown`]: ./struct.Shutdown.html
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum Command {
    /// A command type pertaining to Resources. To see the subcommand for resources, see
    /// [`ResourceCommand`].
    ///
    /// [`ResourceCommand`]: ./struct.ResourceCommand.html
    Resource(ResourceCommand),

    /// A command type pertaining to the Virtual Filesystem. To see the subcommand for the VFS, see
    /// [`VfsCommand`].
    ///
    /// [`VfsCommand`]: ./enum.VfsCommand.html
    VirtualFileSystem(VfsCommand),

    /// Various utilities to aid developers working with files. These commands help work with generated data.
    ///
    /// [`CreateCommand`]: ./struct.CreateCommand.html
    Utilities(UtilityCommand),

    /// A command type to serialize current changes. This currently serializes all changes which the YypBoss
    /// tracks, including Assets and Pipelines.
    Serialize,

    /// A command type to shutdown the YypBoss. If there are changes to be made, they will be permanently lost.
    Shutdown,
}

/// A resource command, which will allow users to read and write resources
/// into the YypBoss.
///
/// The subtype of command, such as [`Add`] or [`Remove`] is indicated by the [`command_type`]
/// subfield.
///
/// The Resource type to manipulate is given by the [`resource`] field.
///
/// Each [`resource`] paired with each [`command_type`], indicates which of the optional fields is required. If
/// an optional field is not given, but should have been, this command will abort.
///
/// [`Add`]: ./enum.ResourceCommandType.html#variant.Add
/// [`Remove`]: ./enum.ResourceCommandType.html#variant.Remove
/// [`command_type`]: #structfield.command_type
/// [`resource`]: #structfield.command_type
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(rename = "subCommand")]
#[serde(rename_all = "camelCase")]
pub struct ResourceCommand {
    /// The command type for this ResourceCommand.
    #[serde(flatten)]
    pub command_type: ResourceCommandType,

    /// The type of resource to interact with.
    pub resource: Resource,
}

/// The command type to run.
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(tag = "subCommand")]
pub enum ResourceCommandType {
    /// Adds a resource to the project.
    ///
    /// ## Errors
    /// If there is a resource by the name already, this command will abort and return
    /// an error.
    ///
    /// ## Returns
    /// If it succeeds, it will return without any extra data, like a `void`.
    Add(NewResource),

    /// Removes and returns the resource.
    ///
    /// ## Errors
    /// If there isn't a resource by that name of the type given, it will return an error.
    ///
    /// ## Returns
    /// If this command succeeds, it will return the resource and its associated data
    /// after having removed it.
    Remove {
        /// The name of the resource to remove.
        identifier: String,
    },

    /// Renames a resource.
    ///
    /// ## Errors
    /// If there isn't a resource by that name of the type given, it will return an error.
    ///
    /// ## Returns
    /// Returns a `CommandOutput` with no additional data on success.
    #[serde(rename_all = "camelCase")]
    Rename {
        identifier: String,
        new_name: String,
    },

    /// Returns a copy of a resource.
    ///
    /// ## Errors
    /// If there isn't a resource by the given name of the given type, an error will be returned.
    ///
    /// ## Returns
    /// If this command succeeds, it will return a copy of the resource.
    /// This command will not mutate any data in the project.
    Get {
        /// The name of the resource to get.
        identifier: String,
    },

    /// Returns a copy of a resource's associated data as SerializedData in a presentable form. This
    /// generally means making keys in HashMaps JSON compatible. Further engineering work is needed
    /// to clarify this.
    ///
    /// If the associated data for a given identifier has not been loaded, it will be loaded.
    ///
    /// ## Errors
    /// If there isn't a resource by the given name of the given type, an error will be returned.
    /// If there is an error loading the resource's associated data, an error will be returned.
    ///
    /// ## Returns
    /// If this command succeeds, it will return a copy of the resource.
    GetAssociatedData {
        /// The name of the resource to get.
        identifier: String,

        /// If the resource associated data should be reloaded from disc.
        /// If the associated data has not been loaded, the resource will be loaded from
        /// disc anyway, so this flag is effectively ignored.
        force: bool,
    },

    /// Returns a boolean indicating if a resource of the given name and given type exists.
    /// This command is a shortcut for performance reasons over [`Get`], since no string writing and
    /// serialization/deserialization will be required.
    ///
    /// ## Errors
    /// This command is infallible.
    ///
    /// ## Returns
    /// This command will return `true` if a resource of the given name and given type exists,
    /// and `false` otherwise.
    ///
    /// [`Get`]: #variant.Get
    Exists {
        /// The name of the resource to check if it exists.
        identifier: String,
    },
}

/// This struct describes the new data needed to [`Add`], [`Replace`], or [`Set`] a resource
/// in the [`ResourceCommandType`].
///
/// The types of the Data required for [`new_resource`] and [`associated_data`] are written in the table
/// below for convenience, but for the absolute answer, look in the [`resources_ext`] module.
///
/// ## Types of Each Field
///|   Resource Type  |   new_resource     | associated_data   |
///|------------------|-----------------------|----------------------|
///| [`Sprite`]       |  [`Sprite Yy File`]   | [`HashMap`]<[`Frame Uuid`], [`SpriteImageBuffer`]>  |
///| [`Object`]       |  [`Object Yy File`]   | [`HashMap`]<[`EventType`], [`String`]>              |
///| [`Script`]       |  [`Script Yy File`]   | String                                       |
///| [`Shader`]       |  [`Shader Yy File`]   | [`ShaderScriptType`] |
///
/// **NB:** Above, "Single-Void" means that a given Map must have only one key (if multiple are present,
/// the command will abort with an error), and the contents of the key do not matter. Using `data` might
/// be a good idea for users, but any name is fine.
///
/// [`Add`]: ./enum.ResourceCommandType.html#variant.Add
/// [`Replace`]: ./enum.ResourceCommandType.html#variant.Replace
/// [`Set`]: ./enum.ResourceCommandType.html#variant.Set
/// [`ResourceCommandType`]: ./enum.ResourceCommandType.html
/// [`new_resource`]: #structfield.new_resource
/// [`associated_data`]: #structfield.associated_data
/// [`Sprite`]: ./enum.Resource.html#variant.Sprite
/// [`Object`]: ./enum.Resource.html#variant.Object
/// [`Script`]: ./enum.Resource.html#variant.Script
/// [`Shader`]: ./enum.Resource.html#variant.Shader
/// [`Sprite Yy File`]: ../../yy_typings/sprite_yy/struct.Sprite.html
/// [`Object Yy File`]: ../../yy_typings/object_yy/struct.Object.html
/// [`Script Yy File`]: ../../yy_typings/struct.Script.html
/// [`Shader Yy File`]: ./error.html
/// [`Frame Uuid`]: ../../yy_typings/sprite_yy/struct.Frame.html#structfield.name
/// [`EventType`]: ../../yy_typings/sprite_yy/object_yy/enum.EventType.html
/// [`ShaderScriptType`]: ./error.html
/// [`HashMap`]: ../../../std/collects/struct.HashMap.html
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NewResource {
    /// This field must contain the Data of a Yy File to add for the given resource.
    ///
    /// See the chart in [`NewResource`] above for more details.
    ///
    /// [`NewResource`]: ./struct.NewResource.html
    pub new_resource: SerializedData,

    /// This fields must contain the Associated Data of a given Yy File.
    ///
    /// See the chart in [`NewResource`] above for more details of which
    /// types of associated data are expected.
    ///
    /// [`NewResource`]: ./struct.NewResource.html
    pub associated_data: SerializedData,
}

/// The Virtual File System command type to run.
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(tag = "subCommand")]
pub enum VfsCommand {
    /// An instruction to move a Resource from one location to another.
    ///
    /// ## Errors
    /// If the [`resource_to_move`] field is not set to a valid resource, or
    /// if [`new_parent`] is not set to a valid folder, this command aborts and
    /// returns an error.
    ///
    /// [`resource_to_move`]: #structfield.resource_to_move
    /// [`new_parent`]: #structfield.new_parent
    #[serde(rename_all = "camelCase")]
    MoveResource {
        /// The name of the Resource to move.
        resource_to_move: String,
        /// The kind of the Resource to move.
        resource: Resource,
        /// The new parent of the Resource, which must be a folder.
        new_parent: ViewPath,
    },

    /// An instruction to create a Folder under the specified folder path.
    ///
    /// ## Errors
    /// If the [`parent_folder`] field is not set to a valid folder, or
    /// if [`folder_name`] is not a unique child of [`parent_folder`], this command aborts and
    /// returns an error.
    ///
    /// [`parent_folder`]: #structfield.parent_folder
    /// [`folder_name`]: #structfield.folder_name
    #[serde(rename_all = "camelCase")]
    CreateFolder {
        /// The identity of the Parent to add a folder to.
        parent_folder: ViewPathLocation,
        /// The name of the new folder to create.
        folder_name: String,
        // /// An optional order field -- if `None` is given, then no folder will be created.
        // /// Currently, we only support `None`.
        // order: Option<usize>,
    },

    /// An instruction to move a Folder from one location to another, along with the
    /// resources within it.
    ///
    /// ## Errors
    /// If the [`folder_to_move`] or [`new_parent`] fields are not set to a valid folder,
    /// this command aborts and returns an error. If `new_parent` is a folder within `folder_to_move`,
    /// this command will abort and return a error.
    ///
    /// [`folder_to_move`]: #structfield.folder_to_move
    /// [`new_parent`]: #structfield.new_parent
    #[serde(rename_all = "camelCase")]
    MoveFolder {
        /// The location of the Resource to move.
        folder: ViewPathLocation,
        /// The new parent of the Resource, which must be a folder.
        new_parent: ViewPathLocation,
    },

    /// Deletes a folder.
    ///
    /// If the folder is not empty, then the `recursive` flag must be passed -- otherwise, this command
    /// will abort.
    #[serde(rename_all = "camelCase")]
    RemoveFolder {
        /// The location of the Folder to remove.
        folder: ViewPathLocation,

        /// If the folder is not empty, and this flag is not set to true, then the command will abort with
        /// an error.
        ///
        /// Since the YypBoss is modeled as a black box, there is no notion of "force" -- otherwise, this command
        /// is similar to `rm -rf` on Unix systems.
        recursive: bool,
    },

    /// Renames a folder.
    #[serde(rename_all = "camelCase")]
    RenameFolder {
        /// The location of the Folder to rename.
        folder: ViewPathLocation,

        /// The new name of the Folder.
        new_name: String,
    },

    /// Returns a [`FolderGraph`] for this folder.
    ///
    /// ## Errors
    /// If the [`ViewPathLocation`] provided does not describe a valid Folder, this command aborts and returns an error.
    GetFolder { folder: ViewPathLocation },

    /// Returns a [`FolderGraph`] for the entire Virtual File System.
    /// Please note, this can result in a fairly massive Json being sent back.
    ///
    /// ## Errors
    /// This command is infallible.
    GetFullVfs,

    /// Returns an enum if the given ViewPath links to a Folder or a File.
    ///
    /// ## Errors
    /// If the [`ViewPath`] provided does not describe a valid Item, this command aborts and returns an error.
    GetPathType { path: ViewPath },
}

/// Utilities for the YypBoss to run. None of these commands will ever return an error.
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(tag = "subCommand")]
pub enum UtilityCommand {
    /// Ask the project for basic info on how it should be ran.
    ///
    /// This will return a `ProjectMetaData` struct as SerializedDataOutput. This is the same
    /// struct which is returned in the initial startup
    ProjectInfo,

    Create(CreateCommand),

    #[serde(rename_all = "camelCase")]
    PrettyEventNames {
        event_names: Vec<String>,
    },

    #[serde(rename_all = "camelCase")]
    CreateEvent {
        identifier: String,
        event_file_name: String,
    },

    #[serde(rename_all = "camelCase")]
    DeleteEvent {
        identifier: String,
        event_file_name: String,
    },

    #[serde(rename_all = "camelCase")]
    ScriptGmlPath {
        script_name: String,
    },

    #[serde(rename_all = "camelCase")]
    EventGmlPath {
        object_name: String,
        event_file_name: String,
    },

    #[serde(rename_all = "camelCase")]
    CanUseResourceName {
        identifier: String,
    },

    #[serde(rename_all = "camelCase")]
    CanUseFolderName {
        parent_folder: ViewPathLocation,
        identifier: String,
    },
}

/// A create command for the Yy to process.
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct CreateCommand {
    pub resource: Resource,
    pub name: Option<String>,
    pub parent: Option<ViewPath>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    #[test]
    fn test() {
        fn harness(command: Command) {
            let command_str = serde_json::to_string_pretty(&command).unwrap();
            println!("{}", command_str);

            let new_command: Command = serde_json::from_str(&command_str).unwrap();
            assert_eq!(new_command, command);
        }
        harness(Command::Resource(ResourceCommand {
            command_type: ResourceCommandType::Add(NewResource {
                new_resource: SerializedData::Value {
                    data: "Hello".to_string(),
                },
                associated_data: SerializedData::Value {
                    data: json!({
                        "test": "10"
                    })
                    .to_string(),
                },
            }),
            resource: Resource::Script,
        }));

        harness(Command::Resource(ResourceCommand {
            command_type: ResourceCommandType::Get {
                identifier: "Something".to_string(),
            },
            resource: Resource::Sprite,
        }));

        harness(Command::Utilities(UtilityCommand::Create(CreateCommand {
            resource: Resource::Script,
            name: Some("jim".to_string()),
            parent: None,
        })));

        harness(Command::Utilities(UtilityCommand::PrettyEventNames {
            event_names: vec!["hello".to_string()],
        }));

        harness(Command::VirtualFileSystem(VfsCommand::MoveResource {
            resource_to_move: "jim".to_string(),
            resource: Resource::Script,
            new_parent: ViewPath::default(),
        }));

        harness(Command::VirtualFileSystem(VfsCommand::RemoveFolder {
            recursive: true,
            folder: ViewPathLocation::new("okay"),
        }));

        harness(Command::VirtualFileSystem(VfsCommand::GetFolder {
            folder: ViewPathLocation::default(),
        }));

        harness(Command::VirtualFileSystem(VfsCommand::GetFullVfs));
        harness(Command::VirtualFileSystem(VfsCommand::GetPathType {
            path: ViewPath::default(),
        }));
    }
}