1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use super::{Block, Tag};
use crate::encoding::Encoder;
use crate::Content;
use crate::traits::{Combine, ContentSequence};
use std::ops::Range;
#[derive(Clone, Copy)]
pub struct Section<'section, Contents: ContentSequence> {
blocks: &'section [Block<'section>],
contents: Contents,
}
type Next<C, X> = (<C as Combine>::I, <C as Combine>::J, <C as Combine>::K, X);
impl<'section> Section<'section, ()> {
#[inline]
pub(crate) fn new(blocks: &'section [Block<'section>]) -> Self {
Self {
blocks,
contents: (),
}
}
}
impl<'section, C> Section<'section, C>
where
C: ContentSequence,
{
#[inline]
fn slice(self, range: Range<usize>) -> Self {
Self {
blocks: &self.blocks[range],
contents: self.contents,
}
}
#[inline]
pub fn with<X>(self, content: &X) -> Section<'section, Next<C, &X>>
where
X: Content + ?Sized,
{
Section {
blocks: self.blocks,
contents: self.contents.combine(content),
}
}
#[inline]
pub fn without_last(self) -> Section<'section, C::Previous>
{
Section {
blocks: self.blocks,
contents: self.contents.crawl_back(),
}
}
pub fn render<E>(&self, encoder: &mut E) -> Result<(), E::Error>
where
E: Encoder,
{
let mut index = 0;
while let Some(block) = self.blocks.get(index) {
index += 1;
encoder.write_unescaped(block.html)?;
match block.tag {
Tag::Escaped => {
self.contents.render_field_escaped(block.hash, block.name, encoder)?;
}
Tag::Unescaped => {
self.contents.render_field_unescaped(block.hash, block.name, encoder)?;
}
Tag::Section => {
self.contents.render_field_section(
block.hash,
block.name,
self.slice(index..index + block.children as usize),
encoder,
)?;
index += block.children as usize;
}
Tag::Inverse => {
self.contents.render_field_inverse(
block.hash,
block.name,
self.slice(index..index + block.children as usize),
encoder,
)?;
index += block.children as usize;
}
_ => {}
}
}
Ok(())
}
}