archunit/layers/assertion/
layer_dependency_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::ProjectedEdge;

use super::LayerDependencyRule;

/// One cross-layer dependency rejected by a named-layer policy.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct LayerDependencyViolation {
    /// The concrete file dependency and every raw Rust reference behind it.
    pub dependency: ProjectedEdge,
    /// The declared layer containing the dependency source file.
    pub source_layer: String,
    /// The declared layer containing the dependency target file.
    pub target_layer: String,
    /// The policy kind that rejected the dependency.
    pub rule: LayerDependencyRule,
}

impl LayerDependencyViolation {
    /// Creates data for one rejected cross-layer dependency.
    #[must_use]
    pub fn new(
        dependency: ProjectedEdge,
        source_layer: impl Into<String>,
        target_layer: impl Into<String>,
        rule: LayerDependencyRule,
    ) -> Self {
        Self {
            dependency,
            source_layer: source_layer.into(),
            target_layer: target_layer.into(),
            rule,
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::common::{Edge, ImportKind, ProjectedEdge};

    use super::{LayerDependencyRule, LayerDependencyViolation};

    #[test]
    fn retains_layers_rule_and_raw_dependency_evidence() {
        let raw = Edge::new("src/api.rs", "src/db.rs", false, [ImportKind::Use]);
        let dependency = ProjectedEdge::new("src/api.rs", "src/db.rs", [raw.clone()]);
        let violation = LayerDependencyViolation::new(
            dependency,
            "api",
            "database",
            LayerDependencyRule::MayNotDependOnLayers,
        );

        assert_eq!(violation.source_layer, "api");
        assert_eq!(violation.target_layer, "database");
        assert_eq!(violation.rule, LayerDependencyRule::MayNotDependOnLayers);
        assert_eq!(violation.dependency.cumulated_edges, [raw]);
    }
}