Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the mermaid implementation to use depth and recursively go down the tree #18

Merged
merged 7 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "closure2wbs"
version = "0.2.0"
version = "0.3.0"
description = "A cli tool to convert closure tables to a WBS representation."
categories = ["command-line-utilities"]
edition = "2021"
Expand Down
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ Prepare a json file like following:
{
"ancestor": "A",
"descendant": "C",
"depth": 1
},
{
"ancestor": "A",
"descendant": "D",
"depth": 2
},
{
"ancestor": "A",
"descendant": "E",
"depth": 2
},
{
Expand All @@ -59,7 +69,12 @@ Prepare a json file like following:
},
{
"ancestor": "B",
"descendant": "C",
"descendant": "D",
"depth": 1
},
{
"ancestor": "B",
"descendant": "E",
"depth": 1
},
{
Expand All @@ -82,7 +97,9 @@ closure2wbs --input input.json --output out.puml --format plantuml
@startwbs
* A
** B
*** C
*** D
*** E
** C
@endwbs
```

Expand All @@ -95,6 +112,7 @@ closure2wbs --input input.json --output out.mmd --format mermaid
```mermaid
flowchart TD
A --> B
B --> D
B --> E
A --> C
B --> C
```
4 changes: 2 additions & 2 deletions examples/mermaid/output.mmd
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
flowchart TD
A --> B
B --> C
B --> D
B --> E
C --> F
C --> G
B --> D
D --> H
B --> E
E --> I
50 changes: 36 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn retrieve_root_node(json: Vec<serde_json::Value>) -> String {
}

// Recursively generates the WBS string from the JSON. The level is used to determine the number of asterisks to prepend to the node (used for printing the PlantUML WBS).
fn closure_children_to_wbs_string(
fn closure_children_to_plantuml_string(
json: Vec<serde_json::Value>,
current: String,
level: i32,
Expand All @@ -94,7 +94,7 @@ fn closure_children_to_wbs_string(

// Recursively call the function for each direct child.
for child in children {
wbs_string.push_str(&closure_children_to_wbs_string(
wbs_string.push_str(&closure_children_to_plantuml_string(
json.clone(),
child,
level + 1,
Expand All @@ -104,13 +104,43 @@ fn closure_children_to_wbs_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");

// Starting from the root node, recursively generate the WBS string.
let root_node = retrieve_root_node(json.clone());
plantuml.push_str(&closure_children_to_wbs_string(json.clone(), root_node, 1));
plantuml.push_str(&closure_children_to_plantuml_string(
json.clone(),
root_node,
1,
));

plantuml.push_str("@endwbs\n");
plantuml
Expand All @@ -119,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