gtether 0.1.0

Highly concurrent multiplayer focused game engine, with an emphasis on realtime streamable asset management.
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
use std::collections::HashMap;
use std::error::Error;
use std::fmt;
use std::fmt::{Debug, Formatter};

/// Validation check for parameter counts.
///
/// Can be used to validate that the count of parameters used for a command match certain
/// conditions.
///
/// # Examples
///
/// Simple usage:
/// ```
/// # use gtether::console::command::CommandError;
/// use gtether::console::command::ParamCountCheck;
///
/// # fn inner() -> Result<(), CommandError> {
/// let param_count = 5;
///
/// // Check parameter count equals 3
/// ParamCountCheck::Equal(3).check(param_count)?;
///
/// // Check parameter count is between 3 and 4
/// ParamCountCheck::Range {min: 3, max: 4}.check(param_count)?;
/// # Ok(())
/// # }
/// ```
///
/// Multiple conditions:
/// ```
/// # use gtether::console::command::CommandError;
/// use gtether::console::command::ParamCountCheck;
///
/// # fn inner() -> Result<(), CommandError> {
/// let param_count = 5;
///
/// // Check parameter count is one of:
/// //  - 2
/// //  - between 5 and 7 (inclusive)
/// //  - greater than or equal to 10
/// ParamCountCheck::OneOf(vec![
///     ParamCountCheck::Equal(2),
///     ParamCountCheck::Range {min: 5, max: 7},
///     ParamCountCheck::AtLeast(10),
/// ]).check(param_count)?;
/// ParamCountCheck::Equal(3).check(param_count)?;
/// # Ok(())
/// # }
/// ```
#[allow(unused)]
#[derive(Clone, Debug, PartialEq)]
pub enum ParamCountCheck {
    /// Parameter count should be equal the target.
    Equal(u32),
    /// Parameter count should be equal to or greater than the target.
    AtLeast(u32),
    /// Parameter count should be equal to or less than the target.
    AtMost(u32),
    /// Parameter count should be within the given range (inclusive).
    Range{ min: u32, max: u32 },
    /// Parameter count should match one of the given sub-conditions
    OneOf(Vec<Self>),
}

impl fmt::Display for ParamCountCheck {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            ParamCountCheck::Equal(target) =>
                write!(f, "x == {target}"),
            ParamCountCheck::AtLeast(target) =>
                write!(f, "{target} <= x"),
            ParamCountCheck::AtMost(target) =>
                write!(f, "x <= {target}"),
            ParamCountCheck::Range {min, max} =>
                write!(f, "{min} <= x <= {max}"),
            ParamCountCheck::OneOf(sub_conditions) => {
                for (idx, condition) in sub_conditions.iter().enumerate() {
                    if idx != 0 {
                        write!(f, " | ")?;
                    }
                    write!(f, "{condition}")?;
                }
                Ok(())
            }
        }
    }
}

impl ParamCountCheck {
    /// Check that the given count matches this condition.
    pub fn matches(&self, count: u32) -> bool {
        match self {
            ParamCountCheck::Equal(target) => &count == target,
            ParamCountCheck::AtLeast(target) => target <= &count,
            ParamCountCheck::AtMost(target) => &count <= target,
            ParamCountCheck::Range {min, max} => min <= &count && &count <= max,
            ParamCountCheck::OneOf(conditions) => conditions.iter()
                .any(|condition| condition.matches(count)),
        }
    }

    /// Check that the given count matches this condition, and create the appropriate [CommandError]
    /// if it doesn't.
    pub fn check(self, count: u32) -> Result<(), CommandError> {
        if !self.matches(count) {
            return Err(CommandError::InvalidParameterCount {actual: count, compare_op: self});
        }
        Ok(())
    }
}

/// Errors that can be encountered during command execution.
#[derive(Debug)]
pub enum CommandError {
    /// The given parameter is invalid for this command, or there is no command that matches said
    /// parameter.
    InvalidParameter(String),
    /// The count of parameters is invalid for this command.
    InvalidParameterCount{ actual: u32, compare_op: ParamCountCheck },
    /// Generalized catch-all for all other failure types.
    CommandFailure(Box<dyn Error>),
}

impl fmt::Display for CommandError {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            CommandError::InvalidParameter(parameter) =>
                write!(f, "Invalid command parameter: {parameter}"),
            CommandError::InvalidParameterCount {actual, compare_op } =>
                write!(f, "Parameter count ({actual}) does not match constraint(s): {compare_op}"),
            CommandError::CommandFailure(e) =>
                write!(f, "Command failed: {}", e.to_string()),
        }
    }
}

impl Error for CommandError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            CommandError::CommandFailure(e) => e.source(),
            _ => None,
        }
    }
}

/// Command that can be executed to trigger custom logic.
///
/// Must be Send + Sync.
pub trait Command: Debug + Send + Sync + 'static {
    /// Handle the execution of this command.
    ///
    /// Process the given parameters and execute the logic this command represents. If there are
    /// any issues, return a [CommandError].
    ///
    /// Note that implementations should _avoid_ panics if anything goes wrong, and prefer to report
    /// failures as [CommandError]s where possible.
    ///
    /// The input parameters are expected to NOT include the command name, but start after said
    /// command name.
    fn handle(&self, parameters: &[String]) -> Result<(), CommandError>;

    /// Retrieve auto-complete options for this command.
    ///
    /// Given any already partially written parameters, determine the collection of valid
    /// possibilities that the next (or current partially written) parameter could be.
    ///
    /// If auto-completion is undesired or unnecessary, it is safe to return an empty list.
    ///
    /// The input parameters are expected to NOT include the command name, but start after said
    /// command name.
    ///
    /// A blank string ("") should be used as the final parameter to indicate that all possibilities
    /// should be retrieved for that parameter; otherwise it is assumed that possibilities are
    /// wanted for the last parameter specified, fully written or not.
    #[allow(unused)]
    fn options(&self, parameters: &[String]) -> Vec<String> {
        vec![]
    }
}

/// Errors that can be encountered when registering commands.
#[derive(Debug)]
pub enum CommandRegisterError {
    /// A command already exists for the given name/key.
    CommandExists(String),
    /// No command exists for the given alias target.
    AliasTargetMissing(String),
}

impl fmt::Display for CommandRegisterError {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            CommandRegisterError::CommandExists(command) =>
                write!(f, "Command '{command}' already exists"),
            CommandRegisterError::AliasTargetMissing(command) =>
                write!(f, "No alias target '{command}' exists"),
        }
    }
}

impl Error for CommandRegisterError {}

/// Interface to register commands.
///
/// # Examples
/// ```
/// # use gtether::console::command::{Command, CommandRegistry};
/// # fn inner(registry: &mut impl CommandRegistry, command: Box<dyn Command>) {
/// // Register a command
/// registry.register_command("myCommand", command)
///     .expect("Command failed to register");
///
/// // Register an alias to a command
/// registry.register_alias("myAlias", "myCommand")
///     .expect("Alias failed to register");
/// # }
/// ```
pub trait CommandRegistry {
    /// Register a new command.
    ///
    /// Can fail to register if a command is already registered with the same key.
    fn register_command<S: Into<String>>(
        &mut self,
        key: S,
        command: Box<dyn Command>,
    ) -> Result<&mut Self, CommandRegisterError>;

    /// Register a new alias.
    ///
    /// Can fail to register if an alias is already registered with the same key, or if the alias
    /// target does NOT exist.
    fn register_alias<S: Into<String>, T: Into<String>>(
        &mut self,
        alias: S,
        key: T,
    ) -> Result<&mut Self, CommandRegisterError>;
}

#[derive(Debug)]
enum CommandEntry {
    COMMAND(Box<dyn Command>),
    ALIAS(String),
}

/// A collection of sub-commands.
///
/// A command that holds sub-commands, and delegates to sub-commands based on sub-parameters.
/// Implements both [Command] and [CommandRegistry].
///
/// # Examples
/// ```
/// # use gtether::console::command::{Command, CommandRegistry};
/// use gtether::console::command::CommandTree;
/// # fn inner(registry: &mut impl CommandRegistry, command_a: Box<dyn Command>, command_b: Box<dyn Command>) {
/// let mut command_tree = CommandTree::default();
/// command_tree
///     .register_command("a", command_a).unwrap()
///     .register_command("b", command_b).unwrap()
///     .register_alias("otherB", "b").unwrap();
/// registry.register_command("commands", Box::new(command_tree)).unwrap();
/// # }
/// ```
#[derive(Debug, Default)]
pub struct CommandTree {
    commands: HashMap<String, CommandEntry>,
}

impl CommandTree {
    fn get(&self, key: &String) -> Option<&Box<dyn Command>> {
        let entry = self.commands.get(key)?;
        match entry {
            CommandEntry::COMMAND(command) => Some(command),
            CommandEntry::ALIAS(alias) => self.get(alias),
        }
    }
}

impl CommandRegistry for CommandTree {
    fn register_command<S: Into<String>>(
        &mut self,
        key: S,
        command: Box<dyn Command>,
    ) -> Result<&mut Self, CommandRegisterError> {
        let key: String = key.into();

        if self.commands.contains_key(&key) {
            return Err(CommandRegisterError::CommandExists(key))
        }

        self.commands.insert(key, CommandEntry::COMMAND(command));
        Ok(self)
    }

    fn register_alias<S: Into<String>, T: Into<String>>(
        &mut self,
        alias: S,
        key: T,
    ) -> Result<&mut Self, CommandRegisterError> {
        let alias: String = alias.into();
        let key: String = key.into();

        if self.commands.contains_key(&alias) {
            return Err(CommandRegisterError::CommandExists(alias))
        } else if !self.commands.contains_key(&key) {
            return Err(CommandRegisterError::AliasTargetMissing(key))
        }

        self.commands.insert(alias, CommandEntry::ALIAS(key));
        Ok(self)
    }
}

impl Command for CommandTree {
    fn handle(&self, parameters: &[String]) -> Result<(), CommandError> {
        if parameters.len() < 1 {
            return Err(CommandError::InvalidParameter("".into()))
        }

        match self.get(&parameters[0]) {
            Some(command) => command.handle(&parameters[1..]),
            None => Err(CommandError::InvalidParameter(parameters[0].clone())),
        }
    }

    fn options(&self, parameters: &[String]) -> Vec<String> {
        if parameters.len() < 1 {
            return Vec::new()
        } else if parameters.len() == 1 {
            let mut options = self.commands.keys()
                .filter(|key| key.starts_with(&parameters[0]))
                .map(|key| key.clone())
                .collect::<Vec<_>>();
            options.sort();
            return options;
        }

        match self.get(&parameters[0]) {
            Some(command) => command.options(&parameters[1..]),
            None => Vec::new(),
        }
    }
}

#[cfg(test)]
mod tests {
    use std::fmt::{Display};
    use std::ptr;
    use parking_lot::RwLock;
    use super::*;

    #[derive(Default, Debug)]
    struct StoreCommand {
        value: RwLock<Option<String>>,
    }

    impl Command for StoreCommand {
        fn handle(&self, parameters: &[String]) -> Result<(), CommandError> {
            ParamCountCheck::Range{min: 1, max: 2}.check(parameters.len() as u32)?;

            match parameters[0].as_str() {
                "set" => {
                    ParamCountCheck::Equal(2).check(parameters.len() as u32)?;
                    *self.value.write() = Some(parameters[1].clone());
                    Ok(())
                },
                "clear" => {
                    ParamCountCheck::Equal(1).check(parameters.len() as u32)?;
                    *self.value.write() = None;
                    Ok(())
                },
                _ => Err(CommandError::InvalidParameter(parameters[0].clone())),
            }
        }

        fn options(&self, parameters: &[String]) -> Vec<String> {
            match parameters.len() {
                1 => ["set".into(), "clear".into()].into_iter()
                    .filter(|val: &String| val.starts_with(&parameters[0]))
                    .collect(),
                _ => vec![],
            }
        }
    }

    #[test]
    fn test_command_options() {
        let command = StoreCommand::default();

        assert_eq!(
            command.options(&["".into()]),
            vec!["set".to_owned(), "clear".to_owned()],
        );
        assert_eq!(
            command.options(&["set".into()]),
            vec!["set".to_owned()],
        );
        assert_eq!(
            command.options(&["cle".into()]),
            vec!["clear".to_owned()],
        );
        assert_eq!(
            command.options(&["invalid".into()]),
            Vec::<String>::new(),
        );
        assert_eq!(
            command.options(&["clear".into(), "extra".into()]),
            Vec::<String>::new(),
        )
    }

    #[test]
    fn test_command_success() {
        let command = StoreCommand::default();

        assert_eq!(command.value.read().clone(), None);

        command.handle(&["set".into(), "a".into()])
            .expect("Command should succeed");
        assert_eq!(command.value.read().clone(), Some("a".to_owned()));

        command.handle(&["set".into(), "b".into()])
            .expect("Command should succeed");
        assert_eq!(command.value.read().clone(), Some("b".to_owned()));

        command.handle(&["clear".into()])
            .expect("Command should succeed");
        assert_eq!(command.value.read().clone(), None);
    }

    #[test]
    fn test_command_invalid_params() {
        let command = StoreCommand::default();

        assert_matches!(
            command.handle(&[])
                .expect_err("Command should fail"),
            CommandError::InvalidParameterCount {actual: 0, compare_op: ParamCountCheck::Range{min: 1, max: 2}}
        );

        assert_matches!(
            command.handle(&["invalid".into()])
                .expect_err("Command should fail"),
            CommandError::InvalidParameter(param) => {
                assert_eq!(param, "invalid");
            }
        );

        assert_matches!(
            command.handle(&["set".into()])
                .expect_err("Command should fail"),
            CommandError::InvalidParameterCount {actual: 1, compare_op: ParamCountCheck::Equal(2)}
        );

        assert_matches!(
            command.handle(&["set".into(), "val".into(), "extra".into()])
                .expect_err("Command should fail"),
            CommandError::InvalidParameterCount {actual: 3, compare_op: ParamCountCheck::Range{min: 1, max: 2}}
        );

        assert_matches!(
            command.handle(&["clear".into(), "extra".into()])
                .expect_err("Command should fail"),
            CommandError::InvalidParameterCount {actual: 2, compare_op: ParamCountCheck::Equal(1)}
        );
    }

    #[derive(Debug)]
    struct CommandFailure;

    impl Display for CommandFailure {
        fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
            write!(f, "Command failed")
        }
    }

    impl Error for CommandFailure {}

    #[derive(Default, Debug)]
    struct FailCommand;

    impl Command for FailCommand {
        fn handle(&self, _parts: &[String]) -> Result<(), CommandError> {
            Err(CommandError::CommandFailure(Box::new(CommandFailure {})))
        }
    }

    #[test]
    fn test_command_failure() {
        let command = FailCommand::default();

        assert_matches!(
            command.handle(&[])
                .expect_err("Command should fail"),
            CommandError::CommandFailure(error) => {
                assert_eq!(error.to_string(), "Command failed".to_owned());
            }
        )
    }

    #[derive(Default, Debug)]
    struct NoOpCommand;

    impl Command for NoOpCommand {
        fn handle(&self, _parts: &[String]) -> Result<(), CommandError> {
            Ok(())
        }
    }

    fn create_command_map() -> CommandTree {
        let mut map = CommandTree::default();
        map.register_command("noop", Box::new(NoOpCommand::default()))
            .expect("Command should register")
            .register_alias("noop2", "noop")
            .expect("Alias to command should register")
            .register_alias("noop3", "noop2")
            .expect("Alias to alias should register");
        map
    }

    fn create_nested_command_map() -> CommandTree {
        let sub_map = create_command_map();

        let mut map = CommandTree::default();
        map.register_command("sub", Box::new(sub_map))
            .expect("Subcommand map should register");
        map.register_command("fail", Box::new(FailCommand::default()))
            .expect("Command should register");

        map
    }

    #[test]
    fn test_command_map_register_failure() {
        let mut commands = create_command_map();

        assert_matches!(
            commands.register_command("noop", Box::new(NoOpCommand::default()))
                .expect_err("Register should fail"),
            CommandRegisterError::CommandExists(command) => {
                assert_eq!(command, "noop");
            }
        );

        assert_matches!(
            commands.register_command("noop2", Box::new(NoOpCommand::default()))
                .expect_err("Register should fail"),
            CommandRegisterError::CommandExists(command) => {
                assert_eq!(command, "noop2");
            }
        );

        assert_matches!(
            commands.register_alias("new-alias", "non-existent")
                .expect_err("Register should fail"),
            CommandRegisterError::AliasTargetMissing(command) => {
                assert_eq!(command, "non-existent");
            }
        );
    }

    #[test]
    fn test_command_map_alias() {
        let commands = create_command_map();

        let command = commands.get(&"noop".into())
            .expect("Command should exist");
        let alias = commands.get(&"noop2".into())
            .expect("Alias should exist");
        let alias2 = commands.get(&"noop3".into())
            .expect("Alias to alias should exist");

        assert!(ptr::eq(command, alias));
        assert!(ptr::eq(command, alias2));
    }

    #[test]
    fn test_command_map_options() {
        let commands = create_nested_command_map();


        assert_eq!(
            commands.options(&["".into()]),
            vec!["fail".to_owned(), "sub".to_owned()],
        );
        assert_eq!(
            commands.options(&["sub".into()]),
            vec!["sub".to_owned()],
        );
        assert_eq!(
            commands.options(&["fa".into()]),
            vec!["fail".to_owned()],
        );
        assert_eq!(
            commands.options(&["invalid".into()]),
            Vec::<String>::new(),
        );
        assert_eq!(
            commands.options(&["sub".into(), "".into()]),
            vec!["noop".to_owned(), "noop2".to_owned(), "noop3".to_owned()],
        );
        assert_eq!(
            commands.options(&["sub".into(), "noo".into()]),
            vec!["noop".to_owned(), "noop2".to_owned(), "noop3".to_owned()],
        );
        assert_eq!(
            commands.options(&["sub".into(), "noop2".into()]),
            vec!["noop2".to_owned()],
        );
        assert_eq!(
            commands.options(&["sub".into(), "invalid".into()]),
            Vec::<String>::new(),
        );
        assert_eq!(
            commands.options(&["invalid".into(), "invalid".into()]),
            Vec::<String>::new(),
        );
    }

    #[test]
    fn test_command_map_handle() {
        let commands = create_nested_command_map();

        commands.handle(&["sub".into(), "noop".into()])
            .expect("Command should succeed");
        commands.handle(&["sub".into(), "noop2".into()])
            .expect("Command should succeed");
        commands.handle(&["sub".into(), "noop3".into()])
            .expect("Command should succeed");
        assert_matches!(
            commands.handle(&["sub".into(), "invalid".into()])
                .expect_err("Command should fail"),
            CommandError::InvalidParameter(param) => {
                assert_eq!(param, "invalid");
            }
        );

        assert_matches!(
            commands.handle(&["fail".into()])
                .expect_err("Command should fail"),
            CommandError::CommandFailure(error) => {
                assert_eq!(error.to_string(), "Command failed".to_owned());
            }
        );
        assert_matches!(
            commands.handle(&["invalid".into()])
                .expect_err("Command should fail"),
            CommandError::InvalidParameter(param) => {
                assert_eq!(param, "invalid");
            }
        );
    }
}