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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use crate::template::Template;
pub trait EvaluableMixedContent<T>: IntoIterator {}
#[derive(Debug, Eq, PartialEq)]
pub enum TemplateSlice<'a> {
Text {
value: &'a str,
start_position: usize,
end_position: usize,
},
Code {
value: &'a str,
start_position: usize,
end_position: usize,
},
}
pub struct EvaluableMixedContentIterator<'a, T> {
source: &'a T,
current_position: usize,
}
impl<'a> EvaluableMixedContent<&'a Template> for &'a Template {}
impl<'a> IntoIterator for &'a Template {
type Item = TemplateSlice<'a>;
type IntoIter = EvaluableMixedContentIterator<'a, Template>;
fn into_iter(self) -> Self::IntoIter {
EvaluableMixedContentIterator {
source: &self,
current_position: 0,
}
}
}
pub(crate) const START_PATTERN: &str = "{{";
pub(crate) const END_PATTERN: &str = "}}";
impl<'a> Iterator for EvaluableMixedContentIterator<'a, Template> {
type Item = TemplateSlice<'a>;
fn next(&mut self) -> Option<Self::Item> {
let i = self.current_position;
let raw_content = self.source.raw_content.as_str();
let source_length = raw_content.len();
let start_position = raw_content[i..].find(START_PATTERN);
if start_position.is_none() && i < source_length {
self.current_position = source_length;
return Some(TemplateSlice::Text {
value: &raw_content[i..],
start_position: i,
end_position: source_length,
});
}
let start_position = start_position? + i;
if i < start_position {
self.current_position = start_position;
return Some(TemplateSlice::Text {
value: &raw_content[i..start_position],
start_position: i,
end_position: start_position,
});
}
let end_offset = raw_content[start_position..].find(END_PATTERN)?;
let end_position = start_position + end_offset + END_PATTERN.len();
self.current_position = end_position;
Some(TemplateSlice::Code {
value: &raw_content[start_position..end_position],
start_position,
end_position,
})
}
}
#[cfg(test)]
mod tests {
use crate::template::Template;
use crate::template::content::TemplateSlice;
use std::path::PathBuf;
#[test]
fn should_find_all_evaluation_spots() {
let path = PathBuf::from("test-assets/simple-template");
let template = Template::read_from(&path).unwrap();
let all_evaluation_spots: Vec<TemplateSlice> = (&template).into_iter().collect();
let expected = vec![
TemplateSlice::Text {
value: "Some template ",
start_position: 0,
end_position: 14,
},
TemplateSlice::Code {
value: "{{ variable }}",
start_position: 14,
end_position: 28,
},
TemplateSlice::Text {
value: " - or something",
start_position: 28,
end_position: 43,
},
];
assert_eq!(all_evaluation_spots, expected);
}
}