use crate::source::{Source, Stop};
use std::path::PathBuf;
use tessera_frontend::tasks::Error;
use tessera_frontend::tasks::task::{Run, Shown, Sink, Task};
#[derive(Task)]
#[task(example = "download the {kind} model")]
pub struct Fetch {}
impl Run<Source> for Fetch {
fn run(self, source: Source, sink: &mut dyn Sink) -> Result<(), Error> {
let mut paths: Vec<(String, PathBuf)> = Vec::new();
if let Some(recipe) = &source.recipe {
paths.push(("recipe".into(), recipe.dir.clone()));
let (cache, token) = (source.cache.as_deref(), source.token.as_deref());
let progress = &source.progress;
let weights = match source.override_weights() {
Some(dir) => dir,
None => recipe.weights(cache, token, progress)?,
};
paths.push(("weights".into(), weights));
for (flag, dir) in recipe.extras(cache, token, progress)? {
paths.push((flag.trim_start_matches('-').into(), dir));
}
} else if let Some(dir) = &source.fetched {
paths.push(("weights".into(), dir.clone()));
} else {
return Err(Stop::Usage.into());
}
if !sink.show(Shown::Paths(&paths)) {
for (key, dir) in &paths {
sink.text(&format!("{key} {}\n", dir.display()));
}
}
Ok(())
}
}
crate::task::register!(Fetch, crate::task::OnSource, "", false);