parade-rs 2.0.0

Rust rewrite of Parade - an experimental interactive-fiction playground / filesystem / operating system?
Documentation
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
use std::{
    fmt::{Display, Formatter},
    str::FromStr,
};

use itertools::Itertools;
use serde::{Deserialize, Serialize};

use strum::{Display, EnumIter, EnumString, IntoEnumIterator};

use crate::vessel::Article;

/// The name of an [Action], for use in parsing and help messages.
#[derive(
    Clone,
    Copy,
    Debug,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Serialize,
    Deserialize,
    EnumIter,
    EnumString,
    Display,
)]
#[strum(ascii_case_insensitive)]
pub enum Verb {
    /// Displays general information on Parade, or information on a specific action or topic.
    Learn,
    /// Creates a new vessel adjacent to the user's.
    Create,
    /// Takes control of an adjacent vessel.
    Become,
    /// Moves the controlled vessel into an adjacent vessel.
    Enter,
    /// Moves the controlled vessel out of its parent, into its parent's parent.
    Leave,
    /// Moves an adjacent vessel into the controlled vessel.
    Take,
    /// Moves a vessel inside the controlled vessel into the controlled vessel's parent.
    Drop,
    /// Moves an adjacent vessel into another adjacent vessel.
    Move,
    /// Displays a list of the adjacent vessels, or of the vessels inside an adjacent vessel.
    Look,
    /// Renames the controlled vessel.
    Transform,
    /// Assigns a description to the parent of the controlled vessel.
    Note,
    /// Moves the controlled vessel into another vessel, regardless of adjacency.
    Warp,
    /// Begins programming a task for an adjacent vessel.
    Program,
    /// Executes an adjacent vessel's program.
    Use,
    /// Executes a distant vessel's program as another vessel.
    Cast,
    /// Exits parade, or stops programming a vessel if the user is doing so.
    Exit,
    /// Saves the state of all vessels to a file.
    Save,
    /// Loads the state of all vessels from a file.
    Load,
    /// Displays the state of all vessels as raw json data.
    Debug,
}
impl Verb {
    /// Returns the short help text for the given [Verb].
    pub fn short_help_text(&self) -> String {
        match self {
            Verb::Learn =>       r#"Learn [about] [action]:              Display general information on Parade, or information on a specific action or topic."#,
            Verb::Create =>      r#"Create <vessel>:                     Create a new vessel adjacent to yours."#,
            Verb::Become =>      r#"Become <vessel>:                     Take control of an adjacent vessel."#,
            Verb::Enter =>       r#"Enter <vessel>:                      Move your vessel into an adjacent vessel."#,
            Verb::Leave =>       r#"Leave:                               Move your vessel out of the vessel it is in (unless it is inside itself)."#,
            Verb::Take =>        r#"Take <vessel>:                       Move an adjacent vessel inside yours."#,
            Verb::Drop =>        r#"Drop <vessel>:                       Move a vessel inside yours outside, so it is adjacent to yours."#,
            Verb::Move =>        r#"Move <vessel> <to/into> <vessel>:    Move an adjacent vessel into another adjacent vessel."#,
            Verb::Look =>        r#"Look [[at/in] <vessel>]:             See a list of the adjacent vessels, or of the vessels inside an adjacent vessel."#,
            Verb::Transform =>   r#"Transform [to/into] <vessel>:        Change your vessel's name."#,
            Verb::Note =>        r#"Note [text]:                         Assign a description to the vessel you are inside."#,
            Verb::Warp =>        r#"Warp [to/into] <vessel>:             Move your vessel into another vessel, anywhere."#,
            Verb::Program =>     r#"Program [vessel]:                    Begin programming a task for a vessel, which can be activated with Use or Cast."#,
            Verb::Use =>         r#"Use <vessel>:                        Execute an adjacent vessel's program."#,
            Verb::Cast =>        r#"Cast <vessel> <at/on> <vessel>:      Executes a distant vessel's program as another vessel."#,
            Verb::Exit =>        r#"Exit:                                Exit parade, or stop programming a vessel if you're doing so."#,
            Verb::Save =>        r#"Save [filename]:                     Save the state of all vessels to a file."#,
            Verb::Load =>        r#"Load [filename]:                     Load the state of all vessels from a file."#,
            Verb::Debug =>       r#"Debug:                               Display the state of all vessels as raw json data."#,
        }.to_string()
    }
    /// Returns the long help text for the given [Verb].
    pub fn help_text(&self) -> String {
        match self {
            Verb::Learn => r#"Learn [about] [Action]: Learn about Parade.

If used on its own, explains what Parade is, how to use it, and lists the available actions.
If used with an action, displays more in-depth information on that action.

Action descriptions begin with their parameters.
Parameters in square brackets (e.g. "[Action]") are optional, whereas those in angle brackets ("<noun>") are required."#,
            Verb::Create => r#"Create [article] <noun>: Create a new vessel adjacent to yours.

"Adjacent", here, means that it is inside the same parent vessel.
This action will not work if there is already a vessel in your world with the same name as the vessel you are trying to create. This applies regardless of where in your world the existing vessel is.

Vessel names can contain any number of words: You can create a vessel named "pink pony", and it will be distinct from a vessel named just "pony".

If an article is provided, parade will refer to the vessel using that article: e.g. "the library", "a ghost", etc.
If no article is provided, parade will refer to the vessel without an article: e.g. "Parade", "Jack the Ripper", etc."#,
            Verb::Become => r#"Become [article] <noun>: Take control of an adjacent vessel.

"Adjacent", here, means that it is inside the same parent vessel.
This action will not work if the selected vessel does not exist, or is not adjacent to yours.

This does not change your vessel or the selected vessel in any way, aside from changing which one you are controlling."#,
            Verb::Enter => r#"Enter [article] <noun>: Move your vessel into an adjacent vessel.

"Adjacent", here, means that it is inside the same parent vessel as yours.
This action will not work if the selected vessel does not exist, or is not adjacent to yours."#,
            Verb::Leave => r#"Leave: Move your vessel out of the vessel it is in.

If your vessel is inside itself, this will do nothing."#,
            Verb::Take => r#"Take [article] <noun>: Move an adjacent vessel inside yours.

"Adjacent", here, means that it is inside the same parent vessel.
This action will not work if the selected vessel does not exist, or is not adjacent to yours."#,
            Verb::Drop => r#"Drop [article] <noun>: Move a vessel inside yours outside, so it is adjacent to yours.

"Adjacent", here, means that it is inside the same parent vessel.
This action will not work if the selected vessel does not exist, or is not inside yours."#,
            Verb::Move => r#"Move [article] <noun> <preposition> [article] <noun>: Move an adjacent vessel into another adjacent vessel.

"Adjacent", here, means that it is inside the same parent vessel.
This action will not work if either of the selected vessels do not exist, or are not adjacent to yours.

Because vessel names can contain any number of words, you must separate the two vessel names a preposition.
The prepositions available here are "to" and "into"."#,
            Verb::Look => r#"Look [[preposition] [article] <noun>]: See a list of the vessels adjacent to yours or inside the selected adjacent vessel.

"Adjacent", here, means that it is inside the same parent vessel.
This action will not work if the selected vessel does not exist, or is not adjacent to yours.

If used on its own, this will display a list of the adjacent vessels.
If used with a noun, this will display a list of the vessels inside it.

The prepositions available here are "at" and "in"."#,
            Verb::Transform => r#"Transform [preposition] [article] <noun>: Change your vessel's name to the provided name.

This action will not work if there is already a vessel in your world with the same name as the one you are trying to take. This applies regardless of where in your world the existing vessel is.

The prepositions available here are "into" and "to"."#,
            Verb::Note => r#"Note [text]: Assign a description to the vessel you are inside.

The description will be displayed to anyone inside the vessel, or who Looks at it."#,
            Verb::Warp => r#"Warp [preposition] [article] <noun>: Move your vessel into another vessel, anywhere.

This can be used to move your vessel inside itself.

This action will not work if the selected vessel does not exist.

The prepositions available here are "into" and "to"."#,
            Verb::Program => r#"Program [article] [noun]: Begin programming a task for a vessel, which can be activated with Use.

If a vessel is provided, it must be adjacent. If no vessel is provided, the parent vessel will be selected.

"Adjacent", here, means that it is inside the same parent vessel.
This action will not work if the selected vessel does not exist, or is not adjacent to yours.

While programming a vessel, all actions will be added to its "program", instead of being executed normally.
The only exception to this is Exit, which will cause you to stop programming the vessel - Unless you're currently programming the vessel to program another vessel. You can nest programs as much as you like.

Nouns mentioned in actions you add to a program are evaluated when you run the program, instead of when you add them to it.
This means that you can reference vessels which do not currently exist or which are not yet accessible."#,
            Verb::Use => r#"Use [article] <noun>: Execute an adjacent vessel's program.

"Adjacent", here, means that it is inside the same parent vessel.
This action will not work if the selected vessel does not exist, if it is not adjacent to yours, or it if does not have a program.

No actions used by the program will display information to you, and the Save and Load actions will do nothing.
When the program executes, it executes with control of the program's vessel, not yours. It can still, however, Become other vessels."#,
            Verb::Cast => r#"Cast [article] <noun> <preposition> [article] <noun>: Execute a vessel's program as an adjacent vessel.

"Adjacent", here, means that it is inside the same parent vessel.
This action will not work if either of the selected vessels do not exist, if the first vessel does not have a program, or if the second vessel is not adjacent to yours.

The first named vessel is the vessel whose program will be cast, and the second named vessel is the vessel which will have the program cast on/as it.

This action otherwise functions as Use.

The prepositions available here are "at" and "on"."#,
            Verb::Exit => r#"Exit: Exit parade, or stop programming a vessel if you're doing so.

Exits parade if you are outside of the Program interface.
If you are inside the Program interface, exits it.
If you are programming a vessel to program another vessel, this will add "Exit" to the former vessel's program.
Parade-rs supports unlimited nesting of programs."#,
            Verb::Save => r#"Save [text]: Save the state of all vessels to a file.

If a name is provided, this will save to the file with that name.
Otherwise, the name "parade_save.json" will be used."#,
            Verb::Load => r#"Load [text]: Load the state of all vessels from a file.

If a name is provided, this will save to the file with that name.
Otherwise, the name "parade_save.json" will be used."#,
            Verb::Debug => r#"Debug: Display the state of all vessels as raw json data."#,
        }.to_string()
    }
    /// Returns the a basic help message, alongside the [Self::short_help_text] for every [Verb].
    pub fn help_list_text() -> String {
        Verb::iter().fold(r#"This is Parade (or, specifically, parade-rs).

Parade is an experimental interactive-fiction playground, or maybe a filesystem or something.
Parade-rs is based on paradise (https://wiki.xxiivv.com/site/paradise.html) and parade (https://wiki.xxiivv.com/site/parade.html).

You interact with parade using the following actions:
"#.to_string(), |acc, topic| {
            format!("{acc}- {}\n", topic.short_help_text())
        })
    }
}

/// An action to be executed by the system interface.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum Action {
    /// Creates a new vessel adjacent to the user's.
    Create(VesselRequest),
    /// Takes control of an adjacent vessel.
    Become(VesselRequest),
    /// Moves the controlled vessel into an adjacent vessel.
    Enter(VesselRequest),
    /// Moves the controlled vessel out of its parent, into its parent's parent.
    Leave,
    /// Moves an adjacent vessel into the controlled vessel.
    Take(VesselRequest),
    /// Moves a vessel inside the controlled vessel into the controlled vessel's parent.
    Drop(VesselRequest),
    /// Moves an adjacent vessel into another adjacent vessel.
    Move {
        /// The optional [Article] with which to refer to the object being moved.
        object_article: Option<Article>,
        /// The name/key of the object being moved.
        object_name: String,
        /// The preposition separating the two [crate::vessel::Vessel]s.
        preposition: IntoPrep,
        /// The optional [Article] with which to refer to the target being moved to.
        target_article: Option<Article>,
        /// The name/key of the target being moved to.
        target_name: String,
    },
    /// Displays a list of the adjacent vessels, or of the vessels inside an adjacent vessel.
    Look {
        /// An optional preposition before the [crate::vessel::Vessel] being specified.
        preposition: Option<AtPrep>,
        /// An optional set of identifiers for a [crate::vessel::Vessel] to look at/inside.
        basic: Option<VesselRequest>,
    },
    /// Renames the controlled vessel.
    Transform {
        /// An optional preposition before the [crate::vessel::Vessel] being specified.
        preposition: Option<IntoPrep>,
        /// A set of identifiers for the [crate::vessel::Vessel]'s new name and [Article].
        basic: VesselRequest,
    },
    /// Assigns a description to the parent of the controlled vessel.
    Note {
        /// The description text, to which the note will be set.
        text: Option<String>,
    },
    /// Moves the controlled vessel into another vessel, regardless of adjacency.
    Warp {
        /// An optional preposition before the [crate::vessel::Vessel] being specified.
        preposition: Option<IntoPrep>,
        /// A set of identifiers for the [crate::vessel::Vessel] being moved to.
        basic: VesselRequest,
    },
    /// Begins programming a task for an adjacent vessel.
    Program(Option<VesselRequest>),
    /// Executes an adjacent vessel's program.
    Use(VesselRequest),
    /// Executes a distant vessel's program as another vessel.
    Cast {
        /// A set of identifiers for the [crate::vessel::Vessel] whose program should be run.
        spell: VesselRequest,
        /// The preposition separating the two [crate::vessel::Vessel]s.
        preposition: OnPrep,
        /// A set of identifiers for the [crate::vessel::Vessel] for the program to run as.
        target: VesselRequest,
    },
    /// Saves the state of all vessels to a file.
    Save {
        /// The name of the file to be saved to.
        filename: Option<String>,
    },
    /// Loads the state of all vessels from a file.
    Load {
        /// The name of the file to be loaded from.
        filename: Option<String>,
    },
    /// Displays the state of all vessels as raw json data.
    Debug,
    /// Exits parade, or stops programming a vessel if the user is doing so.
    Exit,
    /// Displays general information on Parade, or information on a specific action.
    Learn {
        /// An optional preposition before the help topic being specified.
        preposition: Option<AboutPrep>,
        /// The optional help topic, to have its [Verb::help_text] displayed.
        topic: Option<Verb>,
    },
}
impl Action {
    /// Wraps a [VesselRequest] in an [Action], if the [Verb] refers to an [Action] that wraps a [VesselRequest].
    ///
    /// # Errors
    ///
    /// This method fails if the [Verb] refers to an [Action] which does not exclusively wrap a [VesselRequest].
    /// The viable [Verb]s are:
    /// - [Verb::Create]
    /// - [Verb::Become]
    /// - [Verb::Enter]
    /// - [Verb::Take]
    /// - [Verb::Drop]
    /// - [Verb::Program]
    /// - [Verb::Use]
    pub fn from_vessel_request(verb: Verb, basic: VesselRequest) -> Result<Self, Error> {
        match verb {
            Verb::Create => Ok(Action::Create(basic)),
            Verb::Become => Ok(Action::Become(basic)),
            Verb::Enter => Ok(Action::Enter(basic)),
            Verb::Take => Ok(Action::Take(basic)),
            Verb::Drop => Ok(Action::Drop(basic)),
            Verb::Program => Ok(Action::Program(Some(basic))),
            Verb::Use => Ok(Action::Use(basic)),
            Verb::Learn
            | Verb::Leave
            | Verb::Move
            | Verb::Look
            | Verb::Transform
            | Verb::Note
            | Verb::Warp
            | Verb::Cast
            | Verb::Exit
            | Verb::Save
            | Verb::Load
            | Verb::Debug => Err(Error::NonBasicAction(verb)),
        }
    }

    /// Parse a user-provided action string into an [Action] (or none, if the user entered nothing).
    ///
    /// # Errors
    ///
    /// This method fails if the string does not match the requirements of the given [Action], or specifies a [Verb] which does not exist.
    pub fn parse(action_string: &str) -> Result<Option<Action>, Error> {
        let mut words = action_string.split_whitespace();
        match words.next() {
            Some(first_word) => match Verb::from_str(first_word) {
                Ok(verb) => match verb {
                    Verb::Learn => match words.next() {
                        Some(second_word) => {
                            let preposition = AboutPrep::from_str(second_word);
                            let topic = if preposition.is_ok() {
                                match words.next() {
                                    Some(word) => Verb::from_str(word),
                                    None => {
                                        return Ok(Some(Action::Learn {
                                            preposition: preposition.ok(),
                                            topic: None,
                                        }));
                                    }
                                }
                            } else {
                                Verb::from_str(second_word)
                            };
                            match topic {
                                Ok(help_topic) => Ok(Some(Action::Learn {
                                    preposition: preposition.ok(),
                                    topic: Some(help_topic),
                                })),
                                Err(_) => Err(Error::UnknownVerb(second_word.to_string())),
                            }
                        }
                        None => Ok(Some(Action::Learn {
                            preposition: None,
                            topic: None,
                        })),
                    },
                    Verb::Create
                    | Verb::Become
                    | Verb::Enter
                    | Verb::Take
                    | Verb::Drop
                    | Verb::Use => Ok(Some(Action::from_vessel_request(
                        verb,
                        VesselRequest::parse(words.next().ok_or(Error::MissingNoun)?, &mut words)?,
                    )?)),
                    Verb::Leave => Ok(Some(Action::Leave)),
                    Verb::Move => match words.next() {
                        Some(second_word) => {
                            let VesselRequest {
                                article: object_article,
                                name: object_name,
                            } = VesselRequest::parse(
                                second_word,
                                &mut words.take_while_ref(|word| IntoPrep::from_str(word).is_err()),
                            )?;

                            let preposition = words
                                .next()
                                .and_then(|prep_word| IntoPrep::from_str(prep_word).ok())
                                .ok_or(Error::MissingSecondNoun)?;

                            let VesselRequest {
                                article: target_article,
                                name: target_name,
                            } = VesselRequest::parse(
                                words.next().ok_or(Error::MissingSecondNoun)?,
                                &mut words,
                            )?;

                            Ok(Some(Action::Move {
                                object_article,
                                object_name,
                                preposition,
                                target_article,
                                target_name,
                            }))
                        }
                        None => Err(Error::MissingNoun),
                    },
                    Verb::Look => match words.next() {
                        Some(second_word) => {
                            let (preposition, basic) = VesselRequest::parse_with_prep::<AtPrep, _>(
                                second_word,
                                &mut words,
                            )?;
                            Ok(Some(Action::Look {
                                preposition,
                                basic: Some(basic),
                            }))
                        }
                        None => Ok(Some(Action::Look {
                            preposition: None,
                            basic: None,
                        })),
                    },
                    Verb::Transform => {
                        let (preposition, basic) = VesselRequest::parse_with_prep::<IntoPrep, _>(
                            words.next().ok_or(Error::MissingNoun)?,
                            &mut words,
                        )?;
                        Ok(Some(Action::Transform { preposition, basic }))
                    }
                    Verb::Note => Ok(Some(Action::Note {
                        text: join_or_none(&mut words, " "),
                    })),
                    Verb::Warp => {
                        let (preposition, basic) = VesselRequest::parse_with_prep::<IntoPrep, _>(
                            words.next().ok_or(Error::MissingNoun)?,
                            &mut words,
                        )?;
                        Ok(Some(Action::Warp { preposition, basic }))
                    }
                    Verb::Program => {
                        match words
                            .next()
                            .ok_or(Error::MissingNoun)
                            .and_then(|second_word| VesselRequest::parse(second_word, &mut words))
                        {
                            Ok(vessel_request) => {
                                Ok(Some(Action::from_vessel_request(verb, vessel_request)?))
                            }
                            Err(Error::MissingNoun) => Ok(Some(Action::Program(None))),
                            Err(e) => Err(e),
                        }
                    }
                    Verb::Cast => match words.next() {
                        Some(second_word) => {
                            let spell = VesselRequest::parse(
                                second_word,
                                &mut words.take_while_ref(|word| OnPrep::from_str(word).is_err()),
                            )?;

                            let preposition = words
                                .next()
                                .and_then(|prep_word| OnPrep::from_str(prep_word).ok())
                                .ok_or(Error::MissingSecondNoun)?;

                            let target = VesselRequest::parse(
                                words.next().ok_or(Error::MissingSecondNoun)?,
                                &mut words,
                            )?;

                            Ok(Some(Action::Cast {
                                spell,
                                preposition,
                                target,
                            }))
                        }
                        None => Err(Error::MissingNoun),
                    },
                    Verb::Exit => Ok(Some(Action::Exit)),
                    Verb::Save => Ok(Some(Action::Save {
                        filename: join_or_none(&mut words, " "),
                    })),
                    Verb::Load => Ok(Some(Action::Load {
                        filename: join_or_none(&mut words, " "),
                    })),
                    Verb::Debug => Ok(Some(Action::Debug)),
                },
                Err(_) => Err(Error::UnknownVerb(first_word.to_string())),
            },
            None => Ok(None),
        }
    }
}

/// The errors that can be encountered when parsing an [Action].
#[derive(Debug)]
pub enum Error {
    /// Occurs when the [Action] requires a noun, but none is provided.
    MissingNoun,
    /// Occurs when the [Action] requires two nouns, but only one is provided.
    MissingSecondNoun,
    /// Occurs when the input does not match any [Verb].
    UnknownVerb(String),
    /// Occurs when the system attempts to wrap a [VesselRequest] with an [Action] variant that does not support it.
    NonBasicAction(Verb),
}
impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::MissingNoun => write!(f, "This action requires a noun."),
            Self::MissingSecondNoun => write!(
                f,
                "This action requires two nouns. Did you remember to delimit them?"
            ),
            Self::UnknownVerb(verb_string) => {
                write!(f, "Unrecognized action: \"{verb_string}\".")
            }
            Self::NonBasicAction(verb) => write!(
                f,
                "Program tried to parse non-basic action \"{verb}\" as basic."
            ),
        }
    }
}
impl std::error::Error for Error {}

/// Signifies that the given enum is a grammatical preposition.
pub trait Preposition: FromStr {}

/// The prepositions for moving, transforming, or warping to/into something.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, EnumString)]
#[strum(ascii_case_insensitive)]
pub enum IntoPrep {
    /// The English word "Into".
    Into,
    /// The English word "To".
    To,
}
impl Preposition for IntoPrep {}

/// The prepositions for looking at/in something.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, EnumString)]
#[strum(ascii_case_insensitive)]
pub enum AtPrep {
    /// The English word "At".
    At,
    /// The English word "In".
    In,
}
impl Preposition for AtPrep {}

/// The prepositions for casting something at/on something.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, EnumString)]
#[strum(ascii_case_insensitive)]
pub enum OnPrep {
    /// The English word "At".
    At,
    /// The English word "On".
    On,
}
impl Preposition for OnPrep {}

/// The preposition "About".
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, EnumString)]
#[strum(ascii_case_insensitive)]
pub enum AboutPrep {
    /// The English word "About".
    About,
}
impl Preposition for AboutPrep {}

/// A basic set of [Action] parameters, indicating a [crate::vessel::Vessel] by name and (optionally) [Article].
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct VesselRequest {
    /// The [Article] with which to refer to the [crate::vessel::Vessel].
    pub article: Option<Article>,
    /// The name/key of the [crate::vessel::Vessel] being indicated.
    pub name: String,
}
impl VesselRequest {
    /// Parses a [VesselRequest] from the given second and following words of an action string.
    /// (The first word is always the [Verb].)
    pub fn parse<'a, T: Iterator<Item = &'a str>>(
        second_word: &'a str,
        words: &mut T,
    ) -> Result<Self, Error> {
        let article = Article::from_str(second_word).ok();
        let name = join_or_none(
            &mut if article.is_some() {
                None
            } else {
                Some(second_word)
            }
            .into_iter()
            .chain(words),
            " ",
        )
        .ok_or(Error::MissingNoun)?;
        Ok(Self { article, name })
    }
    /// Parses a [VesselRequest] and optional [Preposition] from the given second and following words of an action string.
    /// (The first word is always the [Verb].)
    pub fn parse_with_prep<'a, P: Preposition, T: Iterator<Item = &'a str>>(
        second_word: &'a str,
        words: &mut T,
    ) -> Result<(Option<P>, Self), Error> {
        let preposition = P::from_str(second_word).ok();
        let third_word = if preposition.is_none() {
            second_word
        } else {
            words.next().ok_or(Error::MissingNoun)?
        };
        Ok((preposition, Self::parse(third_word, words)?))
    }
}

fn join_or_none<'a, T: Iterator<Item = &'a str>>(words: &mut T, sep: &str) -> Option<String> {
    let string = words.join(sep);
    if string.is_empty() {
        None
    } else {
        Some(string)
    }
}