diff --git a/Cargo.lock b/Cargo.lock index e32eb34..a70e650 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -93,7 +93,7 @@ checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" [[package]] name = "closure2wbs" -version = "0.2.0" +version = "0.3.0" dependencies = [ "clap", "serde", diff --git a/Cargo.toml b/Cargo.toml index aa28f67..8d7bcd4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/README.md b/README.md index 3a11732..4b0fd1c 100644 --- a/README.md +++ b/README.md @@ -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 }, { @@ -59,7 +69,12 @@ Prepare a json file like following: }, { "ancestor": "B", - "descendant": "C", + "descendant": "D", + "depth": 1 + }, + { + "ancestor": "B", + "descendant": "E", "depth": 1 }, { @@ -82,7 +97,9 @@ closure2wbs --input input.json --output out.puml --format plantuml @startwbs * A ** B -*** C +*** D +*** E +** C @endwbs ``` @@ -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 ``` diff --git a/examples/mermaid/output.mmd b/examples/mermaid/output.mmd index 39e55c2..973960c 100644 --- a/examples/mermaid/output.mmd +++ b/examples/mermaid/output.mmd @@ -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 diff --git a/src/main.rs b/src/main.rs index 8228af1..3844d9c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -70,7 +70,7 @@ fn retrieve_root_node(json: Vec) -> 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, current: String, level: i32, @@ -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, @@ -104,13 +104,43 @@ fn closure_children_to_wbs_string( wbs_string.to_string() } +fn closure_children_to_mermaid_string(json: Vec, current: String) -> String { + let mut mermaid_string: String = String::new(); + + let mut children: Vec = 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) -> 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 @@ -119,17 +149,9 @@ fn json_to_plantuml_wbs(json: Vec) -> String { fn json_to_mermaid_wbs(json: Vec) -> 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 }