use crate::{error::Result, fs};
use std::path::Path;
#[derive(Debug, Clone, Copy)]
pub enum ContentTest {
ContainsAny(&'static [&'static str]),
LineStartsWith(&'static str),
NonEmpty,
}
impl ContentTest {
#[must_use]
pub fn matches(self, contents: &str) -> bool {
match self {
Self::ContainsAny(needles) => needles.iter().any(|needle| contents.contains(needle)),
Self::LineStartsWith(prefix) => contents
.lines()
.any(|line| line.trim_start().starts_with(prefix)),
Self::NonEmpty => !contents.trim().is_empty(),
}
}
pub fn matches_file(self, file: &Path) -> Result<bool> {
if fs::entry_kind(file)? != Some(fs::EntryKind::File) {
return Ok(false);
}
Ok(fs::read(file)?.is_some_and(|contents| self.matches(&contents)))
}
}
#[cfg(test)]
mod tests {
use super::*;
use claims::assert_ok_eq;
use std::fs::write;
use tempfile::TempDir;
#[test]
fn contains_any_matches_a_single_needle() {
let test = ContentTest::ContainsAny(&["\"workspaces\"", "\"workspace\""]);
assert!(test.matches(r#"{"name": "x", "workspaces": ["a"]}"#));
assert!(!test.matches(r#"{"name": "x"}"#));
}
#[test]
fn line_starts_with_ignores_position_in_file() {
let test = ContentTest::LineStartsWith("[workspace]");
assert!(test.matches("# a comment\n[workspace]\nmembers = []"));
assert!(test.matches("[workspace]"));
assert!(!test.matches("[workspace.dependencies]\n"));
assert!(!test.matches("[package]\nname = \"x\""));
}
#[test]
fn non_empty_ignores_whitespace_only_files() {
assert!(ContentTest::NonEmpty.matches("{}"));
assert!(!ContentTest::NonEmpty.matches(" \n\t\n"));
}
#[test]
fn a_missing_file_matches_nothing() {
let temp = TempDir::new().expect("create temp dir");
assert_ok_eq!(
ContentTest::NonEmpty.matches_file(&temp.path().join("absent")),
false
);
}
#[test]
fn an_existing_file_is_judged_by_its_contents() {
let temp = TempDir::new().expect("create temp dir");
let manifest = temp.path().join("Cargo.toml");
write(&manifest, "[workspace]\nmembers = []\n").expect("write manifest");
assert_ok_eq!(
ContentTest::LineStartsWith("[workspace]").matches_file(&manifest),
true
);
assert_ok_eq!(
ContentTest::LineStartsWith("[package]").matches_file(&manifest),
false
);
}
}