Skip to content

Commit 9839570

Browse files
committed
BUGFIX: template reload no longer fails on globals
1 parent 14fa617 commit 9839570

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

anathema-templates/src/document.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ impl Document {
5757
}
5858

5959
pub fn compile(&mut self, globals: &mut Variables) -> Result<Blueprint> {
60+
globals.reset_globals();
6061
let tokens = Lexer::new(&self.template, &mut self.strings).collect::<Result<Vec<_>>>()?;
6162
let tokens = Tokens::new(tokens, self.template.len());
6263
let parser = Parser::new(tokens, &mut self.strings, &self.template, &mut self.components);

anathema-templates/src/variables.rs

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@ use anathema_store::slab::{Slab, SlabIndex};
66
use crate::error::ErrorKind;
77
use crate::expressions::Expression;
88

9+
#[derive(Debug, Clone)]
10+
enum Global {
11+
// The global value was set from the runtime
12+
Runtime(Expression),
13+
// The global value originates from a template
14+
Template(Expression),
15+
}
16+
917
#[derive(Debug, Default, Clone)]
10-
pub struct Globals(HashMap<String, Expression>);
18+
pub struct Globals(HashMap<String, Global>);
1119

1220
impl Globals {
1321
pub fn empty() -> Self {
@@ -19,15 +27,31 @@ impl Globals {
1927
}
2028

2129
pub fn get(&self, ident: &str) -> Option<&Expression> {
22-
self.0.get(ident)
30+
match self.0.get(ident)? {
31+
Global::Runtime(expression) | Global::Template(expression) => Some(expression),
32+
}
2333
}
2434

25-
pub fn set(&mut self, ident: String, value: Expression) {
35+
fn set(&mut self, ident: String, value: Global) {
2636
if self.0.contains_key(&ident) {
2737
return;
2838
}
2939
_ = self.0.insert(ident, value);
3040
}
41+
42+
fn clear_template_globals(&mut self) {
43+
let mut clear = vec![];
44+
for (key, glob) in &self.0 {
45+
match glob {
46+
Global::Runtime(_) => continue,
47+
Global::Template(_) => clear.push(key.to_owned()),
48+
}
49+
}
50+
51+
for key in clear {
52+
_ = self.0.remove(&key);
53+
}
54+
}
3155
}
3256

3357
#[derive(Debug, Copy, Clone, PartialEq)]
@@ -252,16 +276,34 @@ impl Variables {
252276
var_id
253277
}
254278

255-
pub fn define_global(&mut self, ident: impl Into<String>, value: impl Into<Expression>) -> Result<(), ErrorKind> {
279+
fn set_global(&mut self, ident: impl Into<String>, global: Global) -> Result<(), ErrorKind> {
256280
let ident = ident.into();
257281
if self.globals.contains(&ident) {
258282
return Err(ErrorKind::GlobalAlreadyAssigned(ident));
259283
}
260284

261-
self.globals.set(ident, value.into());
285+
self.globals.set(ident, global);
262286
Ok(())
263287
}
264288

289+
/// Reset the globals defined in the template.
290+
/// This keeps any globals registered through Rust
291+
pub fn reset_globals(&mut self) {
292+
self.globals.clear_template_globals();
293+
}
294+
295+
pub fn register_global(&mut self, ident: impl Into<String>, value: impl Into<Expression>) -> Result<(), ErrorKind> {
296+
let expression = value.into();
297+
let global = Global::Runtime(expression);
298+
self.set_global(ident, global)
299+
}
300+
301+
pub fn define_global(&mut self, ident: impl Into<String>, value: impl Into<Expression>) -> Result<(), ErrorKind> {
302+
let expression = value.into();
303+
let global = Global::Template(expression);
304+
self.set_global(ident, global)
305+
}
306+
265307
pub fn define_local(&mut self, ident: impl Into<String>, value: impl Into<Expression>) -> VarId {
266308
let value = value.into();
267309
let scope_id = self.current.clone();

anathema-widgets/src/nodes/loops.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ impl<'bp> For<'bp> {
2828
parent_widget: Option<WidgetId>,
2929
dirty_widgets: &mut DirtyWidgets,
3030
) -> Result<()> {
31+
if let Some(widget) = parent_widget {
32+
dirty_widgets.push(widget);
33+
}
34+
3135
match change {
3236
Change::Inserted(index) => {
3337
// 1. Declare insert path
@@ -89,9 +93,6 @@ impl<'bp> For<'bp> {
8993
// then truncate the tree
9094
self.collection.reload(ctx.attribute_storage);
9195
ctx.truncate_children(&mut tree);
92-
if let Some(widget) = parent_widget {
93-
dirty_widgets.push(widget);
94-
}
9596
}
9697
}
9798

0 commit comments

Comments
 (0)