use alloc::{
format,
string::String,
vec::Vec,
};
use crate::{
data::{
builds::{
BuildProvider,
CATALOG,
},
catalog::{
BuiltinCatalog,
BunsenCatalogStack,
CatalogName,
CatalogRef,
CatalogRule,
ResolvedBuild,
builtin::find_spelling,
catalog_error,
},
},
errors::BunsenResult,
};
pub const GEOMETRY: &str = "geometry";
#[derive(Clone, Debug)]
pub struct CatalogBuildProvider {
name: String,
description: String,
builds: Vec<ResolvedBuild>,
names: Vec<(CatalogName, usize)>,
}
impl CatalogBuildProvider {
pub fn new(
name: impl Into<String>,
description: impl Into<String>,
stack: &BunsenCatalogStack,
contract: &str,
) -> BunsenResult<Self> {
let contract_name = stack
.expect_contract(&CatalogRef::parse(contract)?)?
.name()
.clone();
let mut builds = Vec::new();
for build in stack.builds_of_contract(&contract_name) {
builds.push(stack.resolve_build(&build)?);
}
Ok(Self::of_builds(name, description, builds))
}
fn of_builds(
name: impl Into<String>,
description: impl Into<String>,
builds: Vec<ResolvedBuild>,
) -> Self {
let mut names = Vec::new();
for (index, build) in builds.iter().enumerate() {
names.push((build.name.clone(), index));
names.extend(build.aliases.iter().map(|a| (a.clone(), index)));
}
Self {
name: name.into(),
description: description.into(),
builds,
names,
}
}
pub fn builds(&self) -> &[ResolvedBuild] {
&self.builds
}
pub fn find(
&self,
name: &str,
) -> BunsenResult<Option<&ResolvedBuild>> {
let found = find_spelling(&self.names, name, |i| self.builds[i].name.to_string())?;
Ok(found.map(|i| &self.builds[i]))
}
}
impl BuildProvider for CatalogBuildProvider {
fn name(&self) -> &str {
&self.name
}
fn description(&self) -> &str {
&self.description
}
fn list(&self) -> Vec<ResolvedBuild> {
self.builds.clone()
}
fn lookup(
&self,
name: &str,
) -> BunsenResult<Option<ResolvedBuild>> {
Ok(self.find(name)?.cloned())
}
}
impl BuiltinCatalog {
pub fn provider(
&self,
contract: &str,
) -> BunsenResult<CatalogBuildProvider> {
let contract_name = CatalogName::contracts_root().join(CatalogRef::parse(contract)?.path());
let description = self.contract_description(&contract_name).ok_or_else(|| {
catalog_error(
CatalogRule::Lookup,
format!("the built-in catalog has no contract `{contract}`"),
)
})?;
let builds = self
.builds()
.iter()
.filter(|build| {
build
.contract
.as_ref()
.is_some_and(|c| c == &contract_name || c.is_within(&contract_name))
})
.cloned()
.collect();
Ok(CatalogBuildProvider::of_builds(
CATALOG,
description,
builds,
))
}
pub fn expect_provider(contract: &str) -> CatalogBuildProvider {
Self::get()
.and_then(|catalog| catalog.provider(contract))
.unwrap_or_else(|e| panic!("the built-in catalog's `{contract}` builds: {e}"))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::data::{
catalog::testing::fixtures,
resources::Source,
};
fn provider(contract: &str) -> CatalogBuildProvider {
CatalogBuildProvider::new(
"examples",
"the example catalogs",
&fixtures::example_stack().unwrap(),
contract,
)
.unwrap()
}
#[test]
fn test_the_rows_are_the_contracts_builds() {
let whisper = provider("whisper-asr");
let names: Vec<String> = whisper
.builds()
.iter()
.map(|b| b.name.to_string())
.collect();
assert!(
names.iter().any(|n| n == "openai/whisper/large/large-v3"),
"{names:?}"
);
assert!(names.iter().all(|n| n.starts_with("openai/whisper/")));
let v3 = whisper.find("large-v3").unwrap().unwrap();
assert_eq!(v3.tag("contract"), Some(&serde_json::json!("whisper-asr")));
assert!(v3.presets.axis_of("translate").is_some());
}
#[test]
fn test_sources() {
let whisper = provider("whisper-asr");
let v1 = whisper.find("large-v1").unwrap().unwrap();
let set = v1.resource_set();
let checkpoint = set.get("checkpoint").unwrap();
assert_eq!(checkpoint.name, "openai/whisper/large/large-v1/checkpoint");
assert_eq!(checkpoint.dtype.as_deref(), Some("float16"));
assert!(
matches!(&checkpoint.sources[0], Source::LocalDir { name, .. } if name == "openai-whisper")
);
let Source::Url(url) = &checkpoint.sources[1] else {
panic!("{:?}", checkpoint.sources)
};
assert!(url.ends_with("/large-v1.pt"), "{url}");
assert!(set.get("vocabulary").is_some());
}
#[test]
fn test_lookups() {
let whisper = provider("whisper-asr");
for name in [
"openai/whisper/large/large-v3",
"openai/large-v3",
"large-v3",
"LARGE-V3",
] {
assert_eq!(
whisper.lookup(name).unwrap().unwrap().name.to_string(),
"openai/whisper/large/large-v3",
"{name}"
);
}
assert!(whisper.lookup("qwen3-0.6b").unwrap().is_none());
assert!(whisper.lookup("large").unwrap().is_none(), "a namespace");
if !cfg!(feature = "kits") {
return;
}
let resnet = BuiltinCatalog::get()
.unwrap()
.provider("resnet-classifier")
.unwrap();
crate::errors::testing::ErrorMatcher::kind(crate::errors::BunsenErrorKind::Lookup)
.message_contains("`resnet34` is ambiguous: torchvision/resnet34, timm/resnet34")
.assert_err(&resnet.lookup("resnet34"));
assert_eq!(
resnet
.lookup("timm/resnet34")
.unwrap()
.unwrap()
.name
.to_string(),
"timm/resnet34"
);
}
}