Adding rust support

This commit is contained in:
Fabian Stamm
2024-10-03 19:59:17 +02:00
parent a3f5a396e5
commit 883b6da7eb
6 changed files with 542 additions and 87 deletions

View File

@ -2,24 +2,37 @@ use std::{collections::HashMap, path::PathBuf};
use anyhow::{Context, Result};
use crate::ir::{EnumDefinition, ServiceDefinition, TypeDefinition};
use crate::{
ir::{EnumDefinition, ServiceDefinition, TypeDefinition},
IR,
};
pub trait Compile {
fn new(options: &HashMap<String, String>) -> Result<Self>
where
Self: Sized;
fn name(&self) -> String;
fn start(&mut self, ctx: &mut CompileContext, options: HashMap<String, String>) -> Result<()>;
fn start(&mut self, ctx: &mut CompileContext) -> Result<()>;
fn generate_type(&mut self, ctx: &mut CompileContext, definition: TypeDefinition)
-> Result<()>;
fn generate_enum(&mut self, ctx: &mut CompileContext, definition: EnumDefinition)
-> Result<()>;
fn generate_type(
&mut self,
ctx: &mut CompileContext,
definition: &TypeDefinition,
) -> Result<()>;
fn generate_enum(
&mut self,
ctx: &mut CompileContext,
definition: &EnumDefinition,
) -> Result<()>;
fn generate_service(
&mut self,
ctx: &mut CompileContext,
definition: ServiceDefinition,
definition: &ServiceDefinition,
) -> Result<()>;
fn finalize(&mut self, ctx: &mut CompileContext) -> Result<()>;
fn finalize(&mut self, ctx: &mut CompileContext, ir: &IR) -> Result<()>;
}
pub struct CompileContext {
@ -42,34 +55,57 @@ impl CompileContext {
}
pub struct FileGenerator {
content: String,
content: Vec<String>,
}
impl FileGenerator {
pub fn new() -> Self {
FileGenerator {
content: String::new(),
content: Vec::new(),
}
}
pub fn a<T: ToString>(&mut self, indent: u32, content: T) {
for _ in 0..indent {
self.content.push_str(" ");
}
self.content.push_str(&content.to_string());
self.content.push_str("\n");
pub fn a<T: ToString>(&mut self, indent: usize, content: T) {
let line = " ".repeat(indent) + &content.to_string();
self.content.push(line);
}
pub fn a0<T: ToString>(&mut self, content: T) {
self.a(0, content);
}
pub fn a1<T: ToString>(&mut self, content: T) {
self.a(1, content);
}
pub fn a2<T: ToString>(&mut self, content: T) {
self.a(2, content);
}
pub fn a3<T: ToString>(&mut self, content: T) {
self.a(3, content);
}
pub fn a4<T: ToString>(&mut self, content: T) {
self.a(4, content);
}
pub fn a5<T: ToString>(&mut self, content: T) {
self.a(5, content);
}
pub fn a6<T: ToString>(&mut self, content: T) {
self.a(6, content);
}
pub fn add_line(&mut self, line: &str) {
self.content.push_str(line);
self.content.push_str("\n");
self.content.push(line.to_string());
}
pub fn get_content(&mut self) -> String {
self.content.clone()
pub fn get_content(&self) -> String {
self.content.join("\n")
}
pub fn into_content(self) -> String {
self.content
self.get_content()
}
}