rust-relations-explorer 0.1.1

Explore relationships in Rust codebases: build a knowledge graph and run queries (connected files, function usage, cycles, paths, hubs, module centrality, trait impls).
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
use crate::errors::KnowledgeGraphError;
use crate::graph::{ItemType, KnowledgeGraph, RelationshipType};
use std::collections::HashSet;
use std::fmt::Write as _;

#[derive(Debug, Clone, Copy)]
pub enum DotTheme {
    Light,
    Dark,
}

#[derive(Debug, Clone, Copy)]
pub enum RankDir {
    LR,
    TB,
}

#[derive(Debug, Clone, Copy)]
pub enum EdgeStyle {
    Curved,
    Ortho,
    Polyline,
}

#[derive(Debug, Clone, Copy)]
pub struct DotOptions {
    pub clusters: bool,
    pub legend: bool,
    pub theme: DotTheme,
    pub rankdir: RankDir,
    pub splines: EdgeStyle,
    pub rounded: bool,
}

impl Default for DotOptions {
    fn default() -> Self {
        Self {
            clusters: true,
            legend: true,
            theme: DotTheme::Light,
            rankdir: RankDir::LR,
            splines: EdgeStyle::Polyline,
            rounded: true,
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub struct SvgOptions {
    pub dot: DotOptions,
    pub interactive: bool,
}

impl Default for SvgOptions {
    fn default() -> Self {
        Self { dot: DotOptions::default(), interactive: true }
    }
}

#[derive(Debug, Default)]
pub struct SvgGenerator;

impl SvgGenerator {
    #[must_use]
    pub fn new() -> Self {
        Self {}
    }

    /// Generate an SVG rendering using Graphviz.
    ///
    /// # Errors
    /// Returns `KnowledgeGraphError::Visualization` if invoking Graphviz fails,
    /// if the process exits with a non-success status, or if its output is not valid UTF-8.
    pub fn generate_svg_with_options(
        &self,
        graph: &KnowledgeGraph,
        opts: SvgOptions,
    ) -> Result<String, KnowledgeGraphError> {
        let dot = DotGenerator::new().generate_dot_with_options(graph, opts.dot)?;
        // Render with Graphviz `dot -Tsvg`
        let output = std::process::Command::new("dot")
            .arg("-Tsvg")
            .stdin(std::process::Stdio::piped())
            .stdout(std::process::Stdio::piped())
            .spawn()
            .and_then(|mut child| {
                use std::io::Write;
                if let Some(stdin) = child.stdin.as_mut() {
                    stdin.write_all(dot.as_bytes())?;
                }
                child.wait_with_output()
            })
            .map_err(|e| {
                KnowledgeGraphError::Visualization(format!("Failed to run graphviz 'dot': {e}"))
            })?;
        if !output.status.success() {
            return Err(KnowledgeGraphError::Visualization(format!(
                "Graphviz 'dot' failed with code {:?}",
                output.status.code()
            )));
        }
        let mut svg = String::from_utf8(output.stdout).map_err(|e| {
            KnowledgeGraphError::Visualization(format!("Invalid UTF-8 from dot: {e}"))
        })?;
        if opts.interactive {
            svg = enhance_svg(&svg);
        }
        Ok(svg)
    }
}

fn enhance_svg(svg: &str) -> String {
    // Inject minimal CSS/JS for hover highlight and clickable nodes
    let injection = r"
<style>
svg .node:hover ellipse, svg .node:hover polygon, svg .node:hover path, svg .node:hover rect { filter: brightness(1.15); stroke-width: 2; }
svg .edge:hover path { stroke-width: 2.2; }
</style>
<script><![CDATA[
(function(){
  document.querySelectorAll('g.node a').forEach(function(a){
    a.addEventListener('click', function(ev){
      ev.preventDefault();
      const href = a.getAttribute('xlink:href') || a.getAttribute('href');
      if (href) { console.log('clicked', href); }
    });
  });
})();
]]></script>
";
    if let Some(pos) = svg.rfind("</svg>") {
        let mut out = String::with_capacity(svg.len() + injection.len());
        out.push_str(&svg[..pos]);
        out.push_str(injection);
        out.push_str(&svg[pos..]);
        out
    } else {
        let mut out = svg.to_string();
        out.push_str(injection);
        out
    }
}

#[derive(Debug, Default)]
pub struct DotGenerator;

impl DotGenerator {
    #[must_use]
    pub fn new() -> Self {
        Self {}
    }

    /// Generate DOT with default options.
    ///
    /// # Errors
    /// Returns a `KnowledgeGraphError` if DOT generation fails for any reason.
    pub fn generate_dot(&self, graph: &KnowledgeGraph) -> Result<String, KnowledgeGraphError> {
        self.generate_dot_with_options(graph, DotOptions::default())
    }

    /// Generate DOT with the given `opts`.
    ///
    /// # Errors
    /// Returns a `KnowledgeGraphError` if DOT generation fails for any reason.
    pub fn generate_dot_with_options(
        &self,
        graph: &KnowledgeGraph,
        opts: DotOptions,
    ) -> Result<String, KnowledgeGraphError> {
        let mut s = String::new();
        s.push_str("digraph KnowledgeRS\n{");
        s.push('\n');
        let rank = match opts.rankdir {
            RankDir::LR => "LR",
            RankDir::TB => "TB",
        };
        let splines = match opts.splines {
            EdgeStyle::Curved => "curved",
            EdgeStyle::Ortho => "ortho",
            EdgeStyle::Polyline => "polyline",
        };
        let node_style = if opts.rounded { "filled,rounded" } else { "filled" };
        let _ = write!(
            s,
            "  rankdir={rank};\n  graph [fontname=Helvetica, splines={splines}] ;\n  node [shape=box, fontsize=10, style=\"{node_style}\"] ;\n  edge [fontname=Helvetica, fontsize=9];\n"
        );

        if opts.clusters {
            // Build hierarchical clusters from module roots
            let mut visited: HashSet<String> = HashSet::new();
            // Identify roots: files without a module parent
            let mut roots: Vec<_> = graph
                .files
                .keys()
                .filter(|p| graph.get_module_parent(p).is_none())
                .cloned()
                .collect();
            // Stable order for determinism
            roots.sort();
            for root in roots {
                self.write_module_cluster(graph, &root, &mut s, &mut visited, opts.theme);
            }
        } else {
            // No clusters: emit all nodes flat
            let mut paths: Vec<_> = graph.files.keys().cloned().collect();
            paths.sort();
            for path in paths {
                if let Some(file) = graph.files.get(&path) {
                    for item in &file.items {
                        let node_id = sanitize_id(&item.id.0);
                        let (fill, shape) = style_for_item_with_theme(&item.item_type, opts.theme);
                        let url = format!("item://{node_id}");
                        let tooltip = escape_label(&item.name);
                        let _ = writeln!(
                            s,
                            "  \"{node_id}\" [label=\"{}\", fillcolor=\"{fill}\", shape=\"{shape}\", URL=\"{url}\", tooltip=\"{tooltip}\"];",
                            escape_label(&item.name)
                        );
                    }
                }
            }
        }

        // Emit edges (relationships)
        for rel in &graph.relationships {
            let from = sanitize_id(&rel.from_item.0);
            let to = sanitize_id(&rel.to_item.0);
            let (label, color, style) = match &rel.relationship_type {
                RelationshipType::Uses { import_type } => {
                    (format!("uses:{import_type}"), "#1f77b4", "dashed")
                }
                RelationshipType::Implements { trait_name } => {
                    (format!("impl:{trait_name}"), "#2ca02c", "dotted")
                }
                RelationshipType::Contains { containment_type } => {
                    (format!("contains:{containment_type}"), "#7f7f7f", "solid")
                }
                RelationshipType::Extends { extension_type } => {
                    (format!("extends:{extension_type}"), "#9467bd", "dashed")
                }
                RelationshipType::Calls { call_type } => {
                    (format!("calls:{call_type}"), "#d62728", "solid")
                }
            };
            let penwidth = 0.8_f64.max(rel.strength).min(3.0);
            let _ = writeln!(
                s,
                "  \"{from}\" -> \"{to}\" [label=\"{}\", color=\"{color}\", style=\"{style}\", penwidth={penwidth}];",
                escape_label(&label)
            );
        }

        if opts.legend {
            // Legend cluster
            s.push_str("  subgraph cluster_legend {\n    label=\"Legend\";\n    color=grey;\n");
            let legend_items = [
                ("Module", ItemType::Module { is_inline: false }),
                ("Function", ItemType::Function { is_async: false, is_const: false }),
                ("Struct", ItemType::Struct { is_tuple: false }),
                ("Enum", ItemType::Enum { variant_count: 0 }),
                ("Trait", ItemType::Trait { is_object_safe: false }),
            ];
            for (name, t) in legend_items {
                let (fill, shape) = style_for_item_with_theme(&t, opts.theme);
                let id = sanitize_id(&format!("legend_{name}"));
                let _ = writeln!(
                    s,
                    "    \"{id}\" [label=\"{name}\", fillcolor=\"{fill}\", shape=\"{shape}\"]; "
                );
            }
            s.push_str("  }\n");
        }

        s.push_str("}\n");
        Ok(s)
    }

    #[allow(clippy::only_used_in_recursion)]
    fn write_module_cluster(
        &self,
        graph: &KnowledgeGraph,
        path: &std::path::PathBuf,
        out: &mut String,
        visited: &mut HashSet<String>,
        theme: DotTheme,
    ) {
        let key = path.to_string_lossy().to_string();
        if !visited.insert(key.clone()) {
            return;
        }
        let cluster_id = format!("cluster_{}", sanitize_id(&key));
        let label = path.file_name().and_then(|p| p.to_str()).unwrap_or("");
        let _ = write!(
            out,
            "  subgraph \"{cluster_id}\" {{\n    label=\"{}\";\n    color=lightgrey;\n",
            escape_label(label)
        );

        if let Some(file) = graph.files.get(path) {
            for item in &file.items {
                let node_id = sanitize_id(&item.id.0);
                let (fill, shape) = style_for_item_with_theme(&item.item_type, theme);
                let url = format!("item://{node_id}");
                let tooltip = escape_label(&item.name);
                let _ = writeln!(
                    out,
                    "    \"{node_id}\" [label=\"{}\", fillcolor=\"{fill}\", shape=\"{shape}\", URL=\"{url}\", tooltip=\"{tooltip}\"];",
                    escape_label(&item.name)
                );
            }
        }

        // Children
        for child in graph.get_module_children(path) {
            self.write_module_cluster(graph, child, out, visited, theme);
        }
        out.push_str("  }\n");
    }
}

fn sanitize_id(s: &str) -> String {
    s.chars()
        .map(|c| match c {
            'a'..='z' | 'A'..='Z' | '0'..='9' | '_' => c,
            _ => '_',
        })
        .collect()
}

fn escape_label(s: &str) -> String {
    s.replace('"', "\\\"")
}

fn style_for_item_with_theme(t: &ItemType, theme: DotTheme) -> (&'static str, &'static str) {
    match (theme, t) {
        (DotTheme::Light, ItemType::Module { .. }) => ("#e0f3ff", "component"),
        (DotTheme::Light, ItemType::Function { .. }) => ("#e8ffe0", "oval"),
        (DotTheme::Light, ItemType::Struct { .. }) => ("#fff4e0", "box"),
        (DotTheme::Light, ItemType::Enum { .. }) => ("#ffe0f0", "hexagon"),
        (DotTheme::Light, ItemType::Trait { .. }) => ("#f0e0ff", "parallelogram"),
        (DotTheme::Light, ItemType::Impl { .. }) => ("#f0fff0", "box3d"),
        (DotTheme::Light, ItemType::Const) => ("#ffffe0", "note"),
        (DotTheme::Light, ItemType::Static { .. }) => ("#ffffe0", "folder"),
        (DotTheme::Light, ItemType::Type) => ("#f0ffff", "box"),
        (DotTheme::Light, ItemType::Macro) => ("#e0ffe8", "cds"),

        (DotTheme::Dark, ItemType::Module { .. }) => ("#124559", "component"),
        (DotTheme::Dark, ItemType::Function { .. }) => ("#0b6e4f", "oval"),
        (DotTheme::Dark, ItemType::Struct { .. }) => ("#7a4c00", "box"),
        (DotTheme::Dark, ItemType::Enum { .. }) => ("#6a1e44", "hexagon"),
        (DotTheme::Dark, ItemType::Trait { .. }) => ("#3c2a5a", "parallelogram"),
        (DotTheme::Dark, ItemType::Impl { .. }) => ("#1a5e1a", "box3d"),
        (DotTheme::Dark, ItemType::Const) => ("#6b6b00", "note"),
        (DotTheme::Dark, ItemType::Static { .. }) => ("#6b6b00", "folder"),
        (DotTheme::Dark, ItemType::Type) => ("#004f4f", "box"),
        (DotTheme::Dark, ItemType::Macro) => ("#0f5e3a", "cds"),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::graph::ItemType;

    #[test]
    fn test_sanitize_id_basic() {
        let s = "file:/path/to:thing.rs#1";
        let got = sanitize_id(s);
        // Allowed: letters, numbers, underscore; others -> underscore
        assert_eq!(got, "file__path_to_thing_rs_1");
        assert_eq!(sanitize_id("abc_DEF012"), "abc_DEF012");
    }

    #[test]
    fn test_escape_label_quotes() {
        let s = "a\"b\"c";
        let got = escape_label(s);
        assert_eq!(got, "a\\\"b\\\"c");
    }

    #[test]
    fn test_style_for_item_with_theme_all_variants() {
        // Light theme expectations
        let cases_light: Vec<(ItemType, (&str, &str))> = vec![
            (ItemType::Module { is_inline: false }, ("#e0f3ff", "component")),
            (ItemType::Function { is_async: false, is_const: false }, ("#e8ffe0", "oval")),
            (ItemType::Struct { is_tuple: false }, ("#fff4e0", "box")),
            (ItemType::Enum { variant_count: 0 }, ("#ffe0f0", "hexagon")),
            (ItemType::Trait { is_object_safe: false }, ("#f0e0ff", "parallelogram")),
            (ItemType::Impl { trait_name: None, type_name: "T".into() }, ("#f0fff0", "box3d")),
            (ItemType::Const, ("#ffffe0", "note")),
            (ItemType::Static { is_mut: false }, ("#ffffe0", "folder")),
            (ItemType::Type, ("#f0ffff", "box")),
            (ItemType::Macro, ("#e0ffe8", "cds")),
        ];
        for (t, expected) in cases_light {
            assert_eq!(style_for_item_with_theme(&t, DotTheme::Light), expected);
        }

        // Dark theme expectations
        let cases_dark: Vec<(ItemType, (&str, &str))> = vec![
            (ItemType::Module { is_inline: false }, ("#124559", "component")),
            (ItemType::Function { is_async: false, is_const: false }, ("#0b6e4f", "oval")),
            (ItemType::Struct { is_tuple: false }, ("#7a4c00", "box")),
            (ItemType::Enum { variant_count: 0 }, ("#6a1e44", "hexagon")),
            (ItemType::Trait { is_object_safe: false }, ("#3c2a5a", "parallelogram")),
            (ItemType::Impl { trait_name: None, type_name: "T".into() }, ("#1a5e1a", "box3d")),
            (ItemType::Const, ("#6b6b00", "note")),
            (ItemType::Static { is_mut: false }, ("#6b6b00", "folder")),
            (ItemType::Type, ("#004f4f", "box")),
            (ItemType::Macro, ("#0f5e3a", "cds")),
        ];
        for (t, expected) in cases_dark {
            assert_eq!(style_for_item_with_theme(&t, DotTheme::Dark), expected);
        }
    }

    #[test]
    fn test_enhance_svg_injection() {
        let minimal = "<svg></svg>";
        let out = enhance_svg(minimal);
        assert!(out.contains("<style>"));
        assert!(out.ends_with("</svg>"));

        // No closing tag case
        let no_close = "<svg>";
        let out2 = enhance_svg(no_close);
        assert!(out2.contains("<style>"));
    }
}