Skip to content

Commit

Permalink
feat: generate mermaid
Browse files Browse the repository at this point in the history
  • Loading branch information
junhaideng committed Oct 12, 2024
1 parent 4fbb65c commit 2541c38
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,45 @@ impl Codegen {
stream
}

pub fn draw(&self, def_ids: &[DefId]) -> Vec<String> {
let mut ret = Vec::new();
let mut visited = FxHashSet::default();
for def_id in def_ids.iter() {
if let Some(graph) = self.graph(*def_id) {
let mut bytes = String::from("graph TD\n");
let mut node_queue = VecDeque::new();
node_queue.push_back(graph.entry_node);
visited.insert(graph.entry_node);
while !node_queue.is_empty() {
let node = node_queue.pop_front().unwrap();
if let Some(node) = self.node(node) {
if !node.to_nodes.is_empty() {
for to in node.to_nodes.iter() {
if !visited.contains(to) {
node_queue.push_back(*to);
visited.insert(*to);
}
if let Some(to) = self.node(*to) {
bytes.push_str(&node.name);
bytes.push_str(" --> ");
bytes.push_str(&to.name);
bytes.push('\n');
}
}
} else {
bytes.push_str(&node.name);
bytes.push('\n');
}
bytes.push('\n');
}
}
ret.push(bytes);
}
}

ret
}

pub fn write_graph(&mut self, def_id: DefId, stream: &mut TokenStream) {
let graph = self.graph(def_id).unwrap();
let graph_name = self.upper_camel_name(&graph.name).as_syn_ident();
Expand Down
23 changes: 23 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ pub fn configure() -> Builder {
emit_rerun_if_changed: std::env::var_os("CARGO").is_some(),
out_dir: None,
file_name: "gen_graph.rs".into(),
enable_mermaid: false,
}
}

Expand All @@ -211,6 +212,7 @@ pub struct Builder {
emit_rerun_if_changed: bool,
out_dir: Option<PathBuf>,
file_name: PathBuf,
enable_mermaid: bool, // generate mermaid file
}

impl Builder {
Expand All @@ -232,6 +234,12 @@ impl Builder {
self
}

#[must_use]
pub fn enable_mermaid(mut self, enable: bool) -> Self {
self.enable_mermaid = enable;
self
}

pub fn compile(self, graph: impl AsRef<Path>) -> std::io::Result<()> {
let out_dir = if let Some(out_dir) = self.out_dir.as_ref() {
out_dir.clone()
Expand Down Expand Up @@ -261,6 +269,21 @@ impl Builder {
cx.set_tags(tags);

let mut cg = Codegen::new(cx);

if self.enable_mermaid {
for (idx, graph) in cg.draw(&entrys).into_iter().enumerate() {
let mut name = self.file_name.file_stem().unwrap().to_os_string();
if idx != 0 {
name.push(format!("_{}", idx));
}
name.push(".mermaid");
let out = out_dir.join(name);
let mut file = std::io::BufWriter::new(std::fs::File::create(&out).unwrap());
file.write_all(graph.as_bytes()).unwrap();
file.flush().unwrap();
}
}

let stream = cg.write_document(entrys);
let out = out_dir.join(self.file_name);
let mut file = std::io::BufWriter::new(std::fs::File::create(&out).unwrap());
Expand Down

0 comments on commit 2541c38

Please sign in to comment.