comline/core/src/codegen/rust.rs
2023-08-28 20:09:31 +01:00

103 lines
2.4 KiB
Rust

// Standard Uses
// Crate Uses
use crate::schema::ir::frozen::unit::FrozenUnit;
// External Uses
#[allow(unused)]
pub fn frozen_unit_to_code(units: &Vec<FrozenUnit>) -> String {
"".to_owned()
}
/*
fn unit_to_code(frozen_unit: FrozenUnit) -> String {
let mut code = String::new();
use FrozenUnit::*;
match frozen_unit {
Tag(_) => {}
Namespace(_) => {}
Import(_) => {}
Constant { .. } => {}
Property { .. } => {}
Parameter { .. } => {}
ExpressionBlock { .. } => {}
Enum { .. } => {}
EnumVariant(_) => {}
Settings { .. } => {}
Struct { .. } => {}
Protocol { .. } => {}
Function { .. } => {}
Error { .. } => {}
Validator { .. } => {}
_ => panic!("Reached forbidden or unimplemented node")
}
code
}
fn protocol_to_code(unit: FrozenUnit) -> String {
let FrozenUnit::Protocol {
docstring, parameters, name, functions
} = unit else { panic!() };
let mut code = String::new();
// Trait Start
// TODO: Maybe only include async_trait if at least one of the functions need it
code.push_str("#[async_trait]");
code.push_str(&*format!("pub trait {} {{", name));
// Functions
for function in functions {
let FrozenUnit::Function {
docstring, name, synchronous, direction,
arguments, returns, throws
} = function else { panic!() };
let mut fn_code = String::new();
// Async definition (with async_trait)
if !synchronous {
fn_code.push_str("async ");
}
// Fn
fn_code.push_str(&*format!("fn {} (", name));
// Arguments
let mut idx = 0;
while idx != arguments.len() {
let FrozenUnit::Parameter {
name, default_value
} = arguments.get(idx).unwrap();
if argument.id.is_none() {
fn_code.push_str(&*format!("arg{}: {:?}", idx, argument.type_));
if idx != arguments.len() { fn_code.push_str(", ") }
} else {
fn_code.push_str(
&*format!("{}: {:?}", argument.id.clone().unwrap(), argument.type_)
);
if idx != arguments.len() { fn_code.push_str(", ") }
}
idx += 1;
}
// Returns
code.push_str(&*fn_code)
}
// Trait End
code.push_str("}}");
code.to_owned()
}
*/