use std::cell::RefCell;
use std::collections::HashMap;
use std::sync::Arc;
use rustango::core::Column as _;
use rustango::sql::FetcherPool as _;
use serde::{Deserialize, Serialize};
use tera::{Function, Tera, Value};
use crate::page::Page;
pub const DEFAULT_DEPTH: i64 = 1;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MenuRow {
pub id: i64,
pub parent_id: Option<i64>,
pub title: String,
pub url_path: String,
pub sort_order: i32,
#[serde(default)]
pub path: String,
#[serde(default)]
pub page_type_id: i64,
}
impl MenuRow {
fn from_page(p: Page) -> Option<Self> {
let id = p.id.get().copied()?;
Some(Self {
id,
parent_id: p.parent_id,
title: p.title,
url_path: p.url_path,
sort_order: p.sort_order,
path: p.path,
page_type_id: p.page_type_id,
})
}
}
thread_local! {
static CURRENT_MENU_ROWS: RefCell<Option<Arc<Vec<MenuRow>>>>
= const { RefCell::new(None) };
}
#[must_use = "the guard clears the thread-local on drop; bind it to a local"]
pub struct AutoMenuGuard {
_priv: (),
}
impl Drop for AutoMenuGuard {
fn drop(&mut self) {
CURRENT_MENU_ROWS.with(|cell| {
*cell.borrow_mut() = None;
});
}
}
pub fn install(rows: Vec<MenuRow>) -> AutoMenuGuard {
CURRENT_MENU_ROWS.with(|cell| {
*cell.borrow_mut() = Some(Arc::new(rows));
});
AutoMenuGuard { _priv: () }
}
pub async fn prefetch(pool: &rustango::sql::Pool) -> Vec<MenuRow> {
let error_tid = crate::error_pages::error_page_type_id(pool).await;
let now = chrono::Utc::now();
let rows = Page::objects()
.where_(Page::show_in_menus.eq(true))
.where_(Page::status.is_in(crate::resolver::served_statuses()))
.order_by(&[("sort_order", false), ("id", false)])
.fetch(pool)
.await;
match rows {
Ok(rows) => rows
.into_iter()
.filter(|p| error_tid.map_or(true, |tid| p.page_type_id != tid))
.filter(|p| crate::resolver::visible_now(p, now))
.filter_map(MenuRow::from_page)
.collect(),
Err(e) => {
tracing::warn!(
target: "rustango_cms::auto_menu",
error = %e,
"auto_menu prefetch failed; templates will see an empty list",
);
Vec::new()
}
}
}
pub async fn prefetch_cached(
pool: &rustango::sql::Pool,
cache: &rustango::cache::BoxedCache,
key: &str,
ttl: Option<std::time::Duration>,
) -> Vec<MenuRow> {
let json = rustango::cache_fragment::cached_render(cache.as_ref(), key, ttl, || async {
serde_json::to_string(&prefetch(pool).await).unwrap_or_else(|_| "[]".to_owned())
})
.await;
serde_json::from_str(&json).unwrap_or_default()
}
pub fn register_tera_function(tera: &mut Tera) {
tera.register_function("auto_menu", AutoMenuFn);
}
struct AutoMenuFn;
impl Function for AutoMenuFn {
fn call(&self, args: &HashMap<String, Value>) -> tera::Result<Value> {
let pool = CURRENT_MENU_ROWS.with(|cell| cell.borrow().clone());
let Some(pool) = pool else {
return Ok(Value::Array(Vec::new()));
};
let parent: Option<i64> = args.get("parent_id").and_then(|v| {
if v.is_null() {
return None;
}
v.as_i64().filter(|n| *n > 0)
});
let depth: i64 = args
.get("depth")
.and_then(Value::as_i64)
.filter(|n| *n >= 1)
.unwrap_or(DEFAULT_DEPTH);
let by_parent = index_by_parent(&pool);
let items = build_items(&by_parent, parent, depth);
serde_json::to_value(&items)
.map_err(|e| tera::Error::msg(format!("auto_menu: serializing items failed: {e}")))
}
}
#[derive(Debug, Serialize)]
pub struct MenuItem {
pub page_id: i64,
pub title: String,
pub url_path: String,
pub children: Vec<MenuItem>,
}
fn index_by_parent(rows: &[MenuRow]) -> HashMap<Option<i64>, Vec<&MenuRow>> {
crate::tree_build::index_by_parent(rows, |r| r.parent_id)
}
fn build_items(
by_parent: &HashMap<Option<i64>, Vec<&MenuRow>>,
parent: Option<i64>,
depth: i64,
) -> Vec<MenuItem> {
crate::tree_build::build(
by_parent,
parent,
depth,
&|r: &MenuRow| r.id,
&|r: &MenuRow, children| MenuItem {
page_id: r.id,
title: r.title.clone(),
url_path: r.url_path.clone(),
children,
},
)
}
#[cfg(test)]
mod tests {
use super::*;
use tera::{Context, Tera};
fn fresh_tera() -> Tera {
let mut tera = Tera::default();
register_tera_function(&mut tera);
tera
}
fn render(tera: &Tera, src: &str) -> String {
let mut t = tera.clone();
t.add_raw_template("t.html", src).unwrap();
t.render("t.html", &Context::new()).unwrap()
}
fn row(id: i64, parent: Option<i64>, title: &str, slug: &str, sort: i32) -> MenuRow {
MenuRow {
id,
parent_id: parent,
title: title.to_owned(),
url_path: format!("/{slug}"),
sort_order: sort,
path: String::new(),
page_type_id: 0,
}
}
fn fixture() -> Vec<MenuRow> {
vec![
row(1, None, "Home", "", 0),
row(2, None, "About", "about", 1),
row(3, Some(2), "Team", "about/team", 0),
row(4, Some(2), "Careers", "about/careers", 1),
row(5, Some(4), "Engineering", "about/careers/eng", 0),
]
}
#[test]
fn returns_empty_when_nothing_installed() {
let tera = fresh_tera();
let out = render(&tera, "{{ auto_menu() | length }}");
assert_eq!(out, "0");
}
#[test]
fn menu_rows_json_round_trip_is_lossless() {
let rows = fixture();
let json = serde_json::to_string(&rows).expect("serialize");
let back: Vec<MenuRow> = serde_json::from_str(&json).expect("deserialize");
assert_eq!(serde_json::to_string(&back).unwrap(), json);
assert_eq!(back.len(), 5);
assert_eq!(back[0].parent_id, None);
assert_eq!(back[4].parent_id, Some(4));
assert_eq!(back[3].title, "Careers");
assert_eq!(back[2].url_path, "/about/team");
}
#[test]
fn root_menu_returns_top_level_items() {
let _g = install(fixture());
let tera = fresh_tera();
let out = render(
&tera,
"{% for it in auto_menu() %}{{ it.title }} {% endfor %}",
);
assert_eq!(out, "Home About ");
}
#[test]
fn explicit_parent_id_returns_children_of_that_page() {
let _g = install(fixture());
let tera = fresh_tera();
let out = render(
&tera,
"{% for it in auto_menu(parent_id=2) %}{{ it.title }} {% endfor %}",
);
assert_eq!(out, "Team Careers ");
}
#[test]
fn depth_one_emits_empty_children_arrays() {
let _g = install(fixture());
let tera = fresh_tera();
let out = render(
&tera,
"{% for it in auto_menu(parent_id=2, depth=1) %}\
{{ it.title }}={{ it.children | length }} {% endfor %}",
);
assert_eq!(out, "Team=0 Careers=0 ");
}
#[test]
fn depth_two_expands_one_level_of_nesting() {
let _g = install(fixture());
let tera = fresh_tera();
let src = r#"
{%- for it in auto_menu(parent_id=2, depth=2) -%}
{{ it.title }}:{% for c in it.children %}{{ c.title }},{% endfor %};
{%- endfor -%}
"#;
let out = render(&tera, src);
assert_eq!(out, "Team:;Careers:Engineering,;");
}
#[test]
fn depth_three_reaches_grandchildren() {
let _g = install(fixture());
let tera = fresh_tera();
let src = r#"
{%- for it in auto_menu(depth=3) -%}
{{ it.title }}({% for c in it.children %}{{ c.title }}[{% for g in c.children %}{{ g.title }},{% endfor %}],{% endfor %});
{%- endfor -%}
"#;
let out = render(&tera, src);
assert_eq!(out, "Home();About(Team[],Careers[Engineering,],);");
}
#[test]
fn parent_id_zero_treated_as_root() {
let _g = install(fixture());
let tera = fresh_tera();
let out = render(
&tera,
"{% for it in auto_menu(parent_id=0) %}{{ it.title }} {% endfor %}",
);
assert_eq!(out, "Home About ");
}
#[test]
fn parent_id_unknown_returns_empty() {
let _g = install(fixture());
let tera = fresh_tera();
let out = render(&tera, "{{ auto_menu(parent_id=999) | length }}");
assert_eq!(out, "0");
}
#[test]
fn items_are_ordered_by_input_order() {
let rows = vec![
row(10, None, "Z-late", "z", 5),
row(11, None, "A-early", "a", 0),
row(12, None, "M-mid", "m", 2),
];
let _g = install(rows);
let tera = fresh_tera();
let out = render(
&tera,
"{% for it in auto_menu() %}{{ it.title }} {% endfor %}",
);
assert_eq!(out, "Z-late A-early M-mid ");
}
#[test]
fn json_shape_matches_issue_spec() {
let _g = install(vec![row(7, None, "Solo", "solo", 0)]);
let tera = fresh_tera();
let out = render(
&tera,
"{% set m = auto_menu() %}{{ m[0].page_id }}|{{ m[0].title }}|{{ m[0].url_path }}|{{ m[0].children | length }}",
);
assert_eq!(out, "7|Solo|/solo|0");
}
#[test]
fn guard_clears_thread_local() {
{
let _g = install(fixture());
let tera = fresh_tera();
assert_eq!(render(&tera, "{{ auto_menu() | length }}"), "2");
}
let tera = fresh_tera();
assert_eq!(render(&tera, "{{ auto_menu() | length }}"), "0");
}
#[test]
fn negative_or_zero_depth_normalizes_to_default() {
let _g = install(fixture());
let tera = fresh_tera();
let out0 = render(
&tera,
"{% for it in auto_menu(parent_id=2, depth=0) %}{{ it.title }} {% endfor %}",
);
let out1 = render(
&tera,
"{% for it in auto_menu(parent_id=2, depth=1) %}{{ it.title }} {% endfor %}",
);
assert_eq!(out0, out1);
}
}