archunit/files/assertion/
file_pattern_violation.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use crate::common::{Filter, ProjectedNode};

/// One selected file that disagrees with a name or location predicate.
///
/// The compiled filter records both the original pattern and the part of the file identifier that
/// was judged. The mood remains data so reporting can describe the rule without reconstructing it.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct FilePatternViolation {
    /// The filename, folder, or path requirement that the file disagreed with.
    pub check_filter: Filter,
    /// The offending file and its dependency evidence.
    pub projected_node: ProjectedNode,
    /// Whether matching the filter was forbidden rather than required.
    pub is_negated: bool,
}

impl FilePatternViolation {
    /// Creates data for one file that failed a pattern predicate.
    #[must_use]
    pub const fn new(
        check_filter: Filter,
        projected_node: ProjectedNode,
        is_negated: bool,
    ) -> Self {
        Self {
            check_filter,
            projected_node,
            is_negated,
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::common::{Graph, PatternTarget, RegexFactory, project_to_nodes};

    use super::FilePatternViolation;

    #[test]
    fn retains_the_requirement_file_evidence_and_mood() {
        let node = project_to_nodes(&Graph::from_edges([crate::common::Edge::self_edge(
            "src/order_service.rs",
        )]))
        .into_iter()
        .next()
        .expect("fixture graph should project one node");
        let filter = RegexFactory::default()
            .filename_matcher("*_service.rs")
            .expect("fixture pattern should compile");

        let violation = FilePatternViolation::new(filter, node, true);

        assert_eq!(violation.check_filter.target(), PatternTarget::Filename);
        assert_eq!(violation.check_filter.pattern().source(), "*_service.rs");
        assert_eq!(violation.projected_node.label, "src/order_service.rs");
        assert!(violation.is_negated);
    }
}