This repository was archived by the owner on Sep 9, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathvisualization.rs
More file actions
105 lines (100 loc) · 2.94 KB
/
Copy pathvisualization.rs
File metadata and controls
105 lines (100 loc) · 2.94 KB
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
// -*- coding: utf-8 -*-
// ------------------------------------------------------------------------------------------------
// Copyright © 2022, stack-graphs authors.
// Licensed under either of Apache License, Version 2.0, or MIT license, at your option.
// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
// ------------------------------------------------------------------------------------------------
use crate::graph::Node;
use crate::graph::StackGraph;
use crate::json::JsonError;
use crate::paths::Path;
use crate::paths::Paths;
static CSS: &'static str = include_str!("visualization.css");
static D3: &'static str = include_str!("d3.v7.min.js");
static D3_DAG: &'static str = include_str!("d3-dag.v0.10.0.min.js");
static JS: &'static str = include_str!("visualization.js");
//-----------------------------------------------------------------------------
// StackGraph
impl StackGraph {
pub fn to_html_string(&self, paths: &mut Paths, title: &str) -> Result<String, JsonError> {
let graph = self.to_json().to_string()?;
let paths = paths
.to_json(self, include_path_in_visualization)
.to_string()?;
let html = format!(
r#"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>{}</title>
<style>
{}
</style>
<style>
html, body, #container {{
width: 100%;
height: 100%;
margin: 0;
}}
</style>
<script type="text/javascript">
{}
</script>
<script type="text/javascript">
{}
</script>
<script charset="utf-8">
{}
</script>
<script type="text/javascript">
let graph = {};
let paths = {};
</script>
</head>
<body>
<div id="controls">
<input type="checkbox" id="show_all_node_labels" />
<label for="show_all_node_labels">show all node labels</label>
</div>
<div id="container">
</div>
<script type="text/javascript">
let container = d3.select("\#container");
let sg = new StackGraph(container, graph, paths);
document.getElementById("show_all_node_labels").addEventListener("click", function (ev) {{
sg.set_show_all_node_labels(ev.target.checked)
}});
</script>
</body>
</html>
"#,
title, CSS, D3, D3_DAG, JS, graph, paths
);
Ok(html)
}
}
fn include_path_in_visualization(graph: &StackGraph, _paths: &Paths, path: &Path) -> bool {
if path.start_node == path.end_node {
return false;
}
if !match &graph[path.start_node] {
Node::PushScopedSymbol(_) | Node::PushSymbol(_) => true,
Node::Root(_) => true,
Node::Scope(node) => node.is_exported,
_ => false,
} {
return false;
}
if !match &graph[path.end_node] {
Node::PopScopedSymbol(_) | Node::PopSymbol(_) => {
path.symbol_stack.is_empty() && path.scope_stack.is_empty()
}
Node::Root(_) => true,
Node::Scope(node) => node.is_exported,
_ => false,
} {
return false;
}
return true;
}