bitomc/templates/
block.rs1use super::*;
2
3#[derive(Boilerplate)]
4pub(crate) struct BlockHtml {
5 best_height: Height,
6 block: Block,
7 featured_inscriptions: Vec<InscriptionId>,
8 hash: BlockHash,
9 height: Height,
10 inscription_count: usize,
11 runes: Vec<SpacedRune>,
12 target: BlockHash,
13}
14
15impl BlockHtml {
16 pub(crate) fn new(
17 block: Block,
18 height: Height,
19 best_height: Height,
20 inscription_count: usize,
21 featured_inscriptions: Vec<InscriptionId>,
22 runes: Vec<SpacedRune>,
23 ) -> Self {
24 Self {
25 hash: block.header.block_hash(),
26 target: target_as_block_hash(block.header.target()),
27 block,
28 height,
29 best_height,
30 inscription_count,
31 featured_inscriptions,
32 runes,
33 }
34 }
35}
36
37impl PageContent for BlockHtml {
38 fn title(&self) -> String {
39 format!("Block {}", self.height)
40 }
41}
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46
47 #[test]
48 fn html() {
49 assert_regex_match!(
50 BlockHtml::new(
51 Chain::Mainnet.genesis_block(),
52 Height(0),
53 Height(0),
54 0,
55 Vec::new(),
56 Vec::new()
57 ),
58 "
59 <h1>Block 0</h1>
60 <dl>
61 <dt>hash</dt><dd class=monospace>[[:xdigit:]]{64}</dd>
62 <dt>target</dt><dd class=monospace>[[:xdigit:]]{64}</dd>
63 <dt>timestamp</dt><dd><time>2009-01-03 18:15:05 UTC</time></dd>
64 <dt>size</dt><dd>285</dd>
65 <dt>weight</dt><dd>1140</dd>
66 </dl>
67 .*
68 prev
69 next
70 .*
71 <h2>0 Runes</h2>
72 <h2>0 Inscriptions</h2>
73 <div class=thumbnails>
74 </div>
75 <h2>1 Transaction</h2>
76 <ul class=monospace>
77 <li><a href=/tx/[[:xdigit:]]{64}>[[:xdigit:]]{64}</a></li>
78 </ul>
79 "
80 .unindent()
81 );
82 }
83
84 #[test]
85 fn next_active_when_not_last() {
86 assert_regex_match!(
87 BlockHtml::new(
88 Chain::Mainnet.genesis_block(),
89 Height(0),
90 Height(1),
91 0,
92 Vec::new(),
93 Vec::new()
94 ),
95 r"<h1>Block 0</h1>.*prev\s*<a class=next href=/block/1>next</a>.*"
96 );
97 }
98
99 #[test]
100 fn prev_active_when_not_first() {
101 assert_regex_match!(
102 BlockHtml::new(
103 Chain::Mainnet.genesis_block(),
104 Height(1),
105 Height(1),
106 0,
107 Vec::new(),
108 Vec::new()
109 ),
110 r"<h1>Block 1</h1>.*<a class=prev href=/block/0>prev</a>\s*next.*",
111 );
112 }
113
114 #[test]
115 fn block_hash_serializes_as_hex_string() {
116 assert_eq!(
117 serde_json::to_string(&BlockHash::all_zeros()).unwrap(),
118 "\"0000000000000000000000000000000000000000000000000000000000000000\""
119 );
120 }
121}