Improve Tex export

This commit is contained in:
Pierre HUBERT 2023-02-03 15:06:29 +01:00
parent 4f07dc2d73
commit ebfe4d6e7c
2 changed files with 44 additions and 13 deletions

View File

@ -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 /// The type of the schema
#[derive(Debug, Clone, serde::Serialize)] #[derive(Debug, Clone, serde::Serialize)]
#[serde(tag = "type")] #[serde(tag = "type")]
@ -47,7 +56,7 @@ pub enum NodeType {
/// Required filed for the object /// Required filed for the object
required: Option<Vec<String>>, required: Option<Vec<String>>,
/// The children of the object /// The children of the object
children: Vec<TreeNode>, children: Vec<ObjectChild>,
}, },
/// String value /// String value
String, String,
@ -219,7 +228,10 @@ fn build_tree_schema(
.iter() .iter()
.map(|e| { .map(|e| {
let o = expect_schema_object(e.1); 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::<Vec<_>>(); .collect::<Vec<_>>();

View File

@ -1,5 +1,5 @@
use clap::{Parser, Subcommand}; 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; 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
@ -68,7 +68,7 @@ fn recurse_export(node: &TreeNode, parent_name: &str, out: &mut String) {
} }
NodeType::Object { children, .. } => { NodeType::Object { children, .. } => {
for child in 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 out
} }
fn tex_export_inner(tree: &TreeNode, out: &mut String, required: bool) { fn tex_export_inner(tree: &ObjectChild, out: &mut String, required: bool) {
let type_str = match &tree.r#type { let type_str = match &tree.node.r#type {
NodeType::Null => "NULL".to_string(), NodeType::Null => "NULL".to_string(),
NodeType::Boolean => "Boolean".to_string(), NodeType::Boolean => "bool".to_string(),
NodeType::Array { item } => format!("{}[]", item.name), NodeType::Array { item } => format!("{}[]", item.name),
NodeType::Object { .. } => "Objet".to_string(), NodeType::Object { .. } => tree.node.name.to_string(),
NodeType::String => "String".to_string(), NodeType::String => "string".to_string(),
NodeType::Number => "Nombre".to_string(), NodeType::Number => "number".to_string(),
NodeType::Integer => "Integer".to_string(), NodeType::Integer => "integer".to_string(),
}; };
write!(out, "\\textbf{{{}}}", tree.name).unwrap(); 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("\\textcolor{red}{*}");
} }
out.push_str("\n\\newline"); 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 { fn tex_export(tree: &TreeNode) -> String {
@ -124,7 +136,14 @@ fn tex_export(tree: &TreeNode) -> String {
out.push_str("\n\n"); 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(); writeln!(out, "% END OF EXPORT OF STRUCTURE {}", tree.name).unwrap();