use crate::dae::ast::Dae;
use crate::dae::balance::BalanceResult;
use crate::ir::ast::{ClassDefinition, StoredDefinition};
use anyhow::{Context, Result};
use std::fs;
#[derive(Debug)]
pub struct CompilationResult {
pub dae: Dae,
pub def: StoredDefinition,
pub expanded_class: ClassDefinition,
pub parse_time: std::time::Duration,
pub flatten_time: std::time::Duration,
pub dae_time: std::time::Duration,
pub model_hash: String,
pub balance: BalanceResult,
}
impl CompilationResult {
pub fn total_time(&self) -> std::time::Duration {
self.parse_time + self.flatten_time + self.dae_time
}
pub fn render_template(&mut self, template_path: &str) -> Result<()> {
let template_content = fs::read_to_string(template_path)
.with_context(|| format!("Failed to read template file: {}", template_path))?;
let template_hash = format!("{:x}", chksum_md5::hash(&template_content));
self.dae.template_hash = template_hash;
crate::dae::jinja::render_template(&self.dae, template_path)
}
pub fn render_template_to_string(&mut self, template_path: &str) -> Result<String> {
use minijinja::{Environment, context};
let template_content = fs::read_to_string(template_path)
.with_context(|| format!("Failed to read template file: {}", template_path))?;
let template_hash = format!("{:x}", chksum_md5::hash(&template_content));
self.dae.template_hash = template_hash.clone();
let mut env = Environment::new();
env.add_function("panic", crate::dae::jinja::panic);
env.add_function("warn", crate::dae::jinja::warn);
env.add_template("template", &template_content)?;
let tmpl = env.get_template("template")?;
let output = tmpl.render(context!(dae => &self.dae))?;
Ok(output)
}
pub fn dae(&self) -> &Dae {
&self.dae
}
pub fn dae_mut(&mut self) -> &mut Dae {
&mut self.dae
}
pub fn is_balanced(&self) -> bool {
self.balance.is_balanced()
}
pub fn balance_status(&self) -> String {
self.balance.status_message()
}
pub fn to_dae_ir_json(&self) -> Result<String> {
self.dae
.to_dae_ir_json()
.context("Failed to serialize DAE to DAE IR JSON")
}
}