Struct initiative_core::app::CommandMatches
source · pub struct CommandMatches<T> {
pub canonical_match: Option<T>,
pub fuzzy_matches: Vec<T>,
}
Expand description
Represents all possible parse results for a given input.
One of the key usability features (and major headaches) of initiative.sh is its use of fuzzy command matching. Most parsers assume only one possible valid interpretation, but initiative.sh has the concept of canonical and fuzzy matches.
Canonical matches are inputs that cannot possibly conflict with one another. This is achieved using differing prefixes, eg. all SRD reference lookups are prefixed as “srd item shield” (or “srd spell shield”).
Fuzzy matches are commands that the user could possibly have meant with a given input. The reason we need to be particularly careful with fuzzy matches is because the user can add arbitrary names to their journal, and typing that arbitrary name is usually enough to pull it up again. However, that arbitrary name could easily be another command, which the user may not want to overwrite. (The notable built-in conflict is the example above, where “shield” is both an item and a spell.)
|| Canonical matches || Fuzzy matches || Result || | 0 | 0 | Error: “Unknown command: ‘…’” | | 0 | 1 | The fuzzy match is run. | | 0 | 2+ | Error: “There are several possible interpretations of this command. Did you mean:” | | 1 | 0 | The canonical match is run. | | 1 | 1+ | The canonical match is run, suffixed with the error: “There are other possible interpretations of this command. Did you mean:” |
Since the parsing logic in the code base runs several layers deep across a number of different structs, this struct provides utilities for combining multiple CommandMatches instances of differing types, and for transforming the inner type as needed.
Fields§
§canonical_match: Option<T>
§fuzzy_matches: Vec<T>
Implementations§
source§impl<T: Debug> CommandMatches<T>
impl<T: Debug> CommandMatches<T>
sourcepub fn new_canonical(canonical_match: T) -> Self
pub fn new_canonical(canonical_match: T) -> Self
Create a new instance of CommandMatches with a canonical match.
sourcepub fn new_fuzzy(fuzzy_match: T) -> Self
pub fn new_fuzzy(fuzzy_match: T) -> Self
Create a new instance of CommandMatches
with a single fuzzy match.
sourcepub fn push_canonical(&mut self, canonical_match: T)
pub fn push_canonical(&mut self, canonical_match: T)
Push a new canonical match. Panics if a canonical match is already present.
sourcepub fn push_fuzzy(&mut self, fuzzy_match: T)
pub fn push_fuzzy(&mut self, fuzzy_match: T)
Add a new fuzzy match to the list of possibilities.
sourcepub fn union<O>(self, other: CommandMatches<O>) -> Self
pub fn union<O>(self, other: CommandMatches<O>) -> Self
Combine the current CommandMatches with another object that can be massaged into the same type. The Vecs of fuzzy matches are combined. Panics if both objects lay claim to a canonical match.
sourcepub fn union_with_overwrite<O>(self, other: CommandMatches<O>) -> Self
pub fn union_with_overwrite<O>(self, other: CommandMatches<O>) -> Self
Variant of union() that resolves canonical conflicts by overwriting self.canonical_match with other.canonical_match instead of panicking.
sourcepub fn into_subtype<O>(self) -> CommandMatches<O>
pub fn into_subtype<O>(self) -> CommandMatches<O>
Convert a CommandMatches<T>' into a
CommandMatches, massaging the inner type using its
From
This could be an impl of From<CommandMatches<T>> for CommandMatches<O>
, but that produces
a trait conflict due to limitations of Rust’s type system.
sourcepub fn take_best_match(self) -> Option<T>
pub fn take_best_match(self) -> Option<T>
Consumes the struct, returning the following in priority order:
- The canonical match, if present.
- The first fuzzy match, if any are present.
None
Trait Implementations§
source§impl<T: Clone> Clone for CommandMatches<T>
impl<T: Clone> Clone for CommandMatches<T>
source§fn clone(&self) -> CommandMatches<T>
fn clone(&self) -> CommandMatches<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<T: Debug> Debug for CommandMatches<T>
impl<T: Debug> Debug for CommandMatches<T>
source§impl<T> Default for CommandMatches<T>
impl<T> Default for CommandMatches<T>
source§impl From<CommandMatches<CommandType>> for Command
impl From<CommandMatches<CommandType>> for Command
source§fn from(input: CommandMatches<CommandType>) -> Self
fn from(input: CommandMatches<CommandType>) -> Self
source§impl<T> From<T> for CommandMatches<T>
impl<T> From<T> for CommandMatches<T>
source§impl<T: PartialEq> PartialEq for CommandMatches<T>
impl<T: PartialEq> PartialEq for CommandMatches<T>
source§fn eq(&self, other: &CommandMatches<T>) -> bool
fn eq(&self, other: &CommandMatches<T>) -> bool
self
and other
values to be equal, and is used
by ==
.impl<T: Eq> Eq for CommandMatches<T>
impl<T> StructuralPartialEq for CommandMatches<T>
Auto Trait Implementations§
impl<T> Freeze for CommandMatches<T>where
T: Freeze,
impl<T> RefUnwindSafe for CommandMatches<T>where
T: RefUnwindSafe,
impl<T> Send for CommandMatches<T>where
T: Send,
impl<T> Sync for CommandMatches<T>where
T: Sync,
impl<T> Unpin for CommandMatches<T>where
T: Unpin,
impl<T> UnwindSafe for CommandMatches<T>where
T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)