1use crate::web::core::html::{Attr, HtmlBuf, Render};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum BadgeTone {
9 Neutral,
10 Green,
11 Purple,
12 Gold,
13}
14
15impl BadgeTone {
16 pub const fn class(self) -> &'static str {
17 match self {
18 Self::Neutral => "badge",
19 Self::Green => "badge badge-green",
20 Self::Purple => "badge badge-purple",
21 Self::Gold => "badge badge-gold",
22 }
23 }
24}
25
26pub struct Badge<'a> {
28 pub label: &'a str,
29 pub tone: BadgeTone,
30}
31
32impl<'a> Badge<'a> {
33 pub const fn new(label: &'a str, tone: BadgeTone) -> Self {
34 Self { label, tone }
35 }
36}
37
38impl Render for Badge<'_> {
39 fn render_to(&self, out: &mut HtmlBuf) {
40 out.el("span", &[Attr::Class(self.tone.class())], self.label);
41 }
42}
43
44pub struct Card<'a, A: Render, B: Render> {
46 pub title: &'a str,
47 pub actions: A,
48 pub body: B,
49 pub padded_body: bool,
50}
51
52impl<'a, A: Render, B: Render> Card<'a, A, B> {
53 pub fn new(title: &'a str, actions: A, body: B) -> Self {
54 Self {
55 title,
56 actions,
57 body,
58 padded_body: true,
59 }
60 }
61
62 pub fn flush(title: &'a str, actions: A, body: B) -> Self {
63 Self {
64 title,
65 actions,
66 body,
67 padded_body: false,
68 }
69 }
70}
71
72impl<'a, B: Render> Card<'a, (), B> {
73 pub fn simple(title: &'a str, body: B) -> Self {
74 Self::new(title, (), body)
75 }
76
77 pub fn flush_simple(title: &'a str, body: B) -> Self {
78 Self::flush(title, (), body)
79 }
80}
81
82impl<A: Render, B: Render> Render for Card<'_, A, B> {
83 fn render_to(&self, out: &mut HtmlBuf) {
84 out.el("div", &[Attr::Class("card")], |b: &mut HtmlBuf| {
85 b.el("div", &[Attr::Class("card-header")], |h: &mut HtmlBuf| {
86 h.el("strong", &[], self.title);
87 self.actions.render_to(h);
88 });
89 if self.padded_body {
90 b.el("div", &[Attr::Class("card-body")], |body: &mut HtmlBuf| {
91 self.body.render_to(body);
92 });
93 } else {
94 self.body.render_to(b);
95 }
96 });
97 }
98}
99
100pub struct Toolbar<L: Render, R: Render> {
102 pub left: L,
103 pub right: R,
104}
105
106impl<L: Render, R: Render> Render for Toolbar<L, R> {
107 fn render_to(&self, out: &mut HtmlBuf) {
108 out.el("div", &[Attr::Class("toolbar")], |b: &mut HtmlBuf| {
109 b.el("div", &[Attr::Class("toolbar-group")], |g: &mut HtmlBuf| {
110 self.left.render_to(g);
111 });
112 b.el("div", &[Attr::Class("toolbar-group")], |g: &mut HtmlBuf| {
113 self.right.render_to(g);
114 });
115 });
116 }
117}
118
119pub struct EmptyState<'a> {
121 pub message: &'a str,
122}
123
124impl Render for EmptyState<'_> {
125 fn render_to(&self, out: &mut HtmlBuf) {
126 out.el("div", &[Attr::Class("card-body muted")], self.message);
127 }
128}
129
130pub struct StatCard<'a> {
132 pub label: &'a str,
133 pub value: &'a str,
134}
135
136impl Render for StatCard<'_> {
137 fn render_to(&self, out: &mut HtmlBuf) {
138 out.el("div", &[Attr::Class("stat-card")], |b: &mut HtmlBuf| {
139 b.el("div", &[Attr::Class("muted")], self.label);
140 b.el("div", &[Attr::Class("stat-val")], self.value);
141 });
142 }
143}
144
145pub struct Alert<'a> {
147 pub title: &'a str,
148 pub detail: &'a str,
149}
150
151impl Render for Alert<'_> {
152 fn render_to(&self, out: &mut HtmlBuf) {
153 out.el(
154 "div",
155 &[Attr::Class("card"), Attr::Role("status")],
156 |b: &mut HtmlBuf| {
157 b.el("div", &[Attr::Class("card-body")], |c: &mut HtmlBuf| {
158 c.el("strong", &[], self.title);
159 if !self.detail.is_empty() {
160 c.text(" — ").text(self.detail);
161 }
162 });
163 },
164 );
165 }
166}
167
168#[derive(Debug, Clone, Copy, PartialEq, Eq)]
170pub enum FormMethod {
171 Get,
172 Post,
173}
174
175impl FormMethod {
176 pub const fn as_str(self) -> &'static str {
177 match self {
178 Self::Get => "get",
179 Self::Post => "post",
180 }
181 }
182}
183
184pub struct Form<'a, B: Render> {
188 pub method: FormMethod,
189 pub action: &'a str,
190 pub class: &'a str,
191 pub csrf_token: Option<&'a str>,
192 pub body: B,
193}
194
195impl<'a, B: Render> Form<'a, B> {
196 pub fn post<C: crate::web::core::ctx::UiCtx + ?Sized>(
197 ctx: &'a C,
198 action: &'a str,
199 class: &'a str,
200 body: B,
201 ) -> Self {
202 Self {
203 method: FormMethod::Post,
204 action,
205 class,
206 csrf_token: ctx.csrf_token(),
207 body,
208 }
209 }
210
211 pub fn get(action: &'a str, class: &'a str, body: B) -> Self {
212 Self {
213 method: FormMethod::Get,
214 action,
215 class,
216 csrf_token: None,
217 body,
218 }
219 }
220}
221
222impl<B: Render> Render for Form<'_, B> {
223 fn render_to(&self, out: &mut HtmlBuf) {
224 let mut attrs: Vec<Attr<'_>> = vec![
225 Attr::Method(self.method.as_str()),
226 Attr::Action(self.action),
227 ];
228 if !self.class.is_empty() {
229 attrs.push(Attr::Class(self.class));
230 }
231 out.el("form", &attrs, |b: &mut HtmlBuf| {
232 if self.method == FormMethod::Post {
233 if let Some(tok) = self.csrf_token {
234 b.void_el(
235 "input",
236 &[Attr::Type("hidden"), Attr::Name("_csrf"), Attr::Value(tok)],
237 );
238 }
239 }
240 self.body.render_to(b);
241 });
242 }
243}
244
245pub struct TextInput<'a> {
247 pub name: &'a str,
248 pub input_type: &'a str,
249 pub value: &'a str,
250 pub placeholder: &'a str,
251 pub extra_class: &'a str,
252 pub required: bool,
253}
254
255impl<'a> TextInput<'a> {
256 pub fn text(name: &'a str, value: &'a str, placeholder: &'a str) -> Self {
257 Self {
258 name,
259 input_type: "text",
260 value,
261 placeholder,
262 extra_class: "",
263 required: false,
264 }
265 }
266
267 pub fn password(name: &'a str, placeholder: &'a str) -> Self {
268 Self {
269 name,
270 input_type: "password",
271 value: "",
272 placeholder,
273 extra_class: "",
274 required: false,
275 }
276 }
277
278 pub fn search(name: &'a str, value: &'a str, placeholder: &'a str) -> Self {
279 Self {
280 name,
281 input_type: "search",
282 value,
283 placeholder,
284 extra_class: "",
285 required: false,
286 }
287 }
288
289 pub fn required(mut self) -> Self {
290 self.required = true;
291 self
292 }
293
294 pub fn with_class(mut self, cls: &'a str) -> Self {
295 self.extra_class = cls;
296 self
297 }
298}
299
300impl Render for TextInput<'_> {
301 fn render_to(&self, out: &mut HtmlBuf) {
302 let cls = if self.extra_class.is_empty() {
303 "input".to_string()
304 } else {
305 format!("input {}", self.extra_class)
306 };
307 let mut attrs: Vec<Attr<'_>> = vec![
308 Attr::Class(&cls),
309 Attr::Type(self.input_type),
310 Attr::Name(self.name),
311 ];
312 if !self.value.is_empty() {
313 attrs.push(Attr::Value(self.value));
314 }
315 if !self.placeholder.is_empty() {
316 attrs.push(Attr::Placeholder(self.placeholder));
317 }
318 if self.required {
319 attrs.push(Attr::Required);
320 }
321 out.void_el("input", &attrs);
322 }
323}
324
325#[cfg(test)]
326mod tests {
327 use super::*;
328 use crate::http::HttpRequest;
329 use crate::web::core::ctx::{MountPrefix, RequestCtx, SessionCtx, UserRole};
330
331 #[test]
332 fn post_form_injects_csrf_automatically_and_get_omits_it() {
333 let req = HttpRequest::new("GET", "/issues", "", Vec::new());
334 let ctx = RequestCtx::new(&req, MountPrefix::new("alpha")).with_session(Some(SessionCtx {
335 username: "alice".into(),
336 role: UserRole::Admin,
337 csrf_token: "tok_123&<>".into(),
338 }));
339
340 let post_html = Form::post(&ctx, "/alpha/issues", "u-stack", "Submit").render_to_string();
341 assert!(post_html.contains("name=\"_csrf\""));
342 assert!(post_html.contains("value=\"tok_123&<>\""));
343 assert!(!post_html.contains("style="));
344
345 let get_html = Form::get("/alpha/issues", "u-flex", "Filter").render_to_string();
346 assert!(!get_html.contains("_csrf"));
347 }
348
349 #[test]
350 fn card_escapes_title_and_contains_no_inline_styles() {
351 let html = Card::new("<b>Title</b>", Badge::new("open", BadgeTone::Green), "Body")
352 .render_to_string();
353 assert!(html.contains("<b>Title</b>"));
354 assert!(!html.contains("style="));
355 }
356}