use crate::web::core::html::{Attr, HtmlBuf, Render};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BadgeTone {
Neutral,
Green,
Purple,
Gold,
}
impl BadgeTone {
pub const fn class(self) -> &'static str {
match self {
Self::Neutral => "badge",
Self::Green => "badge badge-green",
Self::Purple => "badge badge-purple",
Self::Gold => "badge badge-gold",
}
}
}
pub struct Badge<'a> {
pub label: &'a str,
pub tone: BadgeTone,
}
impl<'a> Badge<'a> {
pub const fn new(label: &'a str, tone: BadgeTone) -> Self {
Self { label, tone }
}
}
impl Render for Badge<'_> {
fn render_to(&self, out: &mut HtmlBuf) {
out.el("span", &[Attr::Class(self.tone.class())], self.label);
}
}
pub struct Card<'a, A: Render, B: Render> {
pub title: &'a str,
pub actions: A,
pub body: B,
pub padded_body: bool,
}
impl<'a, A: Render, B: Render> Card<'a, A, B> {
pub fn new(title: &'a str, actions: A, body: B) -> Self {
Self {
title,
actions,
body,
padded_body: true,
}
}
pub fn flush(title: &'a str, actions: A, body: B) -> Self {
Self {
title,
actions,
body,
padded_body: false,
}
}
}
impl<'a, B: Render> Card<'a, (), B> {
pub fn simple(title: &'a str, body: B) -> Self {
Self::new(title, (), body)
}
pub fn flush_simple(title: &'a str, body: B) -> Self {
Self::flush(title, (), body)
}
}
impl<A: Render, B: Render> Render for Card<'_, A, B> {
fn render_to(&self, out: &mut HtmlBuf) {
out.el("div", &[Attr::Class("card")], |b: &mut HtmlBuf| {
b.el("div", &[Attr::Class("card-header")], |h: &mut HtmlBuf| {
h.el("strong", &[], self.title);
self.actions.render_to(h);
});
if self.padded_body {
b.el("div", &[Attr::Class("card-body")], |body: &mut HtmlBuf| {
self.body.render_to(body);
});
} else {
self.body.render_to(b);
}
});
}
}
pub struct Toolbar<L: Render, R: Render> {
pub left: L,
pub right: R,
}
impl<L: Render, R: Render> Render for Toolbar<L, R> {
fn render_to(&self, out: &mut HtmlBuf) {
out.el("div", &[Attr::Class("toolbar")], |b: &mut HtmlBuf| {
b.el("div", &[Attr::Class("toolbar-group")], |g: &mut HtmlBuf| {
self.left.render_to(g);
});
b.el("div", &[Attr::Class("toolbar-group")], |g: &mut HtmlBuf| {
self.right.render_to(g);
});
});
}
}
pub struct EmptyState<'a> {
pub message: &'a str,
}
impl Render for EmptyState<'_> {
fn render_to(&self, out: &mut HtmlBuf) {
out.el("div", &[Attr::Class("card-body muted")], self.message);
}
}
pub struct StatCard<'a> {
pub label: &'a str,
pub value: &'a str,
}
impl Render for StatCard<'_> {
fn render_to(&self, out: &mut HtmlBuf) {
out.el("div", &[Attr::Class("stat-card")], |b: &mut HtmlBuf| {
b.el("div", &[Attr::Class("muted")], self.label);
b.el("div", &[Attr::Class("stat-val")], self.value);
});
}
}
pub struct Alert<'a> {
pub title: &'a str,
pub detail: &'a str,
}
impl Render for Alert<'_> {
fn render_to(&self, out: &mut HtmlBuf) {
out.el(
"div",
&[Attr::Class("card"), Attr::Role("status")],
|b: &mut HtmlBuf| {
b.el("div", &[Attr::Class("card-body")], |c: &mut HtmlBuf| {
c.el("strong", &[], self.title);
if !self.detail.is_empty() {
c.text(" — ").text(self.detail);
}
});
},
);
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FormMethod {
Get,
Post,
}
impl FormMethod {
pub const fn as_str(self) -> &'static str {
match self {
Self::Get => "get",
Self::Post => "post",
}
}
}
pub struct Form<'a, B: Render> {
pub method: FormMethod,
pub action: &'a str,
pub class: &'a str,
pub csrf_token: Option<&'a str>,
pub body: B,
}
impl<'a, B: Render> Form<'a, B> {
pub fn post<C: crate::web::core::ctx::UiCtx + ?Sized>(
ctx: &'a C,
action: &'a str,
class: &'a str,
body: B,
) -> Self {
Self {
method: FormMethod::Post,
action,
class,
csrf_token: ctx.csrf_token(),
body,
}
}
pub fn get(action: &'a str, class: &'a str, body: B) -> Self {
Self {
method: FormMethod::Get,
action,
class,
csrf_token: None,
body,
}
}
}
impl<B: Render> Render for Form<'_, B> {
fn render_to(&self, out: &mut HtmlBuf) {
let mut attrs: Vec<Attr<'_>> = vec![
Attr::Method(self.method.as_str()),
Attr::Action(self.action),
];
if !self.class.is_empty() {
attrs.push(Attr::Class(self.class));
}
out.el("form", &attrs, |b: &mut HtmlBuf| {
if self.method == FormMethod::Post {
if let Some(tok) = self.csrf_token {
b.void_el(
"input",
&[Attr::Type("hidden"), Attr::Name("_csrf"), Attr::Value(tok)],
);
}
}
self.body.render_to(b);
});
}
}
pub struct TextInput<'a> {
pub name: &'a str,
pub input_type: &'a str,
pub value: &'a str,
pub placeholder: &'a str,
pub extra_class: &'a str,
pub required: bool,
}
impl<'a> TextInput<'a> {
pub fn text(name: &'a str, value: &'a str, placeholder: &'a str) -> Self {
Self {
name,
input_type: "text",
value,
placeholder,
extra_class: "",
required: false,
}
}
pub fn password(name: &'a str, placeholder: &'a str) -> Self {
Self {
name,
input_type: "password",
value: "",
placeholder,
extra_class: "",
required: false,
}
}
pub fn search(name: &'a str, value: &'a str, placeholder: &'a str) -> Self {
Self {
name,
input_type: "search",
value,
placeholder,
extra_class: "",
required: false,
}
}
pub fn required(mut self) -> Self {
self.required = true;
self
}
pub fn with_class(mut self, cls: &'a str) -> Self {
self.extra_class = cls;
self
}
}
impl Render for TextInput<'_> {
fn render_to(&self, out: &mut HtmlBuf) {
let cls = if self.extra_class.is_empty() {
"input".to_string()
} else {
format!("input {}", self.extra_class)
};
let mut attrs: Vec<Attr<'_>> = vec![
Attr::Class(&cls),
Attr::Type(self.input_type),
Attr::Name(self.name),
];
if !self.value.is_empty() {
attrs.push(Attr::Value(self.value));
}
if !self.placeholder.is_empty() {
attrs.push(Attr::Placeholder(self.placeholder));
}
if self.required {
attrs.push(Attr::Required);
}
out.void_el("input", &attrs);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::http::HttpRequest;
use crate::web::core::ctx::{MountPrefix, RequestCtx, SessionCtx, UserRole};
#[test]
fn post_form_injects_csrf_automatically_and_get_omits_it() {
let req = HttpRequest::new("GET", "/issues", "", Vec::new());
let ctx = RequestCtx::new(&req, MountPrefix::new("alpha")).with_session(Some(SessionCtx {
username: "alice".into(),
role: UserRole::Admin,
csrf_token: "tok_123&<>".into(),
}));
let post_html = Form::post(&ctx, "/alpha/issues", "u-stack", "Submit").render_to_string();
assert!(post_html.contains("name=\"_csrf\""));
assert!(post_html.contains("value=\"tok_123&<>\""));
assert!(!post_html.contains("style="));
let get_html = Form::get("/alpha/issues", "u-flex", "Filter").render_to_string();
assert!(!get_html.contains("_csrf"));
}
#[test]
fn card_escapes_title_and_contains_no_inline_styles() {
let html = Card::new("<b>Title</b>", Badge::new("open", BadgeTone::Green), "Body")
.render_to_string();
assert!(html.contains("<b>Title</b>"));
assert!(!html.contains("style="));
}
}