more codegen

This commit is contained in:
nora 2022-02-09 17:57:55 +01:00
parent d5fd9abdf7
commit 08628022c2
3 changed files with 40 additions and 12 deletions

View file

@ -6,4 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.53"
heck = "0.4.0"
strong-xml = "0.6.3"

View file

@ -1,3 +1,5 @@
use anyhow::Result;
use heck::{ToSnakeCase, ToUpperCamelCase};
use std::fs;
use strong_xml::{XmlError, XmlRead};
@ -63,18 +65,34 @@ struct Field {
asserts: Vec<Assert>,
}
fn main() {
fn main() -> Result<()> {
let content = fs::read_to_string("./amqp-0-9-1.xml").unwrap();
let amqp: Result<Amqp, XmlError> = Amqp::from_str(&content);
match amqp {
Ok(amqp) => {
println!("{amqp:#?}");
}
Err(err) => {
eprintln!("{err}");
return;
}
}
let amqp = Amqp::from_str(&content)?;
codegen(&amqp)
}
fn codegen(amqp: &Amqp) -> Result<()> {
for class in &amqp.classes {
let enum_name = class.name.to_upper_camel_case();
println!("///////// ---- Class {enum_name}");
println!("enum {enum_name} {{");
for method in &class.methods {
let method_name = method.name.to_upper_camel_case();
print!(" {method_name}");
if method.fields.len() > 0 {
println!(" {{");
for field in &method.fields {
let field_name = field.name.to_snake_case();
println!(" {field_name}: (),");
}
println!(" }}");
} else {
println!(",");
}
}
println!("}}");
}
Ok(())
}