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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
use std::collections::HashMap;
use deepcrow::*;
/*
#[test]
fn lexing() {
let data = r#"Leading comment: [|view "basic" something="yeup"|>Hello there <|view|] Trailing text"#;
let ll = Lexer::new( &data );
for sym in ll {
if let SymbolType::Error{what} = sym.symbol() {
println!("Error {}:{}: {:?}", sym.start().line, sym.start().column, what );
break;
} else {
println!("Symbol: {:?}", sym );
}
}
}
*/
#[test]
fn parsing() {
let mut ct = DeepCrow::new();
if let Err(e) = ct.load_static(r#"Here is a sample
[|view "index" theme="standard"|><html><body>Hi</body></html><|view|]
That was fun! now a placeholder: [|view "placeholder"|] and now one with an implicit [|view "simple"|>Body -> [|show content|] <- and end<|view|] aoeu
[|view "person" layout="simple"|>Welcome [|show person.last_name|], [|show person.first_name|]! It's lovely to see you again.<|view|]
Now with explicit outputs:
[|view "complex"|>
[|output "content"|>
Here's a [|show name|].
<|output|]
[|output "sidebar"|>
What's up world?
<|output|]
[|output "footer"|>
Same, I guess!
<|output|]
<|view|]"#) {
println!("Error: {:?}", e );
} else {
println!("It finished");
if let Ok(v) = ct.render(
"person",
&Context::from([
("hi".to_string(),Value::String("hi".to_string())),
("person".to_string(),Value::Dictionary(HashMap::from([
("first_name".to_string(), Value::String("Bob".to_string())),
("last_name".to_string(), Value::String("Smith".to_string())),
]))),
])
) {
println!("View result: {:?}", v );
} else {
println!("Error?");
}
}
}
#[test]
fn loops() -> Result<()> {
let mut ct = DeepCrow::new();
ct.load_static(r#"Looping code:
[|view "index"|>We will enumerate people here:[|loop people|>
- [|show last_name|], [|show first_name|][|if show_title |>: [|show title|] <|else|> **title redacted** <|if|] ::>[|loop tags|> [|show tag|]<|loop|] <:: <|loop|]
<|view|]
"#)?;
let ctx = Context::from([
("people".to_string(), vec![
[
("first_name".into(), "Joe".into()),
("last_name".into(), "Smith".into()),
("show_title".into(), true.into()),
("title".into(), "CEO".into()),
("tags".into(), vec![
Context::from([("tag".into(), "jerk".into()),]).into(),
Context::from([("tag".into(), "ugly".into()),]).into(),
].into()),
].into(),
[
("first_name".into(), "Chris".into()),
("last_name".into(), "Perkens".into()),
("show_title".into(), false.into()),
("title".into(), "Baconeer".into()),
("tags".into(), vec![
Context::from([("tag".into(), "jerk".into()),]).into(),
Context::from([("tag".into(), "ugly".into()),]).into(),
].into()),
].into(),
[
("first_name".into(), "Will".into()),
("last_name".into(), "Power".into()),
("show_title".into(), false.into()),
("title".into(), "CFO".into()),
("tags".into(), vec![
Context::from([("tag".into(), "jerk".into()),]).into(),
Context::from([("tag".into(), "ugly".into()),]).into(),
].into()),
].into(),
[
("first_name".into(), "Justin".into()),
("last_name".into(), "Time".into()),
("show_title".into(), true.into()),
("title".into(), "CIO".into()),
("tags".into(), vec![
Context::from([("tag".into(), "jerk".into()),]).into(),
Context::from([("tag".into(), "ugly".into()),]).into(),
].into()),
].into(),
].into()),
]);
println!("View result: {}", ct.render("index", &ctx)? );
Ok(())
}
#[test]
fn conditional() -> Result<()> {
let mut ct = DeepCrow::new();
ct.load_static(r#"Hi there
[|view "index"|>
Color: [|if is_red|> red <|elif is_blue |> blue <|else|> green <|if|]
<|view|]"#)?;
let ctx = Context::from([
("is_red".into(), false.into()),
("is_blue".into(), false.into()),
("color".into(), "purple".into()),
]);
println!("View result: {}", ct.render("index", &ctx)? );
Ok(())
}
struct Item {
pub id: i32,
pub title: String,
pub body: String,
}
struct Person {
pub id: i32,
pub username: String,
pub name: String,
}
struct Page {
pub user: Person,
pub total_items: i32,
pub total_pages: i32,
pub cur_page: i32,
pub items: Vec<Item>,
}
impl Page {
fn new() -> Page {
Page {
user: Person {
id: 443,
username: "eichlan".into(),
name: "Mike".into(),
},
total_items: 4000,
total_pages: 400,
cur_page: 35,
items: vec![
Item{
id: 123,
title: "hi".into(),
body: "body".into(),
},
Item{
id: 124,
title: "bye".into(),
body: "wooo".into(),
},
Item{
id: 125,
title: "ciao".into(),
body: "whatever".into(),
}
],
}
}
}
impl<'a> MappedStructure<'a> for Page {
fn get_value(&'a self, id: &str) -> Option<MappedValue<'a>> {
match id {
"user" => Some(MappedValue::Struct(&self.user)),
"total_items" => Some(MappedValue::Int32(self.total_items)),
"total_pages" => Some(MappedValue::Int32(self.total_pages)),
"cur_page" => Some(MappedValue::Int32(self.cur_page)),
"items" => Some(MappedValue::<'a>::List(&self.items)),
_ => None,
}
}
}
impl<'a> MappedStructure<'a> for Person {
fn get_value(&'a self, id: &str) -> Option<MappedValue<'a>> {
match id {
"id" => Some(MappedValue::Int32(self.id)),
"username" => Some(MappedValue::String(&self.username)),
"name" => Some(MappedValue::String(&self.name)),
_ => None,
}
}
}
impl<'a> MappedStructure<'a> for Item {
fn get_value(&'a self, id: &str) -> Option<MappedValue<'a>> {
match id {
"id" => Some(MappedValue::Int32(self.id)),
"title" => Some(MappedValue::String(&self.title)),
"body" => Some(MappedValue::String(&self.body)),
_ => None,
}
}
}
#[test]
fn custom() -> Result<()> {
let page = Page::new();
println!("{:?}",
page.get_value(&"total_pages")
);
if let Some(MappedValue::List(l)) = page.get_value(&"items") {
l.for_each(&|x: &MappedValue, buf: &mut String| {
if let MappedValue::Struct(s) = x {
println!(" - {:?}", s.get_value(&"title"));
buf.push_str(&format!("{:?}",s.get_value(&"title")));
}
Ok(())
}).expect("loop");
}
Ok(())
}
|