#![allow(clippy::needless_doctest_main)]
pub mod ast;
pub mod impls;
use crate::impls::{print_impl_from, print_impl_wire_size, print_types, template};
use std::fmt::Write;
pub const DEFAULT_DERIVE: &str = "#[derive(Debug, PartialEq)]";
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error + 'static>>;
#[derive(Debug)]
pub struct Generator {
derive: String,
}
impl std::default::Default for Generator {
fn default() -> Self {
Generator {
derive: DEFAULT_DERIVE.to_string(),
}
}
}
impl Generator {
pub fn with_derive<D: AsRef<str>>(self, derive: D) -> Self {
Self {
derive: derive.as_ref().to_string(),
}
}
pub fn generate<T: AsRef<str>>(&self, xdr: T) -> Result<String> {
let ast = crate::ast::Ast::new(xdr.as_ref())?;
let mut out = String::new();
writeln!(out, "{}", include_str!("header.rs"))?;
print_types(&mut out, &ast, &self.derive.as_str())?;
print_impl_from(&mut out, template::bytes::Bytes, &ast)?;
print_impl_from(&mut out, template::bytes::RefMutBytes, &ast)?;
print_impl_wire_size(&mut out, template::bytes::Bytes, &ast)?;
writeln!(out, "}}")?;
Ok(out)
}
}