pub(crate) enum Resolved<'a> {
One(&'a str),
Ambiguous(Vec<&'a str>),
Missing,
}
pub(crate) fn resolve<'a>(names: &[&'a str], want: &str) -> Resolved<'a> {
if let Some(n) = named(names, want) {
return Resolved::One(n);
}
let hits: Vec<&'a str> = names
.iter()
.copied()
.filter(|n| n.starts_with(want))
.collect();
match hits.as_slice() {
[one] => Resolved::One(one),
[] => Resolved::Missing,
_ => Resolved::Ambiguous(hits),
}
}
fn named<'a>(names: &[&'a str], want: &str) -> Option<&'a str> {
let latest = format!("{want}:latest");
names
.iter()
.copied()
.find(|n| *n == want)
.or_else(|| names.iter().copied().find(|n| *n == latest))
}
pub(crate) fn ambiguous_msg(want: &str, candidates: &[&str]) -> String {
format!("'{want}' is ambiguous: {}", candidates.join(", "))
}
pub(crate) fn missing_msg(
want: &str,
served: &[&str],
answered: usize,
named_hosts: usize,
) -> String {
format!(
"no fleet host serves '{want}' -- served by {answered}/{named_hosts} \
host(s): {}",
served.join(", ")
)
}
#[cfg(test)]
mod tests {
use super::*;
const FLEET: [&str; 4] = [
"codestral:latest",
"gpt-oss:20b",
"qwen3-coder:30b",
"qwen3-coder:7b",
];
fn one(want: &str) -> Option<&'static str> {
match resolve(&FLEET, want) {
Resolved::One(m) => Some(m),
_ => None,
}
}
#[test]
fn a_unique_prefix_resolves() {
assert_eq!(one("gpt-oss"), Some("gpt-oss:20b"));
assert_eq!(one("gpt"), Some("gpt-oss:20b"));
}
#[test]
fn an_exact_name_wins() {
assert_eq!(one("gpt-oss:20b"), Some("gpt-oss:20b"));
assert_eq!(one("qwen3-coder:7b"), Some("qwen3-coder:7b"));
}
#[test]
fn a_bare_name_means_latest_as_ollama_means_it() {
assert_eq!(one("codestral"), Some("codestral:latest"));
}
#[test]
fn latest_beats_an_otherwise_ambiguous_prefix() {
let fleet = ["qwen3-coder:latest", "qwen3-coder:30b", "qwen3-coder:7b"];
let got = match resolve(&fleet, "qwen3-coder") {
Resolved::One(m) => Some(m),
_ => None,
};
assert_eq!(got, Some("qwen3-coder:latest"));
}
#[test]
fn an_ambiguous_prefix_names_its_candidates() {
let got = match resolve(&FLEET, "qwen") {
Resolved::Ambiguous(c) => c,
_ => Vec::new(),
};
assert_eq!(got, vec!["qwen3-coder:30b", "qwen3-coder:7b"]);
}
#[test]
fn an_unserved_name_is_missing() {
assert!(matches!(resolve(&FLEET, "llama"), Resolved::Missing));
assert!(matches!(resolve(&[], "gpt-oss"), Resolved::Missing));
}
#[test]
fn the_missing_message_reports_host_coverage() {
let m = missing_msg("llama", &["a", "b"], 2, 3);
assert!(m.contains("2/3 host(s)"), "coverage stated: {m}");
assert!(m.contains("a, b"));
}
#[test]
fn the_ambiguous_message_lists_candidates() {
assert!(ambiguous_msg("q", &["q:1", "q:2"]).contains("q:1, q:2"));
}
}