dfir_lang/graph/
meta_graph_debugging.rs

1#![cfg(feature = "debugging")]
2
3use std::fmt::Write;
4use std::io::Result;
5
6use super::meta_graph::WriteConfig;
7use super::{DfirGraph, WriteGraphType};
8
9impl DfirGraph {
10    /// Opens this as a mermaid graph in the [mermaid.live](https://mermaid.live) browser editor.
11    pub fn open_mermaid(&self, write_config: &WriteConfig) -> Result<()> {
12        let mermaid_src = self.to_mermaid(write_config);
13        let state = serde_json::json!({
14            "code": mermaid_src,
15            "mermaid": serde_json::json!({
16                "theme": "default"
17            }),
18            "autoSync": true,
19            "updateDiagram": true
20        });
21        let state_json = serde_json::to_vec(&state)?;
22        let state_base64 = data_encoding::BASE64URL.encode(&state_json);
23        webbrowser::open(&format!(
24            "https://mermaid.live/edit#base64:{}",
25            state_base64
26        ))
27    }
28
29    /// Opens this as dot/graphviz graph in the [Graphviz Online](https://dreampuf.github.io/GraphvizOnline/#)
30    /// browser editor.
31    pub fn open_dot(&self, write_config: &WriteConfig) -> Result<()> {
32        let dot_src = self.to_dot(write_config);
33        let mut url = "https://dreampuf.github.io/GraphvizOnline/#".to_owned();
34        for byte in dot_src.bytes() {
35            // Lazy percent encoding: https://en.wikipedia.org/wiki/Percent-encoding
36            write!(url, "%{:02x}", byte).unwrap();
37        }
38        webbrowser::open(&url)
39    }
40
41    /// Opens the graph based on `graph_type`, which can be parsed by clap.
42    pub fn open_graph(
43        &self,
44        graph_type: WriteGraphType,
45        write_config: Option<WriteConfig>,
46    ) -> Result<()> {
47        let write_config = &write_config.unwrap_or_default();
48        match graph_type {
49            WriteGraphType::Mermaid => self.open_mermaid(write_config),
50            WriteGraphType::Dot => self.open_dot(write_config),
51        }
52    }
53}