start with write

This commit is contained in:
nora 2022-02-12 22:31:58 +01:00
parent 83778ac2c9
commit cb3710cd3f
6 changed files with 1337 additions and 699 deletions

View file

@ -1,9 +1,11 @@
mod parser;
mod write;
use crate::parser::codegen_parser;
use heck::ToUpperCamelCase;
use std::fs;
use strong_xml::XmlRead;
use crate::write::codegen_write;
#[derive(Debug, XmlRead)]
#[xml(tag = "amqp")]
@ -87,6 +89,7 @@ fn codegen(amqp: &Amqp) {
codegen_domain_defs(amqp);
codegen_class_defs(amqp);
codegen_parser(amqp);
codegen_write(amqp);
}
fn codegen_domain_defs(amqp: &Amqp) {

48
amqp_codegen/src/write.rs Normal file
View file

@ -0,0 +1,48 @@
use crate::{field_type, resolve_type_from_domain, snake_case, Amqp};
use heck::ToUpperCamelCase;
pub(crate) fn codegen_write(amqp: &Amqp) {
println!(
"mod write {{
use super::*;
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> {{
match class {{"
);
for class in &amqp.classes {
let class_name = class.name.to_upper_camel_case();
let class_index = class.index;
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} {{");
for field in &method.fields {
let field_name = snake_case(&field.name);
println!(" {field_name},");
}
println!(" }}) => {{");
println!(" writer.write_all(&[{class_index}, {method_index}])?;");
for field in &method.fields {
let field_name = snake_case(&field.name);
let field_type = resolve_type_from_domain(amqp, field_type(field));
if field_type == "bit" {
println!(" todo!();");
} else {
println!(" {field_type}({field_name}, &mut writer)?;");
}
}
println!(" }}");
}
}
println!(
" }}
Ok(())
}}
}}"
);
}

File diff suppressed because it is too large Load diff

View file

@ -4,6 +4,7 @@ use std::collections::HashMap;
mod generated;
mod parse_helper;
mod write_helper;
pub type Table = HashMap<String, FieldValue>;

View file

@ -0,0 +1,47 @@
use crate::classes::generated::{
Bit, Long, Longlong, Longstr, Octet, Short, Shortstr, Table, Timestamp,
};
use crate::error::TransError;
use std::io;
use std::io::Write;
fn error(e: io::Error) -> TransError {
TransError::Other(e.into())
}
pub fn octet<W: Write>(value: Octet, writer: &mut W) -> Result<(), TransError> {
writer.write_all(&[value])?;
Ok(())
}
pub fn short<W: Write>(value: Short, writer: &mut W) -> Result<(), TransError> {
todo!()
}
pub fn long<W: Write>(value: Long, writer: &mut W) -> Result<(), TransError> {
todo!()
}
pub fn longlong<W: Write>(value: Longlong, writer: &mut W) -> Result<(), TransError> {
todo!()
}
pub fn bit<W: Write>(value: Vec<Bit>, writer: &mut W) -> Result<(), TransError> {
todo!()
}
pub fn shortstr<W: Write>(value: Shortstr, writer: &mut W) -> Result<(), TransError> {
todo!()
}
pub fn longstr<W: Write>(value: Longstr, writer: &mut W) -> Result<(), TransError> {
todo!()
}
pub fn timestamp<W: Write>(value: Timestamp, writer: &mut W) -> Result<(), TransError> {
todo!()
}
pub fn table<W: Write>(value: Table, writer: &mut W) -> Result<(), TransError> {
todo!()
}

View file

@ -1,3 +1,5 @@
use std::io::Error;
#[derive(Debug, thiserror::Error)]
pub enum TransError {
#[error("{0}")]
@ -6,6 +8,12 @@ pub enum TransError {
Other(#[from] anyhow::Error),
}
impl From<std::io::Error> for TransError {
fn from(err: Error) -> Self {
Self::Other(err.into())
}
}
#[derive(Debug, thiserror::Error)]
pub enum ProtocolError {
#[error("fatal error")]