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>
impl<'a> Resolver<'a>
Sourcepub fn new(commands: &'a [Command]) -> Self
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'ais propagated to the references returned byResolver::resolve.
Sourcepub fn resolve(&self, input: &str) -> Result<&'a Command, ResolveError>
pub fn resolve(&self, input: &str) -> Result<&'a Command, ResolveError>
Resolve input against the registered commands.
Resolution order:
- Normalize: trim + lowercase.
- Exact match across canonical/aliases/spellings → return immediately.
- Prefix match → return if exactly one command matches; else
Ambiguous. - No match →
Unknown.
§Arguments
input— The raw string to resolve (trimming and lowercasing are applied internally).
§Errors
ResolveError::Unknown— no command matchedinputexactly or as a prefix. Thesuggestionsfield contains up to three canonical names whose edit distance frominputis ≤ 2, or which containinputas a substring. May be empty if no close matches exist.ResolveError::Ambiguous—inputis a prefix of more than one command; thecandidatesfield 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> 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
Mutably borrows from an owned value. Read more