use crate::head::HeadContext;
#[derive(Debug, Clone)]
pub struct Outlet {
pub content: String,
}
impl Outlet {
pub fn new(content: impl Into<String>) -> Self {
Self {
content: content.into(),
}
}
pub fn empty() -> Self {
Self {
content: String::new(),
}
}
}
type LayoutRenderFn = dyn Fn(&Outlet, &HeadContext) -> String + Send + Sync;
pub struct Layout {
pub name: String,
render_fn: Box<LayoutRenderFn>,
}
impl Layout {
pub fn new<F>(name: impl Into<String>, render_fn: F) -> Self
where
F: Fn(&Outlet, &HeadContext) -> String + Send + Sync + 'static,
{
Self {
name: name.into(),
render_fn: Box::new(render_fn),
}
}
pub fn render(&self, outlet: &Outlet, head: &HeadContext) -> String {
(self.render_fn)(outlet, head)
}
}
impl std::fmt::Debug for Layout {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Layout").field("name", &self.name).finish()
}
}
#[derive(Debug)]
pub struct LayoutRoute {
pub path_prefix: String,
pub layout: Layout,
pub children: Vec<LayoutRoute>,
}
impl LayoutRoute {
pub fn new(path_prefix: impl Into<String>, layout: Layout) -> Self {
Self {
path_prefix: path_prefix.into(),
layout,
children: Vec::new(),
}
}
pub fn child(mut self, route: LayoutRoute) -> Self {
self.children.push(route);
self
}
}
#[derive(Debug)]
pub struct LayoutTree {
root: LayoutRoute,
}
impl LayoutTree {
pub fn new(root_layout: Layout) -> Self {
Self {
root: LayoutRoute::new("/", root_layout),
}
}
pub fn nest(mut self, path_prefix: impl Into<String>, layout: Layout) -> Self {
self.root
.children
.push(LayoutRoute::new(path_prefix, layout));
self
}
pub fn nest_route(mut self, route: LayoutRoute) -> Self {
self.root.children.push(route);
self
}
pub fn resolve(&self, path: &str) -> Vec<&Layout> {
let mut chain = vec![&self.root.layout];
Self::resolve_recursive(&self.root.children, path, &mut chain);
chain
}
fn resolve_recursive<'a>(children: &'a [LayoutRoute], path: &str, chain: &mut Vec<&'a Layout>) {
for child in children {
if path.starts_with(&child.path_prefix) {
chain.push(&child.layout);
Self::resolve_recursive(&child.children, path, chain);
return; }
}
}
pub fn render(&self, path: &str, page_content: String, head: &HeadContext) -> String {
let chain = self.resolve(path);
let mut content = page_content;
for layout in chain.iter().rev() {
let outlet = Outlet::new(content);
content = layout.render(&outlet, head);
}
content
}
}
#[cfg(test)]
mod tests {
use super::*;
fn make_root_layout() -> Layout {
Layout::new("root", |outlet, head| {
format!(
"<!DOCTYPE html><html><head>{}</head><body><nav>Nav</nav><main>{}</main></body></html>",
head.render_tags(),
outlet.content
)
})
}
fn make_blog_layout() -> Layout {
Layout::new("blog", |outlet, _head| {
format!(
"<div class=\"blog\"><aside>Blog Sidebar</aside><article>{}</article></div>",
outlet.content
)
})
}
fn make_admin_layout() -> Layout {
Layout::new("admin", |outlet, _head| {
format!(
"<div class=\"admin\"><nav>Admin Nav</nav><section>{}</section></div>",
outlet.content
)
})
}
#[test]
fn layout_tree_root_only() {
let tree = LayoutTree::new(make_root_layout());
let head = HeadContext::new().title("Home");
let result = tree.render("/", "<h1>Welcome</h1>".to_string(), &head);
assert!(result.contains("<nav>Nav</nav>"));
assert!(result.contains("<h1>Welcome</h1>"));
assert!(result.contains("<title>Home</title>"));
}
#[test]
fn layout_tree_nested_blog() {
let tree = LayoutTree::new(make_root_layout()).nest("/blog", make_blog_layout());
let head = HeadContext::new().title("Blog Post");
let result = tree.render("/blog/my-post", "<p>Post content</p>".to_string(), &head);
assert!(result.contains("<nav>Nav</nav>"));
assert!(result.contains("Blog Sidebar"));
assert!(result.contains("<p>Post content</p>"));
}
#[test]
fn layout_chain_resolution() {
let tree = LayoutTree::new(make_root_layout())
.nest("/blog", make_blog_layout())
.nest("/admin", make_admin_layout());
let blog_chain = tree.resolve("/blog/post-1");
assert_eq!(blog_chain.len(), 2);
assert_eq!(blog_chain[0].name, "root");
assert_eq!(blog_chain[1].name, "blog");
let admin_chain = tree.resolve("/admin/users");
assert_eq!(admin_chain.len(), 2);
assert_eq!(admin_chain[0].name, "root");
assert_eq!(admin_chain[1].name, "admin");
let root_chain = tree.resolve("/about");
assert_eq!(root_chain.len(), 1);
assert_eq!(root_chain[0].name, "root");
}
#[test]
fn outlet_empty() {
let outlet = Outlet::empty();
assert!(outlet.content.is_empty());
}
#[test]
fn deeply_nested_layouts() {
let tree = LayoutTree::new(make_root_layout()).nest_route(
LayoutRoute::new("/admin", make_admin_layout()).child(LayoutRoute::new(
"/admin/settings",
Layout::new("settings", |outlet, _| {
format!("<div class=\"settings\">{}</div>", outlet.content)
}),
)),
);
let chain = tree.resolve("/admin/settings/profile");
assert_eq!(chain.len(), 3);
assert_eq!(chain[0].name, "root");
assert_eq!(chain[1].name, "admin");
assert_eq!(chain[2].name, "settings");
}
#[test]
fn preserves_parent_shell_across_child_transitions() {
let tree = LayoutTree::new(make_root_layout())
.nest("/blog", make_blog_layout())
.nest("/admin", make_admin_layout());
let head = HeadContext::new().title("Transitions");
let blog_html = tree.render("/blog/post-1", "<p>Blog</p>".to_string(), &head);
let admin_html = tree.render("/admin/users", "<p>Admin</p>".to_string(), &head);
assert!(blog_html.contains("<nav>Nav</nav>"));
assert!(admin_html.contains("<nav>Nav</nav>"));
assert_eq!(blog_html.matches("<nav>Nav</nav>").count(), 1);
assert_eq!(admin_html.matches("<nav>Nav</nav>").count(), 1);
}
#[test]
fn applies_layouts_from_leaf_to_root_order() {
let root = Layout::new("root", |outlet, _| format!("[ROOT:{}]", outlet.content));
let child = Layout::new("child", |outlet, _| format!("[CHILD:{}]", outlet.content));
let tree = LayoutTree::new(root).nest("/child", child);
let html = tree.render("/child/page", "PAGE".to_string(), &HeadContext::new());
assert_eq!(html, "[ROOT:[CHILD:PAGE]]");
}
}