use once_cell::sync::Lazy;
use regex::Regex;
pub fn compile(input: &str) -> String {
let mut output = input.to_string();
static AUTH_START: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^\s*@auth\s*$").unwrap());
output = AUTH_START
.replace_all(&output, "{% if auth_check %}")
.to_string();
static ENDAUTH: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^\s*@endauth\s*$").unwrap());
output = ENDAUTH.replace_all(&output, "{% endif %}").to_string();
static GUEST_START: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^\s*@guest\s*$").unwrap());
output = GUEST_START
.replace_all(&output, "{% if not auth_check %}")
.to_string();
static ENDGUEST: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^\s*@endguest\s*$").unwrap());
output = ENDGUEST.replace_all(&output, "{% endif %}").to_string();
static ERROR_START: Lazy<Regex> =
Lazy::new(|| Regex::new(r"@error\s*\(\s*'([^']+)'\s*\)").unwrap());
output = ERROR_START
.replace_all(&output, |caps: ®ex::Captures| {
let field = &caps[1];
format!(r#"{{% if errors["{field}"] %}}"#)
})
.to_string();
static ENDERROR: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^\s*@enderror\s*$").unwrap());
output = ENDERROR.replace_all(&output, "{% endif %}").to_string();
static ELSE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^\s*@else\s*$").unwrap());
output = ELSE.replace_all(&output, "{% else %}").to_string();
static CSRF: Lazy<Regex> = Lazy::new(|| Regex::new(r"@csrf\b").unwrap());
output = CSRF
.replace_all(
&output,
r#"<input type="hidden" name="_token" value="{{ csrf_token }}">"#,
)
.to_string();
static METHOD: Lazy<Regex> =
Lazy::new(|| Regex::new(r"@method\s*\(\s*'([^']+)'\s*\)").unwrap());
output = METHOD
.replace_all(&output, |caps: ®ex::Captures| {
let method = &caps[1];
format!(r#"<input type="hidden" name="_method" value="{}">"#, method)
})
.to_string();
output
}
pub fn compile_with_csrf(input: &str, csrf_token: &str) -> String {
let compiled = compile(input);
compiled.replace("{{ csrf_token }}", csrf_token)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_auth_directive() {
let result = compile("@auth\n<p>Logged in</p>\n@endauth");
assert!(result.contains("{% if auth_check %}"));
assert!(result.contains("{% endif %}"));
assert!(!result.contains("@auth"));
assert!(!result.contains("@endauth"));
}
#[test]
fn test_auth_with_else() {
let result = compile("@auth\n<p>A</p>\n@else\n<p>B</p>\n@endauth");
assert!(result.contains("{% if auth_check %}"));
assert!(result.contains("{% else %}"));
assert!(result.contains("{% endif %}"));
}
#[test]
fn test_auth_indented() {
let result = compile(" @auth\n <p>Hi</p>\n @endauth");
assert!(result.contains("{% if auth_check %}"));
assert!(result.contains("{% endif %}"));
}
#[test]
fn test_guest_directive() {
let result = compile("@guest\n<p>Guest</p>\n@endguest");
assert!(result.contains("{% if not auth_check %}"));
assert!(result.contains("{% endif %}"));
assert!(!result.contains("@guest"));
}
#[test]
fn test_error_directive() {
let result = compile("@error('email')\n<p>Invalid</p>\n@enderror");
assert!(result.contains(r#"{% if errors["email"] %}"#));
assert!(result.contains("{% endif %}"));
assert!(!result.contains("@error"));
}
#[test]
fn test_error_with_spaces() {
let result = compile("@error( 'name' )\n<p>Error</p>\n@enderror");
assert!(result.contains(r#"errors["name"]"#));
}
#[test]
fn test_csrf_directive() {
let result = compile("<form>@csrf</form>");
assert!(result.contains("csrf_token"));
assert!(result.contains("_token"));
assert!(result.contains("type=\"hidden\""));
}
#[test]
fn test_csrf_multiple() {
let result = compile("@csrf\n@csrf");
assert_eq!(result.matches("csrf_token").count(), 2);
}
#[test]
fn test_csrf_word_boundary() {
let result = compile("@csrf_token should be preserved, but @csrf should be replaced");
assert!(result.contains("@csrf_token"));
assert!(result.contains("<input"));
assert!(result.contains("csrf_token"));
}
#[test]
fn test_method_put() {
let result = compile("@method('PUT')");
assert!(result.contains("_method"));
assert!(result.contains("PUT"));
assert!(result.contains("type=\"hidden\""));
}
#[test]
fn test_method_delete() {
let result = compile("@method('DELETE')");
assert!(result.contains("DELETE"));
}
#[test]
fn test_compile_with_csrf_replaces_token() {
let result = compile_with_csrf("@csrf", "abc123");
assert!(result.contains("abc123"));
assert!(!result.contains("{{ csrf_token }}"));
}
#[test]
fn test_mixed_directives() {
let input = "\
@auth
<form method=\"POST\">@csrf</form>
@error('email')
<p>{{ message }}</p>
@enderror
@else
@guest
<p>Please log in</p>
@endguest
@endauth
";
let result = compile(input);
assert!(result.contains("{% if auth_check %}"));
assert!(result.contains("{% else %}"));
assert!(result.contains("{% endif %}"));
assert!(result.contains("csrf_token"));
assert!(result.contains(r#"errors["email"]"#));
assert!(result.contains("{% if not auth_check %}"));
}
#[test]
fn test_no_directives_passthrough() {
let input = "<h1>Hello</h1>\n<p>{{ name }}</p>";
let result = compile(input);
assert_eq!(result, input);
}
}