use super::{BzOutcome, BzRunner, ProviderRow, provider_rows};
use crate::bz_host::{self, Tty};
use crate::xdg::Env;
use std::path::Path;
#[derive(Debug, Clone)]
pub struct RealBzRunner {
env: Env,
}
impl RealBzRunner {
#[cfg(test)]
pub(crate) fn new(env: Env) -> Self {
Self { env }
}
pub fn resolve(world: &Env) -> Self {
Self { env: world.clone() }
}
fn capture(&self, args: &[&str]) -> BzOutcome {
let argv = args.iter().map(|a| (*a).to_string()).collect();
let (mut out, mut err) = (Vec::new(), Vec::new());
let exit = bz_host::run(
argv,
&self.env,
Tty::PIPED,
&mut std::io::empty(),
&mut out,
&mut err,
);
BzOutcome {
success: exit == 0,
stdout: String::from_utf8_lossy(&out).into_owned(),
stderr: String::from_utf8_lossy(&err).into_owned(),
}
}
}
impl BzRunner for RealBzRunner {
fn dump_config_at(&self, config: &Path) -> BzOutcome {
self.capture(&["--config", &config.display().to_string(), "--dump-config"])
}
fn dump_config_effective(&self) -> BzOutcome {
self.capture(&["--dump-config"])
}
fn providers(&self) -> Vec<ProviderRow> {
provider_rows(&self.capture(&["--list-providers", "--json"]).stdout)
}
fn list_models(&self, provider: &str) -> BzOutcome {
self.capture(&["--list-models", "--provider", provider, "--json"])
}
}
#[cfg(test)]
mod tests;