archunit/common/projection/
project_to_nodes.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
use std::collections::BTreeMap;

use crate::common::{Edge, Graph};

use super::ProjectedNode;

/// Options controlling projection of a raw graph to file nodes.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct NodeProjectionOptions {
    include_externals: bool,
}

impl NodeProjectionOptions {
    /// Creates the internal-node-only projection configuration.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            include_externals: false,
        }
    }

    /// Returns whether external dependency targets become projected nodes.
    #[must_use]
    pub const fn includes_externals(self) -> bool {
        self.include_externals
    }

    /// Controls whether external dependency targets become projected nodes.
    #[must_use]
    pub const fn with_externals(mut self, include: bool) -> Self {
        self.include_externals = include;
        self
    }
}

/// Projects a graph to its internal nodes using default options.
///
/// Self-edges retain dependency-free files as nodes but are not exposed as incoming or outgoing
/// dependencies.
#[must_use]
pub fn project_to_nodes(graph: &Graph) -> Vec<ProjectedNode> {
    project_to_nodes_with_options(graph, NodeProjectionOptions::default())
}

/// Projects a graph to nodes using explicit options.
#[must_use]
pub fn project_to_nodes_with_options(
    graph: &Graph,
    options: NodeProjectionOptions,
) -> Vec<ProjectedNode> {
    let mut nodes = BTreeMap::<String, (Vec<Edge>, Vec<Edge>)>::new();

    for edge in graph {
        nodes.entry(edge.source.clone()).or_default();
        if edge.is_self_edge() {
            continue;
        }

        if let Some((_, outgoing)) = nodes.get_mut(&edge.source) {
            outgoing.push(edge.clone());
        }

        if !edge.external || options.includes_externals() {
            let (incoming, _) = nodes.entry(edge.target.clone()).or_default();
            incoming.push(edge.clone());
        }
    }

    nodes
        .into_iter()
        .map(|(label, (incoming, outgoing))| ProjectedNode::new(label, incoming, outgoing))
        .collect()
}

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

    use super::{NodeProjectionOptions, project_to_nodes, project_to_nodes_with_options};

    #[test]
    fn retains_isolated_nodes_without_reporting_self_dependencies() {
        let graph = Graph::from_edges([Edge::self_edge("src/isolated.rs")]);

        let nodes = project_to_nodes(&graph);

        assert_eq!(nodes.len(), 1);
        assert_eq!(nodes[0].label, "src/isolated.rs");
        assert!(nodes[0].incoming.is_empty());
        assert!(nodes[0].outgoing.is_empty());
    }

    #[test]
    fn groups_internal_edges_into_sorted_nodes() {
        let dependency = Edge::new("src/a.rs", "src/b.rs", false, [ImportKind::Use]);
        let graph = Graph::from_edges([
            Edge::self_edge("src/b.rs"),
            dependency.clone(),
            Edge::self_edge("src/a.rs"),
        ]);

        let nodes = project_to_nodes(&graph);

        assert_eq!(
            nodes
                .iter()
                .map(|node| node.label.as_str())
                .collect::<Vec<_>>(),
            ["src/a.rs", "src/b.rs"]
        );
        assert_eq!(
            nodes[0].outgoing.as_slice(),
            std::slice::from_ref(&dependency)
        );
        assert_eq!(nodes[1].incoming, [dependency]);
    }

    #[test]
    fn omits_external_target_nodes_but_keeps_source_evidence_by_default() {
        let dependency = Edge::new("src/lib.rs", "serde", true, [ImportKind::Use]);
        let graph = Graph::from_edges([dependency.clone()]);

        let nodes = project_to_nodes(&graph);

        assert_eq!(nodes.len(), 1);
        assert_eq!(nodes[0].label, "src/lib.rs");
        assert_eq!(nodes[0].outgoing, [dependency]);
    }

    #[test]
    fn includes_external_target_nodes_when_requested() {
        let dependency = Edge::new("src/lib.rs", "serde", true, [ImportKind::Use]);
        let graph = Graph::from_edges([dependency.clone()]);

        let nodes = project_to_nodes_with_options(
            &graph,
            NodeProjectionOptions::new().with_externals(true),
        );

        assert_eq!(
            nodes
                .iter()
                .map(|node| node.label.as_str())
                .collect::<Vec<_>>(),
            ["serde", "src/lib.rs"]
        );
        assert_eq!(
            nodes[0].incoming.as_slice(),
            std::slice::from_ref(&dependency)
        );
        assert_eq!(nodes[1].outgoing, [dependency]);
    }
}