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>

source

pub fn new_canonical(canonical_match: T) -> Self

Create a new instance of CommandMatches with a canonical match.

source

pub fn new_fuzzy(fuzzy_match: T) -> Self

Create a new instance of CommandMatches with a single fuzzy match.

source

pub fn push_canonical(&mut self, canonical_match: T)

Push a new canonical match. Panics if a canonical match is already present.

source

pub fn push_fuzzy(&mut self, fuzzy_match: T)

Add a new fuzzy match to the list of possibilities.

source

pub fn union<O>(self, other: CommandMatches<O>) -> Self
where O: Debug, T: From<O>,

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.

source

pub fn union_with_overwrite<O>(self, other: CommandMatches<O>) -> Self
where O: Debug, T: From<O>,

Variant of union() that resolves canonical conflicts by overwriting self.canonical_match with other.canonical_match instead of panicking.

source

pub fn into_subtype<O>(self) -> CommandMatches<O>
where O: From<T> + Debug,

Convert a CommandMatches<T>' into a CommandMatches, massaging the inner type using its From` trait.

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.

source

pub fn take_best_match(self) -> Option<T>

Consumes the struct, returning the following in priority order:

  1. The canonical match, if present.
  2. The first fuzzy match, if any are present.
  3. None

Trait Implementations§

source§

impl<T: Clone> Clone for CommandMatches<T>

source§

fn clone(&self) -> CommandMatches<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for CommandMatches<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T> Default for CommandMatches<T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl From<CommandMatches<CommandType>> for Command

source§

fn from(input: CommandMatches<CommandType>) -> Self

Converts to this type from the input type.
source§

impl<T> From<T> for CommandMatches<T>

source§

fn from(input: T) -> Self

Converts to this type from the input type.
source§

impl<T: PartialEq> PartialEq for CommandMatches<T>

source§

fn eq(&self, other: &CommandMatches<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: Eq> Eq for CommandMatches<T>

source§

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V