archunit/files/fluentapi/
depend_on_file_condition.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use crate::checkable::execute_logged_check;
use crate::{
    checkable::{CheckResult, Checkable},
    common::{
        ArchUnitError, CheckOptions, Filter, PatternError, ProjectLocator, RegexFactory, UserError,
        extract_graph_with_options, locate_project_from, per_internal_edge, project_edges,
    },
    files::gather_file_dependency_violations,
};

use super::{
    DependOnFileConditionBuilder,
    file_rule_support::{empty_selection_violation, selected_nodes},
};

/// Executable rule over dependencies between project files.
#[derive(Debug, Clone)]
#[must_use = "an architecture rule has no effect until it is checked"]
pub struct DependOnFileCondition {
    builder: DependOnFileConditionBuilder,
    object_filters: Vec<Filter>,
    object_selector_error: Option<PatternError>,
}

impl DependOnFileCondition {
    pub(super) fn new(
        builder: DependOnFileConditionBuilder,
        object_filter: Result<Filter, PatternError>,
    ) -> Self {
        let (object_filters, object_selector_error) = match object_filter {
            Ok(filter) => (vec![filter], None),
            Err(error) => (Vec::new(), Some(error)),
        };
        Self {
            builder,
            object_filters,
            object_selector_error,
        }
    }

    /// Returns the object-stage builder that owns the subject scope and mood.
    pub const fn builder(&self) -> &DependOnFileConditionBuilder {
        &self.builder
    }

    /// Returns where Cargo project discovery begins.
    #[must_use]
    pub const fn project_locator(&self) -> &ProjectLocator {
        self.builder.project_locator()
    }

    /// Returns the subject-file filters in chain order.
    #[must_use]
    pub fn subject_filters(&self) -> &[Filter] {
        self.builder.subject_filters()
    }

    /// Returns the dependency-target filters in chain order.
    #[must_use]
    pub fn object_filters(&self) -> &[Filter] {
        &self.object_filters
    }

    /// Returns whether matching object dependencies are forbidden rather than allowed.
    #[must_use]
    pub const fn is_negated(&self) -> bool {
        self.builder.is_negated()
    }

    /// Returns the first invalid subject or object selector in sentence order.
    #[must_use]
    pub fn selector_error(&self) -> Option<&PatternError> {
        self.builder
            .selector_error()
            .or(self.object_selector_error.as_ref())
    }

    /// Further restricts dependency targets by filename using AND semantics.
    pub fn with_name(self, pattern: impl Into<crate::common::PatternSpec>) -> Self {
        let filter = RegexFactory::default().filename_matcher(pattern);
        self.with_filter(filter)
    }

    /// Further restricts dependency targets by containing folder using AND semantics.
    pub fn in_folder(self, pattern: impl Into<crate::common::PatternSpec>) -> Self {
        let filter = RegexFactory::default().folder_matcher(pattern);
        self.with_filter(filter)
    }

    /// Further restricts dependency targets by complete path using AND semantics.
    pub fn in_path(self, pattern: impl Into<crate::common::PatternSpec>) -> Self {
        let filter = RegexFactory::default().path_matcher(pattern);
        self.with_filter(filter)
    }

    fn with_filter(mut self, filter: Result<Filter, PatternError>) -> Self {
        if self.object_selector_error.is_some() {
            return self;
        }

        match filter {
            Ok(filter) => self.object_filters.push(filter),
            Err(error) => self.object_selector_error = Some(error),
        }
        self
    }
}

impl Checkable for DependOnFileCondition {
    fn check_with(&self, options: &CheckOptions) -> CheckResult {
        execute_logged_check("files.dependencies", options, |logger| {
            if let Some(error) = self.builder.selector_error() {
                return Err(ArchUnitError::from(UserError::with_source(
                    "the file scope contains an invalid selector",
                    error.clone(),
                )));
            }
            if let Some(error) = &self.object_selector_error {
                return Err(ArchUnitError::from(UserError::with_source(
                    "the dependency target contains an invalid selector",
                    error.clone(),
                )));
            }

            logger.log_progress("extracting project graph")?;
            let project = locate_project_from(self.project_locator())?;
            let extraction = extract_graph_with_options(&project, options)?;
            let selected = selected_nodes(extraction.graph(), self.subject_filters());
            logger.log_progress(format!("selected files={}", selected.len()))?;
            if let Some(violation) = empty_selection_violation(
                &selected,
                self.subject_filters(),
                self.is_negated(),
                options,
            ) {
                return Ok(vec![violation]);
            }

            let edges = project_edges(extraction.graph(), per_internal_edge());
            logger.log_progress(format!("internal dependencies={}", edges.len()))?;

            Ok(gather_file_dependency_violations(
                &edges,
                self.subject_filters(),
                self.object_filters(),
                self.is_negated(),
            ))
        })
    }
}

#[cfg(test)]
mod tests {
    use crate::{common::PatternTarget, files::project_files_in};

    #[test]
    fn object_selectors_chain_immutably_with_and_semantics() {
        let base = project_files_in("examples/layered")
            .should_not()
            .depend_on_files()
            .in_folder("src/service");
        let named = base.clone().with_name("*_service.rs");
        let path = base.clone().in_path("src/**");

        assert_eq!(base.object_filters().len(), 1);
        assert_eq!(named.object_filters().len(), 2);
        assert_eq!(path.object_filters().len(), 2);
        assert_eq!(
            named.object_filters()[0].target(),
            PatternTarget::PathWithoutFilename
        );
        assert_eq!(named.object_filters()[1].target(), PatternTarget::Filename);
        assert_eq!(path.object_filters()[1].target(), PatternTarget::Path);
        assert!(named.is_negated());
    }

    #[test]
    fn retains_the_first_invalid_object_selector_without_breaking_the_chain() {
        let rule = project_files_in("examples/layered")
            .should()
            .depend_on_files()
            .in_folder("src/[service")
            .with_name("*.rs");

        assert!(rule.object_filters().is_empty());
        assert_eq!(
            rule.selector_error().map(|error| error.pattern()),
            Some("src/[service")
        );
    }
}