Start to perform Tex export

This commit is contained in:
Pierre HUBERT 2023-02-03 14:48:28 +01:00
parent ed0d8e05f1
commit 4f07dc2d73

View File

@ -29,6 +29,9 @@ enum Action {
/// Dump as Graphviz graph /// Dump as Graphviz graph
Graph, Graph,
/// Dump as Tex list
Tex,
} }
fn main() { fn main() {
@ -47,6 +50,8 @@ fn main() {
match args.action { match args.action {
Action::Json => println!("{}", serde_json::to_string(&tree).unwrap()), Action::Json => println!("{}", serde_json::to_string(&tree).unwrap()),
Action::Graph => println!("{}", graphviz_export(&tree)), 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.push_str("}\n");
out 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
}