use std::cell::RefCell;
use std::collections::HashMap;
use std::sync::Arc;
use rustango::core::Column as _;
use rustango::sql::FetcherPool as _;
use tera::{Filter, Function, Tera, Value};
use crate::page::Page;
pub const MAX_PREFETCH_IDS: usize = 16;
thread_local! {
static CURRENT_PAGES_BY_ID: RefCell<Option<Arc<HashMap<i64, String>>>>
= const { RefCell::new(None) };
static CURRENT_URL_PREFIX: RefCell<String> = const { RefCell::new(String::new()) };
static CURRENT_SITE_PREFIX: RefCell<String> = const { RefCell::new(String::new()) };
}
#[must_use = "the guard clears the thread-local on drop; bind it to a local"]
pub struct PageUrlGuard {
_priv: (),
}
impl Drop for PageUrlGuard {
fn drop(&mut self) {
CURRENT_PAGES_BY_ID.with(|cell| {
*cell.borrow_mut() = None;
});
}
}
pub fn install(pages_by_id: HashMap<i64, String>) -> PageUrlGuard {
CURRENT_PAGES_BY_ID.with(|cell| {
*cell.borrow_mut() = Some(Arc::new(pages_by_id));
});
PageUrlGuard { _priv: () }
}
#[must_use]
pub fn lookup(id: i64) -> Option<String> {
if id <= 0 {
return None;
}
CURRENT_PAGES_BY_ID.with(|cell| cell.borrow().as_ref().and_then(|m| m.get(&id).cloned()))
}
#[must_use = "the guard clears the thread-local on drop; bind it to a local"]
pub struct UrlPrefixGuard {
_priv: (),
}
impl Drop for UrlPrefixGuard {
fn drop(&mut self) {
CURRENT_URL_PREFIX.with(|cell| cell.borrow_mut().clear());
}
}
#[must_use = "the guard clears the thread-local on drop; bind it to a local"]
pub struct SitePrefixGuard {
_priv: (),
}
impl Drop for SitePrefixGuard {
fn drop(&mut self) {
CURRENT_SITE_PREFIX.with(|cell| cell.borrow_mut().clear());
}
}
pub fn install_site_prefix(prefix: &str) -> SitePrefixGuard {
CURRENT_SITE_PREFIX.with(|cell| {
*cell.borrow_mut() = prefix.to_owned();
});
SitePrefixGuard { _priv: () }
}
fn current_site_prefix() -> String {
CURRENT_SITE_PREFIX.with(|cell| cell.borrow().clone())
}
pub fn install_url_prefix(prefix: &str) -> UrlPrefixGuard {
CURRENT_URL_PREFIX.with(|cell| {
*cell.borrow_mut() = prefix.to_owned();
});
UrlPrefixGuard { _priv: () }
}
fn current_url_prefix() -> String {
CURRENT_URL_PREFIX.with(|cell| cell.borrow().clone())
}
pub fn register_tera_function(tera: &mut Tera) {
tera.register_function("pageurl", PageurlFn);
tera.register_filter("pageurl", PageurlFilter);
tera.register_function("page_href", PageHrefFn);
tera.register_filter("page_href", PageHrefFilter);
tera.register_function("page_link", PageLinkFn);
tera.register_filter("page_link", PageLinkFilter);
}
struct PageLinkFn;
impl Function for PageLinkFn {
fn call(&self, args: &HashMap<String, Value>) -> tera::Result<Value> {
let (href, title) = match args.get("page") {
Some(page) => (href_from_value(page), title_from_value(page)),
None => {
let id = args.get("id").and_then(read_id);
(href_from_id(id), None)
}
};
let class = args.get("class").and_then(Value::as_str);
Ok(Value::String(anchor(&href, title.as_deref(), class)))
}
fn is_safe(&self) -> bool {
true
}
}
struct PageLinkFilter;
impl Filter for PageLinkFilter {
fn filter(&self, value: &Value, args: &HashMap<String, Value>) -> tera::Result<Value> {
let class = args.get("class").and_then(Value::as_str);
Ok(Value::String(anchor(
&href_from_value(value),
title_from_value(value).as_deref(),
class,
)))
}
fn is_safe(&self) -> bool {
true
}
}
fn title_from_value(value: &Value) -> Option<String> {
value
.get("title")
.and_then(Value::as_str)
.filter(|s| !s.is_empty())
.map(str::to_owned)
}
fn anchor(href: &str, title: Option<&str>, class: Option<&str>) -> String {
let label = escape_html(title.unwrap_or(href));
match class {
Some(c) if !c.is_empty() => {
format!(r#"<a class="{}" href="{href}">{label}</a>"#, escape_html(c))
}
_ => format!(r#"<a href="{href}">{label}</a>"#),
}
}
fn escape_html(s: &str) -> String {
let mut out = String::with_capacity(s.len());
for c in s.chars() {
match c {
'&' => out.push_str("&"),
'<' => out.push_str("<"),
'>' => out.push_str(">"),
'"' => out.push_str("""),
'\'' => out.push_str("'"),
_ => out.push(c),
}
}
out
}
struct PageHrefFn;
impl Function for PageHrefFn {
fn call(&self, args: &HashMap<String, Value>) -> tera::Result<Value> {
let href = match args.get("page") {
Some(page) => href_from_value(page),
None => href_from_id(args.get("id").and_then(read_id)),
};
Ok(Value::String(href))
}
fn is_safe(&self) -> bool {
true
}
}
struct PageHrefFilter;
impl Filter for PageHrefFilter {
fn filter(&self, value: &Value, _args: &HashMap<String, Value>) -> tera::Result<Value> {
Ok(Value::String(href_from_value(value)))
}
fn is_safe(&self) -> bool {
true
}
}
fn href_from_value(value: &Value) -> String {
if let Some(url_path) = value.get("url_path").and_then(Value::as_str) {
return with_prefix(url_path);
}
href_from_id(read_id(value))
}
fn href_from_id(id: Option<i64>) -> String {
match id.and_then(lookup) {
Some(url_path) => with_prefix(&url_path),
None => "#".to_owned(),
}
}
fn with_prefix(url_path: &str) -> String {
let prefix = current_url_prefix();
let site = current_site_prefix();
let url_path = crate::site::to_public_path(&site, url_path);
if url_path.is_empty() {
format!("{prefix}/")
} else {
format!("{prefix}{url_path}")
}
}
struct PageurlFn;
impl Function for PageurlFn {
fn call(&self, args: &HashMap<String, Value>) -> tera::Result<Value> {
let id = args.get("id").and_then(read_id);
Ok(Value::String(href_from_id(id)))
}
fn is_safe(&self) -> bool {
true
}
}
struct PageurlFilter;
impl Filter for PageurlFilter {
fn filter(&self, value: &Value, _args: &HashMap<String, Value>) -> tera::Result<Value> {
Ok(Value::String(href_from_id(read_id(value))))
}
fn is_safe(&self) -> bool {
true
}
}
fn read_id(v: &Value) -> Option<i64> {
if v.is_null() {
return None;
}
if let Some(n) = v.as_i64() {
return Some(n);
}
v.as_str()
.map(str::trim)
.filter(|s| !s.is_empty())
.and_then(|s| s.parse::<i64>().ok())
}
pub async fn prefetch_for_extension(
extension: &serde_json::Value,
pool: &rustango::sql::Pool,
) -> HashMap<i64, String> {
let mut ids: Vec<i64> = candidate_ids(extension);
if ids.is_empty() {
return HashMap::new();
}
ids.sort_unstable();
ids.dedup();
if ids.len() > MAX_PREFETCH_IDS {
tracing::debug!(
target: "rustango_cms::page_url",
count = ids.len(),
cap = MAX_PREFETCH_IDS,
"extension referenced more *_id fields than the prefetch cap; tail truncated",
);
ids.truncate(MAX_PREFETCH_IDS);
}
match Page::objects()
.where_(Page::id.is_in(ids))
.fetch(pool)
.await
{
Ok(rows) => rows
.into_iter()
.filter_map(|p| p.id.get().copied().map(|id| (id, p.url_path)))
.collect(),
Err(e) => {
tracing::warn!(
target: "rustango_cms::page_url",
error = %e,
"pageurl prefetch failed; templates will see '#'",
);
HashMap::new()
}
}
}
fn candidate_ids(extension: &serde_json::Value) -> Vec<i64> {
let Some(obj) = extension.as_object() else {
return Vec::new();
};
obj.iter()
.filter(|(k, _)| k.ends_with("_id"))
.filter_map(|(_, v)| read_id(v))
.filter(|n| *n > 0)
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
use tera::{Context, Tera};
fn fresh_tera() -> Tera {
let mut tera = Tera::default();
register_tera_function(&mut tera);
tera
}
#[test]
fn function_resolves_known_id() {
let mut map = HashMap::new();
map.insert(42i64, "/about".to_owned());
let _guard = install(map);
let tera = fresh_tera();
let mut t = tera.clone();
t.add_raw_template("t.html", "{{ pageurl(id=42) }}")
.unwrap();
let out = t.render("t.html", &Context::new()).unwrap();
assert_eq!(out, "/about");
}
#[test]
fn function_returns_hash_for_unknown_id() {
let _guard = install(HashMap::new());
let tera = fresh_tera();
let mut t = tera.clone();
t.add_raw_template("t.html", "{{ pageurl(id=999) }}")
.unwrap();
let out = t.render("t.html", &Context::new()).unwrap();
assert_eq!(out, "#");
}
#[test]
fn function_returns_hash_for_zero_and_negative() {
let mut map = HashMap::new();
map.insert(0i64, "/should-not-leak".to_owned());
let _guard = install(map);
let tera = fresh_tera();
let mut t = tera.clone();
t.add_raw_template("z.html", "{{ pageurl(id=0) }}").unwrap();
t.add_raw_template("n.html", "{{ pageurl(id=-3) }}")
.unwrap();
assert_eq!(t.render("z.html", &Context::new()).unwrap(), "#");
assert_eq!(t.render("n.html", &Context::new()).unwrap(), "#");
}
#[test]
fn function_accepts_stringified_id() {
let mut map = HashMap::new();
map.insert(7i64, "/team".to_owned());
let _guard = install(map);
let tera = fresh_tera();
let mut t = tera.clone();
t.add_raw_template("t.html", r#"{{ pageurl(id="7") }}"#)
.unwrap();
assert_eq!(t.render("t.html", &Context::new()).unwrap(), "/team");
}
#[test]
fn filter_resolves_known_id() {
let mut map = HashMap::new();
map.insert(3i64, "/blog".to_owned());
let _guard = install(map);
let tera = fresh_tera();
let mut t = tera.clone();
let mut ctx = Context::new();
ctx.insert("hero_cta_page_id", &3i64);
t.add_raw_template("t.html", "{{ hero_cta_page_id | pageurl }}")
.unwrap();
assert_eq!(t.render("t.html", &ctx).unwrap(), "/blog");
}
#[test]
fn filter_returns_hash_for_null() {
let _guard = install(HashMap::new());
let tera = fresh_tera();
let mut t = tera.clone();
let mut ctx = Context::new();
ctx.insert("maybe_id", &serde_json::Value::Null);
t.add_raw_template("t.html", "{{ maybe_id | pageurl }}")
.unwrap();
assert_eq!(t.render("t.html", &ctx).unwrap(), "#");
}
#[test]
fn guard_clears_thread_local() {
{
let mut map = HashMap::new();
map.insert(1i64, "/x".to_owned());
let _g = install(map);
assert_eq!(href_from_id(Some(1)), "/x");
}
assert_eq!(href_from_id(Some(1)), "#");
}
#[test]
fn candidate_ids_picks_up_underscore_id_fields() {
let ext = json!({
"title": "Home",
"hero_cta_page_id": 4,
"related_page_id": "9",
"irrelevant": 1,
"body_stream": [{"type": "page_chooser", "value": {"page_id": 99}}],
"expire_at_id": null,
});
let mut ids = candidate_ids(&ext);
ids.sort_unstable();
assert_eq!(ids, vec![4, 9]);
}
#[test]
fn candidate_ids_ignores_non_object_extension() {
assert!(candidate_ids(&serde_json::Value::Null).is_empty());
assert!(candidate_ids(&json!([1, 2, 3])).is_empty());
assert!(candidate_ids(&json!("string")).is_empty());
}
#[test]
fn candidate_ids_skips_non_positive() {
let ext = json!({"foo_id": 0, "bar_id": -1, "baz_id": 5});
let ids = candidate_ids(&ext);
assert_eq!(ids, vec![5]);
}
fn render_expr(expr: &str, ctx: &Context) -> String {
let mut t = fresh_tera();
t.add_raw_template("t.html", expr).unwrap();
t.render("t.html", ctx).unwrap()
}
#[test]
fn page_href_filter_uses_full_url_path_not_leaf_slug() {
let _prefix = install_url_prefix("");
let mut ctx = Context::new();
ctx.insert(
"child",
&json!({ "slug": "team", "url_path": "/about/team", "title": "Team" }),
);
assert_eq!(render_expr("{{ child | page_href }}", &ctx), "/about/team");
}
#[test]
fn page_href_prepends_mount_prefix() {
let _prefix = install_url_prefix("/p");
let mut ctx = Context::new();
ctx.insert("child", &json!({ "url_path": "/about" }));
assert_eq!(render_expr("{{ child | page_href }}", &ctx), "/p/about");
}
#[test]
fn page_href_function_form_and_id_resolution() {
let mut map = HashMap::new();
map.insert(42i64, "/about".to_owned());
let _guard = install(map);
let _prefix = install_url_prefix("/p");
let mut ctx = Context::new();
ctx.insert("anc", &json!({ "url_path": "/about/team" }));
assert_eq!(
render_expr("{{ page_href(page=anc) }}", &ctx),
"/p/about/team"
);
assert_eq!(
render_expr("{{ page_href(id=42) }}", &Context::new()),
"/p/about"
);
}
#[test]
fn page_href_missing_and_empty() {
let _guard = install(HashMap::new());
let _prefix = install_url_prefix("");
assert_eq!(render_expr("{{ page_href(id=999) }}", &Context::new()), "#");
let mut ctx = Context::new();
ctx.insert("root", &json!({ "url_path": "" }));
assert_eq!(render_expr("{{ root | page_href }}", &ctx), "/");
}
#[test]
fn page_href_prefix_guard_clears() {
{
let _p = install_url_prefix("/p");
assert_eq!(current_url_prefix(), "/p");
}
assert_eq!(current_url_prefix(), "");
}
#[test]
fn site_prefix_is_stripped_before_the_mount_prefix_is_added() {
let _mount = install_url_prefix("/p");
let _site = install_site_prefix("/site-b");
let mut ctx = Context::new();
ctx.insert("child", &json!({ "url_path": "/site-b/about" }));
assert_eq!(render_expr("{{ child | page_href }}", &ctx), "/p/about");
}
#[test]
fn the_site_root_itself_links_to_the_mount_root() {
let _mount = install_url_prefix("");
let _site = install_site_prefix("/site-b");
let mut ctx = Context::new();
ctx.insert("child", &json!({ "url_path": "/site-b" }));
assert_eq!(render_expr("{{ child | page_href }}", &ctx), "/");
}
#[test]
fn a_page_outside_the_site_keeps_its_absolute_path() {
let _mount = install_url_prefix("");
let _site = install_site_prefix("/site-b");
let mut ctx = Context::new();
ctx.insert("child", &json!({ "url_path": "/other/page" }));
assert_eq!(render_expr("{{ child | page_href }}", &ctx), "/other/page");
}
#[test]
fn no_site_prefix_means_unchanged_behaviour() {
let _mount = install_url_prefix("/p");
let mut ctx = Context::new();
ctx.insert("child", &json!({ "url_path": "/about" }));
assert_eq!(render_expr("{{ child | page_href }}", &ctx), "/p/about");
}
#[test]
fn pageurl_strips_the_site_and_adds_the_mount_prefix() {
let _mount = install_url_prefix("/p");
let _site = install_site_prefix("/site-b");
let _pages = install(HashMap::from([(4i64, "/site-b/about".to_owned())]));
let ctx = Context::new();
assert_eq!(render_expr("{{ pageurl(id=4) }}", &ctx), "/p/about");
assert_eq!(render_expr("{{ 4 | pageurl }}", &ctx), "/p/about");
}
}