use std::collections::BTreeMap;
use std::path::Path;
use crate::types::manifest::PackVersion;
use crate::types::manifest::VersionRange;
use crate::types::name::PackName;
use crate::types::pack::Pack;
use crate::types::report::Diagnostic;
use crate::types::report::Location;
use crate::types::set::error::SetError;
use crate::types::set::interval::VersionInterval;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AirslRange {
needs: Vec<(PackName, VersionRange)>,
together: VersionInterval,
}
impl AirslRange {
#[must_use]
pub fn new(mut needs: Vec<(PackName, VersionRange)>) -> Option<Self> {
if needs.is_empty() {
return None;
}
needs.sort_by(|(left, _), (right, _)| left.cmp(right));
let together = needs
.iter()
.map(|(_, req)| VersionInterval::of_req(req.as_version_req()))
.fold(VersionInterval::UNBOUNDED, |both, next| {
both.intersect(&next)
});
Some(Self { needs, together })
}
pub(crate) fn of_packs(packs: &BTreeMap<PackName, Pack>) -> Option<Self> {
let needs = packs
.iter()
.filter_map(|(key, pack)| {
let req = pack.manifest().requires().airsl()?;
Some((key.clone(), req.clone()))
})
.collect();
Self::new(needs)
}
#[must_use]
pub fn needs(&self) -> &[(PackName, VersionRange)] {
&self.needs
}
#[must_use]
pub fn is_satisfiable(&self) -> bool {
!self.together.is_empty()
}
#[must_use]
pub fn admits(&self, installed: &PackVersion) -> bool {
self.needs
.iter()
.all(|(_, req)| req.as_version_req().matches(installed.as_version()))
}
#[must_use]
pub fn installed_refusal(
&self,
project_file: &Path,
installed: &PackVersion,
) -> Option<Diagnostic> {
if self.admits(installed) {
return None;
}
Some(Diagnostic::new(
Location::set_level(project_file, None),
SetError::AirslInstalled {
installed: installed.clone(),
ranges: self.needs.iter().map(|(_, req)| req.clone()).collect(),
},
))
}
pub(crate) fn disjoint_refusal(&self, project_file: &Path) -> Option<Diagnostic> {
if self.is_satisfiable() {
return None;
}
Some(Diagnostic::new(
Location::set_level(project_file, None),
SetError::AirslDisjoint {
needs: self.needs.clone(),
},
))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::set::testing::{PROJECT_FILE, name};
use std::error::Error;
type TestResult = Result<(), Box<dyn Error>>;
fn range(needs: &[(&str, &str)]) -> Result<AirslRange, Box<dyn Error>> {
let mut parsed = Vec::new();
for (pack, written) in needs {
parsed.push((name(pack)?, VersionRange::new(written)?));
}
Ok(AirslRange::new(parsed).ok_or("no pack declares a range")?)
}
fn version(text: &str) -> Result<PackVersion, Box<dyn Error>> {
Ok(PackVersion::new(text)?)
}
#[test]
fn ranges_intersect_per_cargo_semantics() -> TestResult {
assert!(range(&[("a", ">=0.1.4"), ("b", "^0.1")])?.is_satisfiable());
assert!(!range(&[("a", "~0.1.4"), ("b", ">=0.2")])?.is_satisfiable());
assert!(range(&[("a", "*"), ("b", ">=0.1.4")])?.is_satisfiable());
let any = range(&[("a", "*")])?;
assert!(any.admits(&version("0.0.0")?));
assert!(any.admits(&version("3.0.0")?));
let between = range(&[("a", ">=0.1, <0.3")])?;
assert!(between.admits(&version("0.2.3")?));
assert!(!between.admits(&version("0.3.0")?));
Ok(())
}
#[test]
fn packs_are_kept_in_name_order() -> TestResult {
let found = range(&[("sdlc", "<0.3"), ("core", ">=0.1.4")])?;
let names: Vec<&str> = found
.needs()
.iter()
.map(|(pack, _)| pack.as_str())
.collect();
assert_eq!(names, ["core", "sdlc"]);
assert!(AirslRange::new(Vec::new()).is_none());
Ok(())
}
#[test]
fn disjoint_ranges_are_refused_naming_each_pack() -> TestResult {
let refusal = range(&[("sdlc", "<0.2"), ("core", ">=0.2")])?
.disjoint_refusal(Path::new(PROJECT_FILE))
.ok_or("the ranges do not meet")?;
assert_eq!(
refusal.to_string(),
"agentstacks.toml: airsl: core needs >=0.2, sdlc needs <0.2"
);
let refusal = range(&[("team-lint", ">=0.1"), ("sdlc", "<0.2"), ("core", ">=0.2")])?
.disjoint_refusal(Path::new(PROJECT_FILE))
.ok_or("the ranges do not meet")?;
assert_eq!(
refusal.to_string(),
"agentstacks.toml: airsl: core needs >=0.2, sdlc needs <0.2, team-lint needs >=0.1"
);
assert!(
range(&[("a", ">=0.1")])?
.disjoint_refusal(Path::new(PROJECT_FILE))
.is_none()
);
Ok(())
}
#[test]
fn installed_version_must_satisfy_every_range() -> TestResult {
let refuse =
|range: &AirslRange, installed: &str| -> Result<Option<String>, Box<dyn Error>> {
Ok(range
.installed_refusal(Path::new(PROJECT_FILE), &version(installed)?)
.map(|refusal| refusal.to_string()))
};
let one = range(&[("core", ">=0.1.4")])?;
assert_eq!(
refuse(&one, "0.1.3")?.as_deref(),
Some("agentstacks.toml: airsl 0.1.3 installed, packs need >=0.1.4")
);
assert_eq!(refuse(&one, "0.1.4")?, None);
let two = range(&[("core", ">=0.1.4"), ("sdlc", "<0.3")])?;
assert_eq!(
refuse(&two, "0.1.3")?.as_deref(),
Some("agentstacks.toml: airsl 0.1.3 installed, packs need >=0.1.4, <0.3")
);
assert_eq!(
refuse(&two, "0.3.0")?.as_deref(),
Some("agentstacks.toml: airsl 0.3.0 installed, packs need >=0.1.4, <0.3")
);
Ok(())
}
}