use crate::types::format::TomlFields;
use crate::types::manifest::AirslReq;
use crate::types::manifest::error::ManifestError;
use crate::types::name::BinaryName;
use crate::types::name::PackName;
use crate::types::report::ListedTwice;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Requires {
packs: Vec<PackName>,
airsl: Option<AirslReq>,
binaries: Vec<BinaryName>,
}
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 = fields
.optional_string("airsl")
.and_then(|text| match AirslReq::new(&text) {
Ok(req) => Some(req),
Err(error) => {
fields.refuse("airsl", ManifestError::from(error));
None
}
});
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", "airsl", "binaries"]);
Self {
packs,
airsl,
binaries,
}
}
#[must_use]
pub fn packs(&self) -> &[PackName] {
&self.packs
}
#[must_use]
pub const fn airsl(&self) -> Option<&AirslReq> {
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))
}
#[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(())
}
}