use crate::views::{
totaljs::{
parser::Parser,
renderer::{RenderContext, Renderer},
},
ViewEngine,
};
use crate::Result;
use serde_json::{json, Value};
use std::sync::{Arc, OnceLock};
static GLOBAL_VIEW_ENGINE: OnceLock<Arc<ViewEngine>> = OnceLock::new();
pub fn initialize_global_view(engine: Arc<ViewEngine>) -> Result<()> {
GLOBAL_VIEW_ENGINE
.set(engine)
.map_err(|_| crate::error::Error::internal("Global VIEW already initialized"))?;
Ok(())
}
pub struct VIEW;
impl VIEW {
pub fn render(
template_path: &str,
model: Value,
repository: Option<Value>,
layout: Option<&str>,
) -> Result<String> {
let engine = GLOBAL_VIEW_ENGINE.get().ok_or_else(|| {
crate::error::Error::internal(
"Global VIEW not initialized. Call initialize_global_view() during app startup",
)
})?;
let repository_value = repository.unwrap_or(json!({}));
let session_value = json!({});
let data = json!({
"data": model,
"_context_repository": repository_value,
"_context_session": session_value
});
engine.render(template_path, &data, layout)
}
pub fn render_string(
template_string: &str,
model: Value,
repository: Option<Value>,
) -> Result<String> {
let mut parser = Parser::new(template_string)?;
let template = parser.parse()?;
let repository_value = repository.unwrap_or(json!({}));
let session_value = json!({});
let context = RenderContext::new(model)
.with_repository(repository_value)
.with_session(session_value);
let mut renderer = Renderer::new(context);
renderer.render(&template)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::views::ViewEngine;
use std::sync::Arc;
fn setup_test_engine() -> Arc<ViewEngine> {
let engine = ViewEngine::builder()
.views_path("views")
.extension("html")
.build();
Arc::new(engine)
}
#[test]
fn test_render_string_basic() {
let template = "Hello @{M.name}!";
let model = json!({"name": "Alice"});
let result = VIEW::render_string(template, model, None);
assert!(result.is_ok());
assert_eq!(result.unwrap().trim(), "Hello Alice!");
}
#[test]
fn test_render_string_with_repository() {
let template = "Hello @{M.name}, welcome to @{R.site_name}!";
let model = json!({"name": "Alice"});
let repository = json!({"site_name": "My Site"});
let result = VIEW::render_string(template, model, Some(repository));
assert!(result.is_ok());
assert_eq!(result.unwrap().trim(), "Hello Alice, welcome to My Site!");
}
#[test]
fn test_render_string_with_conditionals() {
let template = "@{if M.show}Visible@{else}Hidden@{fi}";
let model_true = json!({"show": true});
let result = VIEW::render_string(template, model_true, None);
assert!(result.is_ok());
assert_eq!(result.unwrap().trim(), "Visible");
let model_false = json!({"show": false});
let result = VIEW::render_string(template, model_false, None);
assert!(result.is_ok());
assert_eq!(result.unwrap().trim(), "Hidden");
}
#[test]
fn test_render_string_with_loops() {
let template = "@{foreach item in M.items}@{item} @{end}";
let model = json!({"items": ["A", "B", "C"]});
let result = VIEW::render_string(template, model, None);
assert!(result.is_ok());
assert_eq!(result.unwrap().trim(), "A B C");
}
#[test]
fn test_render_string_with_variables() {
let template = "@{var x = M.a + M.b}Result: @{x}";
let model = json!({"a": 10, "b": 20});
let result = VIEW::render_string(template, model, None);
assert!(result.is_ok());
let output = result.unwrap();
assert!(output.contains("Result:"));
}
#[test]
fn test_render_not_initialized() {
let result = VIEW::render("test", json!({}), None, None);
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("not initialized"));
}
#[test]
fn test_initialize_global_view() {
let _engine = setup_test_engine();
}
#[test]
fn test_render_string_empty_template() {
let template = "";
let model = json!({});
let result = VIEW::render_string(template, model, None);
assert!(result.is_ok());
assert_eq!(result.unwrap().trim(), "");
}
#[test]
fn test_render_string_no_placeholders() {
let template = "Just plain text";
let model = json!({"ignored": "value"});
let result = VIEW::render_string(template, model, None);
assert!(result.is_ok());
assert_eq!(result.unwrap().trim(), "Just plain text");
}
#[test]
fn test_render_string_nested_model() {
let template = "User: @{M.user.name}, Email: @{M.user.email}";
let model = json!({
"user": {
"name": "Alice",
"email": "alice@example.com"
}
});
let result = VIEW::render_string(template, model, None);
assert!(result.is_ok());
let html = result.unwrap();
assert!(html.contains("User: Alice"));
assert!(html.contains("Email: alice@example.com"));
}
#[test]
fn test_render_string_complex_repository() {
let template = "@{M.greeting}, @{M.name}! @{R.footer}";
let model = json!({"greeting": "Hello", "name": "Bob"});
let repository = json!({"footer": "© 2025 My Company"});
let result = VIEW::render_string(template, model, Some(repository));
assert!(result.is_ok());
let html = result.unwrap();
assert!(html.contains("Hello, Bob!"));
assert!(html.contains("© 2025 My Company"));
}
}