Remove duplicate vertices

This commit is contained in:
Pierre HUBERT 2023-02-10 10:52:50 +01:00
parent 8d1bf2dfb0
commit 235e32d29a

View File

@ -2,6 +2,7 @@ use clap::{Parser, Subcommand};
use openapi_parser::{ use openapi_parser::{
build_tree, parse_schema, parse_schema_fix_example_issue, NodeType, ObjectChild, TreeNode, build_tree, parse_schema, parse_schema_fix_example_issue, NodeType, ObjectChild, TreeNode,
}; };
use std::collections::HashSet;
use std::fmt::Write; use std::fmt::Write;
/// Dump the tree structure of a schema element as dot file /// Dump the tree structure of a schema element as dot file
@ -78,7 +79,17 @@ fn main() {
} }
} }
fn recurse_export(node: &TreeNode, parent_name: &str, out: &mut String) { fn recurse_export(
node: &TreeNode,
parent_name: &str,
out: &mut String,
already_processed: &mut HashSet<String>,
) {
if already_processed.contains(&node.name) {
return;
}
already_processed.insert(node.name.to_string());
if !parent_name.is_empty() && matches!(node.r#type, NodeType::Object { .. }) { if !parent_name.is_empty() && matches!(node.r#type, NodeType::Object { .. }) {
writeln!(out, "\"{}\" -> \"{}\";", parent_name, node.name).unwrap(); writeln!(out, "\"{}\" -> \"{}\";", parent_name, node.name).unwrap();
} }
@ -87,11 +98,11 @@ fn recurse_export(node: &TreeNode, parent_name: &str, out: &mut String) {
NodeType::Array { item } => { NodeType::Array { item } => {
let mut item = item.clone(); let mut item = item.clone();
item.name.push_str(" []"); item.name.push_str(" []");
recurse_export(&item, parent_name, out); recurse_export(&item, parent_name, out, already_processed);
} }
NodeType::Object { children, .. } => { NodeType::Object { children, .. } => {
for child in children { for child in children {
recurse_export(&child.node, &node.name, out); recurse_export(&child.node, &node.name, out, already_processed);
} }
} }
@ -102,7 +113,9 @@ fn recurse_export(node: &TreeNode, parent_name: &str, out: &mut String) {
fn graphviz_export(tree: &TreeNode) -> String { fn graphviz_export(tree: &TreeNode) -> String {
let mut out = "digraph G {\n".to_string(); let mut out = "digraph G {\n".to_string();
recurse_export(tree, "", &mut out); let mut already_processed = HashSet::new();
recurse_export(tree, "", &mut out, &mut already_processed);
out.push_str("}\n"); out.push_str("}\n");
out out