1pub struct Fortune {
2 pub id: i32,
3 pub message: &'static str,
4}
5
6markup::define! {
7 Define<'a>(fortunes: &'a [Fortune]) {
8 @let fortunes = *fortunes;
9 {markup::doctype()}
10 html {
11 head {
12 title { "Fortunes" }
13 }
14 body {
15 table {
16 tr { th { "id" } th { "message" } }
17 @for item in fortunes {
18 tr {
19 td { @item.id }
20 td { @item.message }
21 }
22 }
23 }
24 }
25 }
26 }
27}
28
29pub fn new(fortunes: &[Fortune]) -> impl std::fmt::Display + '_ {
30 markup::new! {
31 {markup::doctype()}
32 html {
33 head {
34 title { "Fortunes" }
35 }
36 body {
37 table {
38 tr { th { "id" } th { "message" } }
39 @for item in fortunes {
40 tr {
41 td { @item.id }
42 td { @item.message }
43 }
44 }
45 }
46 }
47 }
48 }
49}
50
51pub static FORTUNES: &[Fortune] = &[
52 Fortune {
53 id: 1,
54 message: "fortune: No such file or directory",
55 },
56 Fortune {
57 id: 2,
58 message: "A computer scientist is someone who fixes things that aren\'t broken.",
59 },
60 Fortune {
61 id: 3,
62 message: "After enough decimal places, nobody gives a damn.",
63 },
64 Fortune {
65 id: 4,
66 message: "A bad random number generator: 1, 1, 1, 1, 1, 4.33e+67, 1, 1, 1",
67 },
68 Fortune {
69 id: 5,
70 message: "A computer program does what you tell it to do, not what you want it to do.",
71 },
72 Fortune {
73 id: 6,
74 message: "Emacs is a nice operating system, but I prefer UNIX. — Tom Christaensen",
75 },
76 Fortune {
77 id: 7,
78 message: "Any program that runs right is obsolete.",
79 },
80 Fortune {
81 id: 8,
82 message: "A list is only as strong as its weakest link. — Donald Knuth",
83 },
84 Fortune {
85 id: 9,
86 message: "Feature: A bug with seniority.",
87 },
88 Fortune {
89 id: 10,
90 message: "Computers make very fast, very accurate mistakes.",
91 },
92 Fortune {
93 id: 11,
94 message:
95 "<script>alert(\"This should not be displayed in a browser alert box.\");</script>",
96 },
97 Fortune {
98 id: 12,
99 message: "フレームワークのベンチマーク",
100 },
101];
102
103#[allow(dead_code)]
104pub fn main() {
105 println!("{}", Define { fortunes: FORTUNES });
106}
107
108#[test]
109fn t() {
110 let define = Define { fortunes: FORTUNES }.to_string();
111 let new = new(FORTUNES).to_string();
112 assert_eq!(define.len(), 1153);
113 assert_eq!(define, new);
114}