use std::path::Path;
use semver::Version;
use crate::types::format::TomlFields;
use crate::types::format::parse_toml_document;
use crate::types::manifest::VersionRange;
use crate::types::manifest::error::ManifestError;
use crate::types::name::BinaryName;
use crate::types::name::PackName;
use crate::types::report::Diagnostic;
use crate::types::report::ListedTwice;
use crate::types::report::Location;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Requires {
packs: Vec<PackName>,
agentstacks: Option<VersionRange>,
airsl: Option<VersionRange>,
binaries: Vec<BinaryName>,
}
pub(in crate::types) fn agentstacks_too_new(
file: &Path,
text: &str,
running: &Version,
) -> Option<Diagnostic> {
let table = parse_toml_document(file, text).ok()?;
let written = table.get("requires")?.get("agentstacks")?.as_str()?;
let range = VersionRange::new(written).ok()?;
if range.as_version_req().matches(running) {
return None;
}
Some(Diagnostic::new(
Location::at_key(file, "requires.agentstacks"),
ManifestError::TooNew {
range: Box::new(range),
version: running.clone(),
},
))
}
impl Requires {
pub(in crate::types) fn read(fields: &mut TomlFields, own_name: Option<&PackName>) -> Self {
let mut packs: Vec<PackName> = Vec::new();
for (index, text) in fields.optional_string_list("packs").unwrap_or_default() {
let item_path = fields.item_path("packs", index);
match PackName::new(&text) {
Err(error) => fields.refuse_path(&item_path, ManifestError::from(error)),
Ok(name) if packs.contains(&name) => {
fields.refuse_path(&item_path, ManifestError::from(ListedTwice::text(text)));
}
Ok(name) => {
if own_name == Some(&name) {
fields.refuse_path(&item_path, ManifestError::RequiresItself);
}
packs.push(name);
}
}
}
let airsl = VersionRange::read::<ManifestError>(fields, "airsl");
let agentstacks = VersionRange::read::<ManifestError>(fields, "agentstacks");
let mut binaries: Vec<BinaryName> = Vec::new();
for (index, text) in fields.optional_string_list("binaries").unwrap_or_default() {
let item_path = fields.item_path("binaries", index);
match BinaryName::new(&text) {
Err(error) => fields.refuse_path(&item_path, ManifestError::from(error)),
Ok(name) if binaries.contains(&name) => {
fields.refuse_path(&item_path, ManifestError::from(ListedTwice::text(text)));
}
Ok(name) => binaries.push(name),
}
}
fields.refuse_unknown_keys(&["packs", "agentstacks", "airsl", "binaries"]);
Self {
packs,
agentstacks,
airsl,
binaries,
}
}
#[must_use]
pub fn packs(&self) -> &[PackName] {
&self.packs
}
#[must_use]
pub const fn agentstacks(&self) -> Option<&VersionRange> {
self.agentstacks.as_ref()
}
#[must_use]
pub const fn airsl(&self) -> Option<&VersionRange> {
self.airsl.as_ref()
}
#[must_use]
pub fn binaries(&self) -> &[BinaryName] {
&self.binaries
}
}
#[cfg(test)]
mod tests {
use std::path::Path;
use super::*;
use crate::types::format::parse_toml_document;
type TestResult = Result<(), Box<dyn std::error::Error>>;
const FILE: &str = "packs/sdlc/pack.toml";
fn read(
text: &str,
own: Option<&PackName>,
) -> Result<(Requires, Vec<String>), Box<dyn std::error::Error>> {
let file = Path::new(FILE);
let table = parse_toml_document(file, text).map_err(|d| d.to_string())?;
let mut fields = TomlFields::new(file, "requires", table);
let requires = Requires::read(&mut fields, own);
let shown = fields
.into_diagnostics()
.into_sorted()
.iter()
.map(ToString::to_string)
.collect();
Ok((requires, shown))
}
fn excluded(text: &str, running: &Version) -> Option<String> {
agentstacks_too_new(Path::new(FILE), text, running).map(|found| found.to_string())
}
#[test]
fn agentstacks_too_new_words_the_refusal() {
assert_eq!(
excluded("[requires]\nagentstacks = \">=1.3\"\n", &Version::new(1, 0, 0)),
Some(
"packs/sdlc/pack.toml: requires.agentstacks: \">=1.3\" does not match agentstacks 1.0.0 (install a newer agentstacks)"
.to_owned()
)
);
}
#[test]
fn agentstacks_too_new_is_silent_unless_a_valid_range_excludes() {
let running = Version::new(1, 0, 0);
for text in [
"[requires]\nagentstacks = \">=1.0\"\n",
"[requires]\nagentstacks = \"*\"\n",
"[requires]\nairsl = \">=99\"\n",
"[requires]\nagentstacks = \"nope\"\n",
"[requires]\nagentstacks = 3\n",
"requires = \"x\"\n",
"[pack]\nname = \"a\"\n",
"agentstacks = \">=99\"\na = [1,",
"",
] {
assert_eq!(excluded(text, &running), None, "{text:?}");
}
}
#[test]
fn requires_parses_a_typical_example() -> TestResult {
let (requires, shown) = read(
"packs = [\"core\"]\nairsl = \">=0.1.4\"\nbinaries = [\"git\"]\n",
None,
)?;
assert!(shown.is_empty(), "{shown:?}");
assert_eq!(requires.packs(), [PackName::new("core")?]);
assert_eq!(
requires.airsl().map(ToString::to_string),
Some(">=0.1.4".to_owned())
);
assert_eq!(requires.binaries(), [BinaryName::new("git")?]);
let (none, shown) = read("", None)?;
assert!(shown.is_empty(), "{shown:?}");
assert!(none.packs().is_empty());
assert!(none.airsl().is_none());
assert!(none.binaries().is_empty());
Ok(())
}
#[test]
fn requires_refuses_its_own_name_once_and_a_repeat_as_listed_twice() -> TestResult {
let own = PackName::new("sdlc")?;
let (_, shown) = read("packs = [\"sdlc\", \"sdlc\"]", Some(&own))?;
assert_eq!(
shown,
[
"packs/sdlc/pack.toml: requires.packs[0]: a pack cannot require itself",
"packs/sdlc/pack.toml: requires.packs[1]: \"sdlc\" listed twice",
]
);
Ok(())
}
#[test]
fn requires_refuses_each_rule() -> TestResult {
let own = PackName::new("sdlc")?;
let cases = [
(
"packs = [\"core\", \"core\"]",
"packs/sdlc/pack.toml: requires.packs[1]: \"core\" listed twice",
),
(
"packs = [\"sdlc\"]",
"packs/sdlc/pack.toml: requires.packs[0]: a pack cannot require itself",
),
(
"packs = [\"Core\"]",
"packs/sdlc/pack.toml: requires.packs[0]: \"Core\" is not a valid name",
),
(
"airsl = \"nope\"",
"packs/sdlc/pack.toml: requires.airsl: \"nope\" is not a version range",
),
(
"binaries = [\"git\", \"git\"]",
"packs/sdlc/pack.toml: requires.binaries[1]: \"git\" listed twice",
),
("foo = 1", "packs/sdlc/pack.toml: requires.foo: unknown key"),
];
for (text, message) in cases {
let (_, shown) = read(text, Some(&own))?;
assert_eq!(shown, [message], "{text}");
}
Ok(())
}
#[test]
fn requires_agentstacks_absent_accepts() -> TestResult {
let (requires, shown) = read("airsl = \">=0.1.4\"\n", None)?;
assert!(shown.is_empty(), "{shown:?}");
assert!(requires.agentstacks().is_none());
let (requires, shown) = read("agentstacks = \">=1.0\"\n", None)?;
assert!(shown.is_empty(), "{shown:?}");
assert_eq!(
requires.agentstacks().map(ToString::to_string),
Some(">=1.0".to_owned())
);
Ok(())
}
#[test]
fn agentstacks_range_messages_match_airsl() -> TestResult {
for text in ["", " ", "nope"] {
let (_, shown) = read(&format!("agentstacks = \"{text}\""), None)?;
assert_eq!(
shown,
[format!(
"packs/sdlc/pack.toml: requires.agentstacks: \"{text}\" is not a version range"
)],
"{text:?}"
);
}
let (_, shown) = read("agentstacks = 1", None)?;
assert_eq!(
shown,
["packs/sdlc/pack.toml: requires.agentstacks: expected string, got integer"]
);
Ok(())
}
}