Skip to main content

layer_conform_core/
rule.rs

1//! Compiled rule used by the matcher and pipeline.
2//!
3//! Constructed by `layer-conform-io::config` from JSON. `layer-conform-core` keeps no I/O,
4//! so this type only knows about already-compiled glob sets.
5
6use std::path::Path;
7
8use globset::GlobSet;
9
10#[derive(Clone, Debug)]
11pub struct Rule {
12    pub id: String,
13    pub goldens: Vec<GoldenSelector>,
14    pub apply_to: GlobSet,
15    pub ignore: GlobSet,
16    pub threshold: Option<f64>,
17    pub disabled: bool,
18}
19
20#[derive(Clone, Debug, PartialEq, Eq, Hash)]
21pub struct GoldenSelector {
22    pub file: String,
23    pub symbol: String,
24}
25
26impl Rule {
27    /// Returns true if `path` matches `apply_to` and is not excluded by `ignore`.
28    /// Disabled rules never match.
29    pub fn matches(&self, path: &Path) -> bool {
30        if self.disabled {
31            return false;
32        }
33        if !self.apply_to.is_match(path) {
34            return false;
35        }
36        if self.ignore.is_match(path) {
37            return false;
38        }
39        true
40    }
41}