use std::path::Path;
use crate::types::manifest::PackVersion;
use crate::types::op::OpError;
use crate::types::report::{Diagnostic, Location};
use crate::types::set::AirslRange;
#[must_use]
pub fn parse_airsl_version(text: &str) -> Option<PackVersion> {
let version = text.trim().strip_prefix("airsl ")?;
PackVersion::new(version).ok()
}
#[must_use]
pub fn airsl_not_found(project_file: &Path, range: &AirslRange) -> Diagnostic {
let packs = range.needs().iter().map(|(pack, _)| pack.clone()).collect();
Diagnostic::new(
Location::set_level(project_file, None),
OpError::AirslNotFound { packs },
)
}
#[must_use]
pub fn airsl_unparseable(project_file: &Path, text: &str) -> Diagnostic {
Diagnostic::new(
Location::set_level(project_file, None),
OpError::AirslVersionText {
text: text.trim().to_owned(),
},
)
}
#[cfg(test)]
mod tests {
use std::path::Path;
use super::*;
use crate::types::manifest::AirslReq;
use crate::types::name::PackName;
type TestResult = Result<(), Box<dyn std::error::Error>>;
const PROJECT: &str = "agentstacks.toml";
fn range(needs: &[(&str, &str)]) -> Result<AirslRange, Box<dyn std::error::Error>> {
let mut listed = Vec::new();
for (pack, req) in needs {
listed.push((PackName::new(pack)?, AirslReq::new(req)?));
}
Ok(AirslRange::new(listed).ok_or("a pack declares a range")?)
}
#[test]
fn airsl_version_parses_the_probed_output() {
for text in ["airsl 0.1.4", "airsl 0.1.4\n", " airsl 0.1.4 \n"] {
let parsed = parse_airsl_version(text).map(|version| version.to_string());
assert_eq!(parsed.as_deref(), Some("0.1.4"), "{text:?}");
}
}
#[test]
fn airsl_version_refuses_other_text() {
for text in [
"airsl-dev",
"0.1.4",
"airsl",
"airsl x.y",
"airsl 0.1.4 extra",
"",
] {
assert_eq!(parse_airsl_version(text), None, "{text:?}");
}
}
#[test]
fn airsl_not_found_names_the_needing_packs() -> TestResult {
let needs = range(&[("sdlc", "<0.3"), ("core", ">=0.1.4")])?;
assert_eq!(
airsl_not_found(Path::new(PROJECT), &needs).to_string(),
"agentstacks.toml: airsl not found on PATH (needed by core, sdlc)"
);
Ok(())
}
#[test]
fn airsl_unparseable_quotes_the_trimmed_text() {
assert_eq!(
airsl_unparseable(Path::new(PROJECT), " airsl-dev\n").to_string(),
"agentstacks.toml: airsl --version printed \"airsl-dev\", expected \"airsl <version>\""
);
}
#[test]
fn airsl_out_of_range_reuses_the_built_refusal() -> TestResult {
let needs = range(&[("core", ">=0.1.4")])?;
let installed = parse_airsl_version("airsl 0.1.3").ok_or("a version")?;
let refusal = needs
.installed_refusal(Path::new(PROJECT), &installed)
.ok_or("0.1.3 is below the range")?;
assert_eq!(
refusal.to_string(),
"agentstacks.toml: airsl 0.1.3 installed, packs need >=0.1.4"
);
Ok(())
}
}