Skip to content

Commit

Permalink
fix mermaid implementation to work down from the root node
Browse files Browse the repository at this point in the history
  • Loading branch information
stefafafan committed Nov 8, 2024
1 parent b0459a2 commit e7e18e9
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,32 @@ fn closure_children_to_plantuml_string(
wbs_string.to_string()
}

fn closure_children_to_mermaid_string(json: Vec<serde_json::Value>, current: String) -> String {
let mut mermaid_string: String = String::new();

let mut children: Vec<String> = Vec::new();

// Find all direct children of the current node.
for record in json.clone() {
let closure_record: ClosureRecord =
serde_json::from_value(record).expect("Unable to deserialize");
if closure_record.ancestor == current
&& closure_record.descendant != current
&& closure_record.depth == 1
{
children.push(closure_record.descendant);
}
}

// Recursively call the function for each direct child.
for child in children {
mermaid_string.push_str(&format!("{} --> {}\n", current, child));
mermaid_string.push_str(&closure_children_to_mermaid_string(json.clone(), child));
}

mermaid_string.to_string()
}

// Converts the JSON to a PlantUML WBS string.
fn json_to_plantuml_wbs(json: Vec<serde_json::Value>) -> String {
let mut plantuml = String::from("@startwbs\n");
Expand All @@ -123,17 +149,9 @@ fn json_to_plantuml_wbs(json: Vec<serde_json::Value>) -> String {
fn json_to_mermaid_wbs(json: Vec<serde_json::Value>) -> String {
let mut mermaid = String::from("flowchart TD\n");

for record in json {
let closure_record: ClosureRecord =
serde_json::from_value(record).expect("Unable to deserialize");
if closure_record.ancestor == closure_record.descendant {
continue;
}
mermaid.push_str(&format!(
"{} --> {}\n",
closure_record.ancestor, closure_record.descendant
));
}
// Starting from the root node, recursively generate the WBS string.
let root_node = retrieve_root_node(json.clone());
mermaid.push_str(&closure_children_to_mermaid_string(json.clone(), root_node));

mermaid
}
Expand Down

0 comments on commit e7e18e9

Please sign in to comment.