source-mesh-core 1.1.1

Rust dependency graph analysis, diagrams, and cycle reports
Documentation
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
use std::collections::BTreeMap;
use std::collections::BTreeSet;

use anyhow::Context as _;
use anyhow::Result;

use super::markdown::inline_code;
use super::model::Edge;
use super::model::FileGraph;
use super::model::FolderGraph;

const NO_VIOLATIONS_FOUND: &str = "No outgoing dependency violations found.";

#[derive(Clone, Copy)]
pub enum Mode {
    Files,
    Folders,
}

impl Mode {
    #[must_use]
    const fn label(self) -> &'static str {
        match self {
            Self::Files => "files",
            Self::Folders => "folders",
        }
    }
}

pub struct Analysis {
    mode: Mode,
    maximum: usize,
    violations: BTreeMap<String, usize>,
}

impl Analysis {
    pub fn files(graph: &FileGraph, maximum: usize) -> Result<Self> {
        let violations = violating_sources(graph.edges().iter(), maximum).map(|(source, count)| {
            let label = source.path().to_str().context("Outgoing dependency file path is not UTF-8")?;
            Ok((label.to_owned(), count))
        }).collect::<Result<_>>()?;
        Ok(Self { mode: Mode::Files, maximum, violations })
    }

    #[must_use]
    pub fn folders(graph: &FolderGraph, maximum: usize) -> Self {
        let violations = violating_sources(graph.edges().keys(), maximum);
        let violations = violations.map(|(source, count)| (source.label().clone(), count)).collect();
        Self { mode: Mode::Folders, maximum, violations }
    }

    #[must_use]
    pub fn has_violations(&self) -> bool {
        !self.violations.is_empty()
    }

    #[must_use]
    pub fn diagnostic(&self) -> String {
        let mode = self.mode.label();
        let mut output = if self.has_violations() {
            format!(
                "Outgoing dependency threshold exceeded (mode: {mode}, maximum: {}, violating sources: {}).\n",
                self.maximum,
                self.violations.len(),
            )
        } else {
            format!("{NO_VIOLATIONS_FOUND} Mode: {mode}; maximum: {}.\n", self.maximum)
        };
        output.push_str(&self.violation_list(|label| format!("{label:?}")));
        output
    }

    #[must_use]
    pub fn markdown(&self) -> String {
        let mode = self.mode.label();
        let mut output = format!("# Outgoing dependency threshold\n\nMode: {mode}\n\nMaximum: {}\n\n", self.maximum);
        if self.has_violations() {
            output.push_str(&self.violation_list(inline_code));
        } else {
            output.push_str(NO_VIOLATIONS_FOUND);
            output.push('\n');
        }
        output
    }

    #[must_use]
    fn violation_list(&self, label: impl Fn(&str) -> String) -> String {
        let mut output = String::new();
        let maximum = self.maximum.to_string();
        for (source, count) in &self.violations {
            output.push_str("- ");
            output.push_str(&label(source));
            output.push_str(": ");
            output.push_str(&count.to_string());
            output.push_str(" outgoing dependencies (maximum: ");
            output.push_str(&maximum);
            output.push_str(")\n");
        }
        output
    }
}

fn violating_sources<'a, Node: Ord + 'a>(
    edges: impl Iterator<Item = &'a Edge<Node>>,
    maximum: usize,
) -> impl Iterator<Item = (&'a Node, usize)> {
    let mut targets_by_source: BTreeMap<_, BTreeSet<_>> = BTreeMap::new();
    for edge in edges {
        targets_by_source.entry(edge.source()).or_default().insert(edge.target());
    }
    let counts = targets_by_source.into_iter().map(|(source, targets)| (source, targets.len()));
    counts.filter(move |&(_, count)| count > maximum)
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;
    use std::collections::BTreeSet;
    use std::path::Path;
    use std::path::PathBuf;

    use anyhow::Error;
    use anyhow::Result;
    use claims::assert_ok;
    use claims::assert_some;
    use pretty_assertions::assert_eq;
    use rstest::rstest;

    use super::Analysis;
    use super::Mode;
    use super::NO_VIOLATIONS_FOUND;
    use crate::SourcePath;
    use crate::model::DependencyCount;
    use crate::model::DependencyKind;
    use crate::model::DependencyKind::Production;
    use crate::model::DependencyKind::Test;
    use crate::model::FileEdge;
    use crate::model::FileGraph;
    use crate::model::FileNode;
    use crate::model::FolderEdge;
    use crate::model::FolderGraph;
    use crate::model::FolderNode;

    fn file_node_fixture(path: PathBuf) -> Result<FileNode> {
        SourcePath::new(path).map(FileNode::new)
    }

    fn graph_fixture(
        dependencies: &[(&str, &str, DependencyKind, usize)],
        isolated: &[&str],
    ) -> Result<(FileGraph, FolderGraph)> {
        let labels = dependencies.iter().flat_map(|&(source, target, _, _)| [source, target]);
        let labels: BTreeSet<_> = labels.chain(isolated.iter().copied()).collect();
        let files = labels.iter().map(|&label| file_node_fixture(label.into())).collect::<Result<_>>()?;
        let file_edges = dependencies.iter().map(|&(source, target, kind, _)| {
            Ok(FileEdge::new(file_node_fixture(source.into())?, file_node_fixture(target.into())?, kind))
        }).collect::<Result<_>>()?;
        let folders = labels.iter().map(|&label| FolderNode::new(label.into())).collect();
        let folder_edges = dependencies.iter().map(|&(source, target, kind, weight)| {
            let edge = FolderEdge::new(FolderNode::new(source.into()), FolderNode::new(target.into()), kind);
            (edge, DependencyCount::new(weight))
        }).collect();
        Ok((FileGraph::new(files, file_edges)?, FolderGraph::new(folders, folder_edges)?))
    }

    #[rstest]
    fn test_outgoing_direction(#[values(Mode::Files, Mode::Folders)] mode: Mode) {
        let fixture = graph_fixture(&[
            ("alpha", "sink", Production, 1),
            ("beta", "sink", Production, 1),
            ("gamma", "sink", Production, 1),
            ("source", "alpha", Production, 1),
            ("source", "beta", Production, 1),
        ], &[]);
        let (files, folders) = assert_ok!(fixture, "Incoming and outgoing dependencies must form valid graphs");

        let system_under_test = match mode {
            Mode::Files => Analysis::files(&files, 1),
            Mode::Folders => Ok(Analysis::folders(&folders, 1)),
        };
        let system_under_test = assert_ok!(system_under_test, "Directed dependencies must support threshold analysis");

        let expected = BTreeMap::from([(String::from("source"), 2)]);
        assert_eq!(expected, system_under_test.violations, "Incoming dependencies must not contribute to the source count");
        assert!(system_under_test.has_violations(), "Two outgoing targets must exceed a maximum of one");
    }

    #[rstest]
    #[case::zero(0, true)]
    #[case::above(1, true)]
    #[case::equal(2, false)]
    #[case::below(3, false)]
    #[case::maximum(usize::MAX, false)]
    fn test_distinct_targets_and_thresholds(
        #[values(Mode::Files, Mode::Folders)] mode: Mode,
        #[case] maximum: usize,
        #[case] expected_violation: bool,
    ) {
        let fixture = graph_fixture(&[
            ("source", "first", Production, usize::MAX),
            ("source", "first", Test, usize::MAX),
            ("source", "second", Test, 7),
        ], &[]);
        let (files, folders) = assert_ok!(fixture, "Mixed dependency kinds and large weights must form valid graphs");

        let system_under_test = match mode {
            Mode::Files => Analysis::files(&files, maximum),
            Mode::Folders => Ok(Analysis::folders(&folders, maximum)),
        };
        let system_under_test = assert_ok!(system_under_test, "Threshold analysis must accept every usize maximum");

        let expected = if expected_violation { BTreeMap::from([(String::from("source"), 2)]) } else { BTreeMap::new() };
        assert_eq!(expected, system_under_test.violations, "Count distinct targets, not edge kinds or folder weights");
        assert_eq!(expected_violation, system_under_test.has_violations(), "Only counts strictly above the maximum violate");
    }

    #[rstest]
    #[case::empty(&[])]
    #[case::isolated(&["isolated"])]
    fn test_empty_and_isolated_reports(#[values(Mode::Files, Mode::Folders)] mode: Mode, #[case] isolated: &[&str]) {
        let fixture = graph_fixture(&[], isolated);
        let (files, folders) = assert_ok!(fixture, "Empty and isolated-node graphs must be valid");

        let system_under_test = match mode {
            Mode::Files => Analysis::files(&files, 0),
            Mode::Folders => Ok(Analysis::folders(&folders, 0)),
        };
        let system_under_test = assert_ok!(system_under_test, "Graphs without edges must support threshold analysis");
        let diagnostic = system_under_test.diagnostic();
        let markdown = system_under_test.markdown();

        assert!(!system_under_test.has_violations(), "An empty outgoing target set must satisfy a zero maximum");
        assert!(diagnostic.contains(NO_VIOLATIONS_FOUND), "Diagnostic must explicitly state that no sources violate");
        assert!(diagnostic.contains(&format!("Mode: {}; maximum: 0", mode.label())), "Diagnostic must identify its scope");
        assert_eq!(Some(NO_VIOLATIONS_FOUND), markdown.lines().last(), "Markdown must explicitly state that no sources violate");
        assert!(markdown.contains(&format!("Mode: {}", mode.label())), "Markdown must identify the analysis mode");
        assert!(markdown.contains("Maximum: 0"), "Markdown must include the configured zero maximum");
        assert!(!diagnostic.lines().any(|line| line.starts_with("- ")), "Diagnostic must not list isolated nodes");
        assert!(!markdown.lines().any(|line| line.starts_with("- ")), "Markdown must not list isolated nodes");
    }

    #[rstest]
    #[case::above(0, true)]
    #[case::equal(1, false)]
    fn test_self_edges(
        #[values(Mode::Files, Mode::Folders)] mode: Mode,
        #[case] maximum: usize,
        #[case] expected_violation: bool,
    ) {
        let fixture = graph_fixture(&[("self", "self", Production, usize::MAX), ("self", "self", Test, 3)], &[]);
        let (files, folders) = assert_ok!(fixture, "Self edges of both dependency kinds must form valid graphs");

        let system_under_test = match mode {
            Mode::Files => Analysis::files(&files, maximum),
            Mode::Folders => Ok(Analysis::folders(&folders, maximum)),
        };
        let system_under_test = assert_ok!(system_under_test, "Self edges must support threshold analysis");

        let expected = if expected_violation { BTreeMap::from([(String::from("self"), 1)]) } else { BTreeMap::new() };
        assert_eq!(expected, system_under_test.violations, "A retained self edge must count as one distinct target");
        assert_eq!(expected_violation, system_under_test.has_violations(), "Self-edge thresholds must use strict comparison");
    }

    #[rstest]
    fn test_all_violations_in_sorted_order(#[values(Mode::Files, Mode::Folders)] mode: Mode) {
        let fixture = graph_fixture(&[
            ("zebra", "first", Production, 1),
            ("zebra", "second", Test, 1),
            ("middle", "first", Production, 1),
            ("middle", "second", Production, 1),
            ("alpha", "first", Production, 1),
            ("alpha", "second", Production, 1),
            ("alpha", "third", Production, 1),
        ], &[]);
        let (files, folders) = assert_ok!(fixture, "Several violating sources must form valid graphs");

        let system_under_test = match mode {
            Mode::Files => Analysis::files(&files, 1),
            Mode::Folders => Ok(Analysis::folders(&folders, 1)),
        };
        let system_under_test = assert_ok!(system_under_test, "Every violating source must be analyzed");
        let diagnostic = Error::msg(system_under_test.diagnostic()).to_string();
        let markdown = system_under_test.markdown();
        let diagnostic_lines: Vec<_> = diagnostic.lines().filter(|line| line.starts_with("- ")).collect();
        let markdown_lines: Vec<_> = markdown.lines().filter(|line| line.starts_with("- ")).collect();

        assert_eq!(vec![
            "- \"alpha\": 3 outgoing dependencies (maximum: 1)",
            "- \"middle\": 2 outgoing dependencies (maximum: 1)",
            "- \"zebra\": 2 outgoing dependencies (maximum: 1)",
        ], diagnostic_lines, "The complete error string must list all offending sources in label order");
        assert_eq!(vec![
            "- <code>alpha</code>: 3 outgoing dependencies (maximum: 1)",
            "- <code>middle</code>: 2 outgoing dependencies (maximum: 1)",
            "- <code>zebra</code>: 2 outgoing dependencies (maximum: 1)",
        ], markdown_lines, "Markdown must list every source with its actual count and maximum");
        let summary = format!("mode: {}, maximum: 1, violating sources: 3", mode.label());
        assert!(diagnostic.contains(&summary), "The error summary must identify the mode, maximum, and violation total");
        assert!(markdown.contains(&format!("Mode: {}", mode.label())), "Markdown must identify the analysis mode");
        assert!(markdown.contains("Maximum: 1"), "Markdown must include the configured maximum");
    }

    #[test]
    fn test_file_paths_and_folder_labels() {
        let service = file_node_fixture(Path::new("src").join("api").join("deep").join("service.rs"));
        let service = assert_ok!(service, "Nested source file must be a valid project-relative path");
        let database = file_node_fixture(Path::new("src").join("storage").join("database.rs"));
        let database = assert_ok!(database, "Target file must be a valid project-relative path");
        let edge = FileEdge::new(service.clone(), database.clone(), Production);
        let fixture = FileGraph::new(BTreeSet::from([service.clone(), database]), BTreeSet::from([edge]));
        let fixture = assert_ok!(fixture, "Cross-folder dependency must form a valid file graph");
        let folders = fixture.folders(Path::new("src"), Some(1), false);
        let folders = assert_ok!(folders, "Folder aggregation must supply root-relative depth-folded labels");
        let service_path = service.path().to_str();
        let service_path = assert_some!(service_path, "Source fixture path must be UTF-8");

        let system_under_test = Analysis::files(&fixture, 0);
        let system_under_test = assert_ok!(system_under_test, "Nested source paths must support file threshold analysis");
        let folder_analysis = Analysis::folders(&folders, 0);

        let expected_files = BTreeMap::from([(service_path.to_owned(), 1)]);
        assert_eq!(expected_files, system_under_test.violations, "File labels must retain the complete project-relative path");
        let expected_folders = BTreeMap::from([(String::from("api"), 1)]);
        assert_eq!(expected_folders, folder_analysis.violations, "Folder labels must come from the final aggregated graph");
        let diagnostic_path = format!("{service_path:?}");
        assert!(system_under_test.diagnostic().contains(&diagnostic_path), "Diagnostic must retain the complete file path");
    }

    #[rstest]
    fn test_folder_self_edge_filtering(#[values(false, true)] include_self: bool) {
        let service = file_node_fixture(Path::new("src").join("api").join("service.rs"));
        let service = assert_ok!(service, "Service fixture path must be valid");
        let request = file_node_fixture(Path::new("src").join("api").join("request.rs"));
        let request = assert_ok!(request, "Request fixture path must be valid");
        let edge = FileEdge::new(service.clone(), request.clone(), Production);
        let fixture = FileGraph::new(BTreeSet::from([service, request]), BTreeSet::from([edge]));
        let fixture = assert_ok!(fixture, "A dependency inside one folder must form a valid graph");
        let folders = fixture.folders(Path::new("src"), None, include_self);
        let folders = assert_ok!(folders, "Folder aggregation must apply the requested self-edge policy");

        let system_under_test = Analysis::folders(&folders, 0);

        let expected = if include_self { BTreeMap::from([(String::from("api"), 1)]) } else { BTreeMap::new() };
        assert_eq!(expected, system_under_test.violations, "Analysis must count exactly the self edges retained by aggregation");
        assert_eq!(include_self, system_under_test.has_violations(), "Analysis must not add or remove self edges");
    }

    #[rstest]
    #[case("unsafe`<>&\n\r\t", "unsafe&#96;&lt;&gt;&amp;&#92;n&#92;r&#92;t", "\"unsafe`<>&\\n\\r\\t\"")]
    #[case("unsafe*[link]_\\~|", "unsafe&#42;&#91;link&#93;&#95;&#92;&#126;&#124;", "\"unsafe*[link]_\\\\~|\"")]
    #[case(
        "unsafe\n## injected\n<script>alert(1)</script>",
        "unsafe&#92;n## injected&#92;n&lt;script&gt;alert(1)&lt;/script&gt;",
        "\"unsafe\\n## injected\\n<script>alert(1)</script>\""
    )]
    fn test_report_label_escaping(
        #[values(Mode::Files, Mode::Folders)] mode: Mode,
        #[case] label: &str,
        #[case] markdown_label: &str,
        #[case] diagnostic_label: &str,
    ) {
        let fixture = graph_fixture(&[(label, "safe", Production, 1)], &[]);
        let (files, folders) = assert_ok!(fixture, "Markup and control characters must be accepted as lexical graph labels");

        let system_under_test = match mode {
            Mode::Files => Analysis::files(&files, 0),
            Mode::Folders => Ok(Analysis::folders(&folders, 0)),
        };
        let system_under_test = assert_ok!(system_under_test, "Special characters must not prevent threshold analysis");
        let diagnostic = system_under_test.diagnostic();
        let markdown = system_under_test.markdown();
        let expected_diagnostic = format!("- {diagnostic_label}: 1 outgoing dependencies (maximum: 0)");
        let expected_markdown = format!("- <code>{markdown_label}</code>: 1 outgoing dependencies (maximum: 0)");
        let diagnostic_lines: Vec<_> = diagnostic.lines().filter(|line| line.starts_with("- ")).collect();
        let markdown_lines: Vec<_> = markdown.lines().filter(|line| line.starts_with("- ")).collect();

        assert_eq!(vec![expected_diagnostic.as_str()], diagnostic_lines, "Diagnostic labels must remain quoted on one line");
        assert_eq!(vec![expected_markdown.as_str()], markdown_lines, "Markdown must preserve inline-code escaping");
        assert!(!markdown.contains("<script>"), "A graph label must not inject HTML into the report");
        assert!(!markdown.lines().any(|line| line.starts_with("## ")), "A graph label must not inject a report heading");
    }

    #[rstest]
    #[case("unsafe\0\u{7}\u{1b}[31m\u{85}", "\"unsafe\\0\\u{7}\\u{1b}[31m\\u{85}\"")]
    #[case("unsafe\u{2028}\u{202e}", "\"unsafe\\u{2028}\\u{202e}\"")]
    #[case("quote\"and\\backslash", "\"quote\\\"and\\\\backslash\"")]
    fn test_diagnostic_control_characters(
        #[values(Mode::Files, Mode::Folders)] mode: Mode,
        #[case] label: &str,
        #[case] escaped: &str,
    ) {
        let fixture = graph_fixture(&[(label, "safe", Production, 1)], &[]);
        let (files, folders) = assert_ok!(fixture, "Non-printing characters must be accepted as lexical graph labels");

        let system_under_test = match mode {
            Mode::Files => Analysis::files(&files, 0),
            Mode::Folders => Ok(Analysis::folders(&folders, 0)),
        };
        let system_under_test = assert_ok!(system_under_test, "Control characters must not prevent threshold analysis");
        let diagnostic = system_under_test.diagnostic();
        let expected = format!("- {escaped}: 1 outgoing dependencies (maximum: 0)");

        assert_eq!(Some(expected.as_str()), diagnostic.lines().last(), "Diagnostic labels must be quoted and control-safe");
        assert!(diagnostic.chars().all(|character| character == '\n' || !character.is_control()), "Unsafe diagnostic control");
        assert!(!diagnostic.contains('\u{202e}'), "A graph label must not change the diagnostic text direction");
    }

    #[cfg(any(unix, windows))]
    #[rstest]
    #[case::violating_path(0, true)]
    #[case::nonviolating_path(1, false)]
    fn test_file_path_encoding(#[case] maximum: usize, #[case] expected_error: bool) {
        use std::ffi::OsString;

        #[cfg(unix)]
        let file_name = {
            use std::os::unix::ffi::OsStringExt as _;
            OsString::from_vec(vec![0xff])
        };
        #[cfg(windows)]
        let file_name = {
            use std::os::windows::ffi::OsStringExt as _;
            OsString::from_wide(&[0xd800])
        };
        let file = file_node_fixture(file_name.into());
        let file = assert_ok!(file, "A non-UTF-8 filename must remain a valid lexical source path");
        let edge = FileEdge::new(file.clone(), file.clone(), Production);
        let fixture = FileGraph::new(BTreeSet::from([file]), BTreeSet::from([edge]));
        let fixture = assert_ok!(fixture, "A non-UTF-8 source must form a valid file graph");
        let system_under_test = Analysis::files;

        let result = system_under_test(&fixture, maximum);

        assert_eq!(expected_error, result.is_err(), "Only reported source paths require lossless UTF-8 labels");
    }
}