mirror of
https://github.com/Noratrieb/haesli.git
synced 2026-01-15 04:05:03 +01:00
restructuring
This commit is contained in:
parent
ed4a107c44
commit
9b48dec533
12 changed files with 1988 additions and 1586 deletions
|
|
@ -123,7 +123,7 @@ pub fn main() {
|
|||
|
||||
fn codegen(amqp: &Amqp) {
|
||||
println!("#![allow(dead_code)]");
|
||||
println!("// This file has been generated by `amqp_codegen`. Do not edit it manually.\n");
|
||||
println!("// This file has been generated by `xtask/src/codegen`. Do not edit it manually.\n");
|
||||
codegen_domain_defs(amqp);
|
||||
codegen_class_defs(amqp);
|
||||
codegen_parser(amqp);
|
||||
|
|
@ -159,22 +159,15 @@ fn codegen_domain_defs(amqp: &Amqp) {
|
|||
|
||||
fn codegen_class_defs(amqp: &Amqp) {
|
||||
println!("#[derive(Debug, Clone, PartialEq)]");
|
||||
println!("pub enum Class {{");
|
||||
for class in &amqp.classes {
|
||||
let class_name = class.name.to_upper_camel_case();
|
||||
println!(" {class_name}({class_name}),");
|
||||
}
|
||||
println!("}}\n");
|
||||
println!("pub enum Method {{");
|
||||
|
||||
for class in &amqp.classes {
|
||||
let enum_name = class.name.to_upper_camel_case();
|
||||
doc_comment(&class.doc, 0);
|
||||
println!("#[derive(Debug, Clone, PartialEq)]");
|
||||
println!("pub enum {enum_name} {{");
|
||||
for method in &class.methods {
|
||||
let method_name = method.name.to_upper_camel_case();
|
||||
doc_comment(&class.doc, 4);
|
||||
doc_comment(&method.doc, 4);
|
||||
print!(" {method_name}");
|
||||
print!(" {enum_name}{method_name}");
|
||||
if !method.fields.is_empty() {
|
||||
println!(" {{");
|
||||
for field in &method.fields {
|
||||
|
|
@ -197,8 +190,9 @@ fn codegen_class_defs(amqp: &Amqp) {
|
|||
println!(",");
|
||||
}
|
||||
}
|
||||
println!("}}");
|
||||
}
|
||||
|
||||
println!("}}\n");
|
||||
}
|
||||
|
||||
fn amqp_type_to_rust_type(amqp_type: &str) -> &'static str {
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ pub type IResult<'a, T> = nom::IResult<&'a [u8], T, TransError>;
|
|||
"
|
||||
);
|
||||
println!(
|
||||
"pub fn parse_method(input: &[u8]) -> Result<(&[u8], Class), nom::Err<TransError>> {{
|
||||
"pub fn parse_method(input: &[u8]) -> Result<(&[u8], Method), nom::Err<TransError>> {{
|
||||
alt(({}))(input)
|
||||
}}",
|
||||
amqp.classes
|
||||
|
|
@ -47,7 +47,7 @@ pub type IResult<'a, T> = nom::IResult<&'a [u8], T, TransError>;
|
|||
for class in &amqp.classes {
|
||||
let class_name = class.name.to_snake_case();
|
||||
|
||||
function(&class_name, "Class", || {
|
||||
function(&class_name, "Method", || {
|
||||
let class_index = class.index;
|
||||
let all_methods = class
|
||||
.methods
|
||||
|
|
@ -56,8 +56,8 @@ pub type IResult<'a, T> = nom::IResult<&'a [u8], T, TransError>;
|
|||
.join(", ");
|
||||
let class_name_raw = &class.name;
|
||||
println!(
|
||||
r#" let (input, _) = tag({class_index}_u16.to_be_bytes())(input).map_err(err("invalid tag for class {class_name_raw}"))?;
|
||||
alt(({all_methods}))(input).map_err(err("class {class_name_raw}")).map_err(failure)"#
|
||||
r#" let (input, _) = tag({class_index}_u16.to_be_bytes())(input).map_err(fail_err("invalid tag for class {class_name_raw}"))?;
|
||||
alt(({all_methods}))(input).map_err(fail_err("class {class_name_raw}"))"#
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -94,10 +94,10 @@ fn method_parser(amqp: &Amqp, class: &Class, method: &Method) {
|
|||
let method_name_raw = &method.name;
|
||||
|
||||
let function_name = method_function_name(&class_name)(method);
|
||||
function(&function_name, "Class", || {
|
||||
function(&function_name, "Method", || {
|
||||
let method_index = method.index;
|
||||
println!(
|
||||
r#" let (input, _) = tag({method_index}_u16.to_be_bytes())(input).map_err(err("parsing method index"))?;"#
|
||||
r#" let (input, _) = tag({method_index}_u16.to_be_bytes())(input).map_err(fail_err("parsing method index"))?;"#
|
||||
);
|
||||
let mut iter = method.fields.iter().peekable();
|
||||
while let Some(field) = iter.next() {
|
||||
|
|
@ -108,8 +108,9 @@ fn method_parser(amqp: &Amqp, class: &Class, method: &Method) {
|
|||
let fields_with_bit = subsequent_bit_fields(amqp, field, &mut iter);
|
||||
|
||||
let amount = fields_with_bit.len();
|
||||
// todo: remove those map_err(failure)
|
||||
println!(
|
||||
r#" let (input, bits) = bit(input, {amount}).map_err(err("field {field_name_raw} in method {method_name_raw}")).map_err(failure)?;"#
|
||||
r#" let (input, bits) = bit(input, {amount}).map_err(fail_err("field {field_name_raw} in method {method_name_raw}")).map_err(failure)?;"#
|
||||
);
|
||||
|
||||
for (i, field) in fields_with_bit.iter().enumerate() {
|
||||
|
|
@ -120,7 +121,7 @@ fn method_parser(amqp: &Amqp, class: &Class, method: &Method) {
|
|||
let fn_name = domain_function_name(field_type(field));
|
||||
let field_name = snake_case(&field.name);
|
||||
println!(
|
||||
r#" let (input, {field_name}) = {fn_name}(input).map_err(err("field {field_name_raw} in method {method_name_raw}")).map_err(failure)?;"#
|
||||
r#" let (input, {field_name}) = {fn_name}(input).map_err(fail_err("field {field_name_raw} in method {method_name_raw}")).map_err(failure)?;"#
|
||||
);
|
||||
|
||||
for assert in &field.asserts {
|
||||
|
|
@ -130,12 +131,12 @@ fn method_parser(amqp: &Amqp, class: &Class, method: &Method) {
|
|||
}
|
||||
let class_name = class_name.to_upper_camel_case();
|
||||
let method_name = method.name.to_upper_camel_case();
|
||||
println!(" Ok((input, Class::{class_name}({class_name}::{method_name} {{");
|
||||
println!(" Ok((input, Method::{class_name}{method_name} {{");
|
||||
for field in &method.fields {
|
||||
let field_name = snake_case(&field.name);
|
||||
println!(" {field_name},");
|
||||
}
|
||||
println!(" }})))");
|
||||
println!(" }}))");
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,40 +11,37 @@ use super::*;
|
|||
"
|
||||
);
|
||||
|
||||
impl_random("Class", || {
|
||||
impl_random("Method", || {
|
||||
let class_lens = amqp.classes.len();
|
||||
println!(" match rng.gen_range(0u32..{class_lens}) {{");
|
||||
for (i, class) in amqp.classes.iter().enumerate() {
|
||||
let class_name = class.name.to_upper_camel_case();
|
||||
println!(" {i} => Class::{class_name}({class_name}::random(rng)),");
|
||||
}
|
||||
println!(
|
||||
" _ => unreachable!(),
|
||||
}}"
|
||||
);
|
||||
});
|
||||
println!(" {i} => {{");
|
||||
|
||||
for class in &amqp.classes {
|
||||
let class_name = class.name.to_upper_camel_case();
|
||||
impl_random(&class_name, || {
|
||||
let method_len = class.methods.len();
|
||||
println!(" match rng.gen_range(0u32..{method_len}) {{");
|
||||
println!(" match rng.gen_range(0u32..{method_len}) {{");
|
||||
|
||||
for (i, method) in class.methods.iter().enumerate() {
|
||||
let method_name = method.name.to_upper_camel_case();
|
||||
println!(" {i} => {class_name}::{method_name} {{");
|
||||
println!(" {i} => Method::{class_name}{method_name} {{");
|
||||
for field in &method.fields {
|
||||
let field_name = snake_case(&field.name);
|
||||
println!(" {field_name}: RandomMethod::random(rng),");
|
||||
println!(" {field_name}: RandomMethod::random(rng),");
|
||||
}
|
||||
println!(" }},");
|
||||
println!(" }},");
|
||||
}
|
||||
println!(
|
||||
" _ => unreachable!(),
|
||||
}}"
|
||||
" _ => unreachable!(),
|
||||
}}"
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
println!(" }}");
|
||||
}
|
||||
println!(
|
||||
" _ => unreachable!(),
|
||||
}}"
|
||||
);
|
||||
});
|
||||
|
||||
println!("}}");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ use crate::classes::write_helper::*;
|
|||
use crate::error::TransError;
|
||||
use std::io::Write;
|
||||
|
||||
pub fn write_method<W: Write>(class: Class, mut writer: W) -> Result<(), TransError> {{
|
||||
pub fn write_method<W: Write>(class: Method, mut writer: W) -> Result<(), TransError> {{
|
||||
match class {{"
|
||||
);
|
||||
|
||||
|
|
@ -19,12 +19,12 @@ pub fn write_method<W: Write>(class: Class, mut writer: W) -> Result<(), TransEr
|
|||
for method in &class.methods {
|
||||
let method_name = method.name.to_upper_camel_case();
|
||||
let method_index = method.index;
|
||||
println!(" Class::{class_name}({class_name}::{method_name} {{");
|
||||
println!(" Method::{class_name}{method_name} {{");
|
||||
for field in &method.fields {
|
||||
let field_name = snake_case(&field.name);
|
||||
println!(" {field_name},");
|
||||
}
|
||||
println!(" }}) => {{");
|
||||
println!(" }} => {{");
|
||||
let [ci0, ci1] = class_index.to_be_bytes();
|
||||
let [mi0, mi1] = method_index.to_be_bytes();
|
||||
println!(" writer.write_all(&[{ci0}, {ci1}, {mi0}, {mi1}])?;");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue