pub(crate) fn run_model_fetch() -> Result<(), String> {
let spec = ktstr::test_support::DEFAULT_MODEL;
match ktstr::test_support::ensure(&spec) {
Ok(path) => {
println!(
"ktstr: model '{}' ready at {}",
spec.file_name,
path.display()
);
Ok(())
}
Err(e) => Err(format!("fetch model '{}': {e:#}", spec.file_name)),
}
}
pub(crate) fn run_model_status() -> Result<(), String> {
let spec = ktstr::test_support::DEFAULT_MODEL;
let status = ktstr::test_support::status(&spec).map_err(|e| format!("{e:#}"))?;
println!("model: {}", status.spec.file_name);
println!("path: {}", status.path.display());
println!("cached: {}", status.sha_verdict.is_cached());
println!("checked: {}", status.sha_verdict.is_match());
const RE_FETCH_TAIL: &str = "re-fetch to replace it";
match &status.sha_verdict {
ktstr::test_support::ShaVerdict::NotCached => println!(
"(no cached copy — run `cargo ktstr model fetch` to download {} MiB)",
status.spec.size_bytes / 1024 / 1024,
),
ktstr::test_support::ShaVerdict::CheckFailed(err) => {
let single_line = err.replace('\n', "; ");
println!(
"(cached file could not be checked: {single_line}; \
inspect the cache entry or {RE_FETCH_TAIL})",
);
}
ktstr::test_support::ShaVerdict::Mismatches => {
println!("(cached file failed SHA-256 check; {RE_FETCH_TAIL})",);
}
ktstr::test_support::ShaVerdict::Matches => {}
}
Ok(())
}
pub(crate) fn run_model_clean() -> Result<(), String> {
let spec = ktstr::test_support::DEFAULT_MODEL;
let report =
ktstr::test_support::clean(&spec).map_err(|e| format!("clean model cache: {e:#}"))?;
if report.is_empty() {
println!(
"no cached model found at {}",
report.artifact_path.display()
);
return Ok(());
}
if let Some(bytes) = report.artifact_freed_bytes {
println!(
"removed {} ({})",
report.artifact_path.display(),
indicatif::HumanBytes(bytes),
);
}
if let Some(bytes) = report.sidecar_freed_bytes {
println!(
"removed {} ({})",
report.sidecar_path.display(),
indicatif::HumanBytes(bytes),
);
}
println!(
"freed {} total",
indicatif::HumanBytes(report.total_freed_bytes()),
);
Ok(())
}