archunit/layers/assertion/
layer_definition.rsuse crate::common::Filter;
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct LayerDefinition {
pub name: String,
pub filters: Vec<Filter>,
}
impl LayerDefinition {
#[must_use]
pub fn new(name: impl Into<String>, filters: impl IntoIterator<Item = Filter>) -> Self {
Self {
name: name.into(),
filters: filters.into_iter().collect(),
}
}
#[must_use]
pub fn matches(&self, file_path: &str) -> bool {
self.filters.iter().any(|filter| filter.matches(file_path))
}
}
#[cfg(test)]
mod tests {
use crate::common::RegexFactory;
use super::LayerDefinition;
#[test]
fn selectors_define_one_layer_with_or_semantics() {
let factory = RegexFactory::default();
let layer = LayerDefinition::new(
"application",
[
factory
.folder_matcher("src/api")
.expect("fixture folder should compile"),
factory
.path_matcher("src/legacy/**")
.expect("fixture path should compile"),
],
);
assert!(layer.matches("src/api/handler.rs"));
assert!(layer.matches("src/legacy/handler.rs"));
assert!(!layer.matches("src/database/store.rs"));
}
}