Skip to content

Commit

Permalink
feat(hydroflow_lang): open mermaid/dot graph in browser (#923)
Browse files Browse the repository at this point in the history
* `HydroflowGraph::open_mermaid()` opens https://mermaid.live/
* `HydroflowGraph::open_dot()` opens https://dreampuf.github.io/GraphvizOnline/

Behind a new `hydroflow/debugging`/`hydroflow_lang/debugging` feature
gate.
  • Loading branch information
MingweiSamuel authored Oct 4, 2023
1 parent 488d6dd commit e7ea6d8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 5 deletions.
10 changes: 6 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion hydroflow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ documentation = "https://docs.rs/hydroflow/"
description = "Hydro's low-level dataflow runtime and IR"

[features]
default = [ "macros" , "nightly" ]
default = [ "macros" , "nightly", "debugging" ]

nightly = [ "hydroflow_macro", "hydroflow_macro/diagnostics" ]
macros = [ "hydroflow_macro", "hydroflow_datalog" ]
hydroflow_macro = [ "dep:hydroflow_macro" ]
hydroflow_datalog = [ "dep:hydroflow_datalog" ]
cli_integration = [ "dep:hydroflow_cli_integration" ]
python = [ "dep:pyo3" ]
debugging = [ "hydroflow_lang/debugging" ]

[[example]]
name = "kvs_bench"
Expand Down
3 changes: 3 additions & 0 deletions hydroflow_lang/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ description = "Hydroflow's Surface Syntax implementation"
[features]
default = []
diagnostics = []
debugging = [ "dep:data-encoding", "dep:webbrowser" ]

[dependencies]
auto_impl = "1.0.1"
data-encoding = { version = "2.4.0", optional = true }
itertools = "0.10" # TODO(mingwei): remove when `iter_intersperse` is stabilized.
prettyplease = { version = "0.2.0", features = [ "verbatim" ] }
proc-macro2 = { version = "1.0.63", features = ["span-locations"] }
Expand All @@ -22,6 +24,7 @@ serde = "1.0.1"
serde_json = "1.0.8"
slotmap = { version = "1.0.6", features = ["serde"] }
syn = { version = "2.0.0", features = [ "extra-traits", "full", "parsing" ] }
webbrowser = { version = "0.8.11", optional = true }

[dev-dependencies]

Expand Down
39 changes: 39 additions & 0 deletions hydroflow_lang/src/graph/hydroflow_graph_debugging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#![cfg(feature = "debugging")]

use std::fmt::Write;
use std::io::Result;

use super::HydroflowGraph;

impl HydroflowGraph {
/// Opens this as a mermaid graph in the [mermaid.live](https://mermaid.live) browser editor.
pub fn open_mermaid(&self) -> Result<()> {
let mermaid_src = self.to_mermaid();
let state = serde_json::json!({
"code": mermaid_src,
"mermaid": serde_json::json!({
"theme": "default"
}),
"autoSync": true,
"updateDiagram": true
});
let state_json = serde_json::to_vec(&state)?;
let state_base64 = data_encoding::BASE64URL.encode(&state_json);
webbrowser::open(&format!(
"https://mermaid.live/edit#base64:{}",
state_base64
))
}

/// Opens this as dot/graphviz graph in the [Graphviz Online](https://dreampuf.github.io/GraphvizOnline/#)
/// browser editor.
pub fn open_dot(&self) -> Result<()> {
let dot_src = self.to_dot();
let mut url = "https://dreampuf.github.io/GraphvizOnline/#".to_owned();
for byte in dot_src.bytes() {
// Lazy percent encoding: https://en.wikipedia.org/wiki/Percent-encoding
write!(url, "%{:02x}", byte).unwrap();
}
webbrowser::open(&url)
}
}
1 change: 1 addition & 0 deletions hydroflow_lang/src/graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mod flat_to_partitioned;
mod flow_props;
mod graph_write;
mod hydroflow_graph;
mod hydroflow_graph_debugging;

use std::fmt::Display;
use std::path::PathBuf;
Expand Down

0 comments on commit e7ea6d8

Please sign in to comment.