From 4f07dc2d7352a0b86b0e86c92e14a1501999056f Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Fri, 3 Feb 2023 14:48:28 +0100 Subject: [PATCH] Start to perform Tex export --- src/main.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/main.rs b/src/main.rs index b118590..356fb38 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,6 +29,9 @@ enum Action { /// Dump as Graphviz graph Graph, + + /// Dump as Tex list + Tex, } fn main() { @@ -47,6 +50,8 @@ fn main() { match args.action { Action::Json => println!("{}", serde_json::to_string(&tree).unwrap()), Action::Graph => println!("{}", graphviz_export(&tree)), + + Action::Tex => println!("{}", tex_export(&tree)), } } @@ -79,3 +84,49 @@ fn graphviz_export(tree: &TreeNode) -> String { out.push_str("}\n"); out } + +fn tex_export_inner(tree: &TreeNode, out: &mut String, required: bool) { + let type_str = match &tree.r#type { + NodeType::Null => "NULL".to_string(), + NodeType::Boolean => "Boolean".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(), + }; + + write!(out, "\\textbf{{{}}}", tree.name).unwrap(); + if required { + out.push_str("\\textcolor{red}{*}"); + } + out.push_str("\n\\newline"); + + write!(out, "\\textbf{{gray}}{{{type_str}}}").unwrap(); +} + +fn tex_export(tree: &TreeNode) -> String { + let mut out = String::new(); + writeln!(out, "% START OF EXPORT OF STRUCTURE {}", tree.name).unwrap(); + + match &tree.r#type { + NodeType::Object { children, required } => { + for child in children { + tex_export_inner( + child, + &mut out, + required + .as_ref() + .map(|r| r.contains(&child.name)) + .unwrap_or(false), + ); + + out.push_str("\n\n"); + } + } + _ => tex_export_inner(tree, &mut out, false), + } + + writeln!(out, "% END OF EXPORT OF STRUCTURE {}", tree.name).unwrap(); + out +}