From ebfe4d6e7c269bc1b8d78b98ec8f60c3560afca2 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Fri, 3 Feb 2023 15:06:29 +0100 Subject: [PATCH] Improve Tex export --- src/lib.rs | 16 ++++++++++++++-- src/main.rs | 41 ++++++++++++++++++++++++++++++----------- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6e7f0dd..c6e5804 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,6 +29,15 @@ fn expect_schema_object(s: &Schema) -> &SchemaObject { } } +/// Object child information +#[derive(Debug, Clone, serde::Serialize)] +pub struct ObjectChild { + /// The name of the field in the object + pub name: String, + /// The structure of the child + pub node: TreeNode, +} + /// The type of the schema #[derive(Debug, Clone, serde::Serialize)] #[serde(tag = "type")] @@ -47,7 +56,7 @@ pub enum NodeType { /// Required filed for the object required: Option>, /// The children of the object - children: Vec, + children: Vec, }, /// String value String, @@ -219,7 +228,10 @@ fn build_tree_schema( .iter() .map(|e| { let o = expect_schema_object(e.1); - build_tree_schema(o, e.0, components) + ObjectChild { + name: e.0.to_string(), + node: build_tree_schema(o, e.0, components), + } }) .collect::>(); diff --git a/src/main.rs b/src/main.rs index 356fb38..4a2fe3d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use clap::{Parser, Subcommand}; -use openapi_parser::{build_tree, parse_schema, NodeType, TreeNode}; +use openapi_parser::{build_tree, parse_schema, NodeType, ObjectChild, TreeNode}; use std::fmt::Write; /// Dump the tree structure of a schema element as dot file @@ -68,7 +68,7 @@ fn recurse_export(node: &TreeNode, parent_name: &str, out: &mut String) { } NodeType::Object { children, .. } => { for child in children { - recurse_export(child, &node.name, out); + recurse_export(&child.node, &node.name, out); } } @@ -85,15 +85,15 @@ fn graphviz_export(tree: &TreeNode) -> String { out } -fn tex_export_inner(tree: &TreeNode, out: &mut String, required: bool) { - let type_str = match &tree.r#type { +fn tex_export_inner(tree: &ObjectChild, out: &mut String, required: bool) { + let type_str = match &tree.node.r#type { NodeType::Null => "NULL".to_string(), - NodeType::Boolean => "Boolean".to_string(), + NodeType::Boolean => "bool".to_string(), NodeType::Array { item } => format!("{}[]", item.name), - NodeType::Object { .. } => "Objet".to_string(), - NodeType::String => "String".to_string(), - NodeType::Number => "Nombre".to_string(), - NodeType::Integer => "Integer".to_string(), + NodeType::Object { .. } => tree.node.name.to_string(), + NodeType::String => "string".to_string(), + NodeType::Number => "number".to_string(), + NodeType::Integer => "integer".to_string(), }; write!(out, "\\textbf{{{}}}", tree.name).unwrap(); @@ -101,8 +101,20 @@ fn tex_export_inner(tree: &TreeNode, out: &mut String, required: bool) { out.push_str("\\textcolor{red}{*}"); } out.push_str("\n\\newline"); + write!(out, "\\textit{{\\textcolor{{gray}}{{{type_str}}}}}").unwrap(); - write!(out, "\\textbf{{gray}}{{{type_str}}}").unwrap(); + if let Some(description) = &tree.node.description { + write!(out, "\\newline\n\textcolor{{gray}}{{{}}}", description).unwrap(); + } + + if let Some(example) = &tree.node.examples.get(0) { + write!( + out, + "\\newline\n\textcolor{{gray}}{{Exemple : {}}}", + example + ) + .unwrap(); + } } fn tex_export(tree: &TreeNode) -> String { @@ -124,7 +136,14 @@ fn tex_export(tree: &TreeNode) -> String { out.push_str("\n\n"); } } - _ => tex_export_inner(tree, &mut out, false), + _ => tex_export_inner( + &ObjectChild { + name: tree.name.to_string(), + node: tree.clone(), + }, + &mut out, + false, + ), } writeln!(out, "% END OF EXPORT OF STRUCTURE {}", tree.name).unwrap();