Skip to main content

Resolver

Struct Resolver 

Source
pub struct Resolver<'a> { /* private fields */ }
Expand description

Resolves a string token to a Command in a slice, supporting aliases, spellings, and unambiguous prefix matching.

Create a resolver by passing a slice of commands to Resolver::new, then call Resolver::resolve with a raw string token. The returned reference borrows from the original command slice (lifetime 'a).

§Examples

let cmds = vec![
    Command::builder("deploy").alias("d").build().unwrap(),
    Command::builder("delete").build().unwrap(),
];
let resolver = Resolver::new(&cmds);

// Exact match via alias
assert_eq!(resolver.resolve("d").unwrap().canonical, "deploy");
// Prefix "del" is unambiguous
assert_eq!(resolver.resolve("del").unwrap().canonical, "delete");

Implementations§

Source§

impl<'a> Resolver<'a>

Source

pub fn new(commands: &'a [Command]) -> Self

Create a new Resolver over the given command slice.

§Arguments
  • commands — The slice of commands to resolve against. The lifetime 'a is propagated to the references returned by Resolver::resolve.
Source

pub fn resolve(&self, input: &str) -> Result<&'a Command, ResolveError>

Resolve input against the registered commands.

Resolution order:

  1. Normalize: trim + lowercase.
  2. Exact match across canonical/aliases/spellings → return immediately.
  3. Prefix match → return if exactly one command matches; else Ambiguous.
  4. No match → Unknown.
§Arguments
  • input — The raw string to resolve (trimming and lowercasing are applied internally).
§Errors
  • ResolveError::Unknown — no command matched input exactly or as a prefix. The suggestions field contains up to three canonical names whose edit distance from input is ≤ 2, or which contain input as a substring. May be empty if no close matches exist.
  • ResolveError::Ambiguousinput is a prefix of more than one command; the candidates field lists their canonical names.
§Examples
let cmds = vec![Command::builder("get").build().unwrap()];
let resolver = Resolver::new(&cmds);

assert_eq!(resolver.resolve("get").unwrap().canonical, "get");
assert_eq!(resolver.resolve("GET").unwrap().canonical, "get"); // case-insensitive
assert!(matches!(resolver.resolve("xyz"), Err(ResolveError::Unknown { .. })));

Auto Trait Implementations§

§

impl<'a> Freeze for Resolver<'a>

§

impl<'a> !RefUnwindSafe for Resolver<'a>

§

impl<'a> Send for Resolver<'a>

§

impl<'a> Sync for Resolver<'a>

§

impl<'a> Unpin for Resolver<'a>

§

impl<'a> UnsafeUnpin for Resolver<'a>

§

impl<'a> !UnwindSafe for Resolver<'a>

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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.