archunit/files/fluentapi/
depend_on_external_module_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
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_external_edge, project_edges,
    },
    files::gather_external_module_dependency_violations,
};

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

/// Executable rule over dependencies from project files to external crates.
#[derive(Debug, Clone)]
#[must_use = "an architecture rule has no effect until it is checked"]
pub struct DependOnExternalModuleCondition {
    builder: DependOnExternalModuleConditionBuilder,
    module_filters: Vec<Filter>,
    module_selector_error: Option<PatternError>,
}

impl DependOnExternalModuleCondition {
    pub(super) fn new(
        builder: DependOnExternalModuleConditionBuilder,
        module_filter: Result<Filter, PatternError>,
    ) -> Self {
        let (module_filters, module_selector_error) = match module_filter {
            Ok(filter) => (vec![filter], None),
            Err(error) => (Vec::new(), Some(error)),
        };
        Self {
            builder,
            module_filters,
            module_selector_error,
        }
    }

    /// Returns the object-stage builder that owns the subject scope and mood.
    pub const fn builder(&self) -> &DependOnExternalModuleConditionBuilder {
        &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 external-crate filters in chain order.
    ///
    /// These filters combine with OR semantics.
    #[must_use]
    pub fn module_filters(&self) -> &[Filter] {
        &self.module_filters
    }

    /// Returns whether matching crate 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 module selector in sentence order.
    #[must_use]
    pub fn selector_error(&self) -> Option<&PatternError> {
        self.builder
            .selector_error()
            .or(self.module_selector_error.as_ref())
    }

    /// Adds another external crate pattern using OR semantics.
    pub fn matching(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.module_selector_error.is_some() {
            return self;
        }

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

impl Checkable for DependOnExternalModuleCondition {
    fn check_with(&self, options: &CheckOptions) -> CheckResult {
        execute_logged_check("files.external-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.module_selector_error {
                return Err(ArchUnitError::from(UserError::with_source(
                    "the external module 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_external_edge());
            logger.log_progress(format!("external dependencies={}", edges.len()))?;

            Ok(gather_external_module_dependency_violations(
                &edges,
                self.subject_filters(),
                self.module_filters(),
                self.is_negated(),
            ))
        })
    }
}

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

    #[test]
    fn matching_selectors_chain_immutably_with_or_semantics() {
        let std_only = project_files_in("examples/layered")
            .should_not()
            .depend_on_external_modules()
            .matching("std");
        let std_or_core = std_only.clone().matching("core");

        assert_eq!(std_only.module_filters().len(), 1);
        assert_eq!(std_or_core.module_filters().len(), 2);
        assert_eq!(std_or_core.module_filters()[0].pattern().source(), "std");
        assert_eq!(std_or_core.module_filters()[1].pattern().source(), "core");
        assert!(std_or_core.is_negated());
    }

    #[test]
    fn retains_the_first_invalid_module_selector_without_breaking_the_chain() {
        let rule = project_files_in("examples/layered")
            .should()
            .depend_on_external_modules()
            .matching("[external")
            .matching("tokio");

        assert!(rule.module_filters().is_empty());
        assert_eq!(
            rule.selector_error().map(|error| error.pattern()),
            Some("[external")
        );
    }
}