pub fn resolve_unique<T, I, F>(
input: &str,
candidates: I,
key_of: F,
) -> Result<Option<T>, Ambiguity<T>>Expand description
Resolve input to the unique candidate whose key equals it.
key_of projects each candidate to the string compared against input.
Returns Ok(Some(candidate)) when exactly one candidate’s key equals input,
Ok(None) when none do (the caller decides whether an empty match is an error and how to list the alternatives),
and an Ambiguity error listing the matches when two or more share the key. Comparison is exact
and case-sensitive; callers wanting fuzzy resolution use nearest instead.
§Examples
use rskit_util::strings::{resolve_unique, Ambiguity};
fn tail<'a>(candidate: &'a &str) -> &'a str {
candidate.rsplit('/').next().unwrap_or(candidate)
}
let candidates = ["service/user", "job/report", "service/report"];
assert_eq!(resolve_unique("user", candidates, tail), Ok(Some("service/user")));
assert_eq!(resolve_unique("missing", candidates, tail), Ok(None));
assert!(matches!(resolve_unique("report", candidates, tail), Err(Ambiguity { .. })));§Errors
Returns Ambiguity when input matches more than one candidate.