Skip to content

Commit 754f56c

Browse files
committed
globals will raise an error if defined twice
1 parent 396799a commit 754f56c

4 files changed

Lines changed: 20 additions & 4 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* BUGFIX: ctrl+c works with the error display
77
* Trying to use a component twice will now include the component name in the
88
error
9+
* Global definitions will raise an error if it's already assigned
910
* 0.2.7
1011
* BUGFIX: use correct truthiness check in control flow update
1112
* 0.2.6

anathema-templates/src/error/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub enum ErrorKind {
5454
EmptyBody,
5555
InvalidStatement(String),
5656
Io(std::io::Error),
57+
GlobalAlreadyAssigned(String)
5758
}
5859

5960
impl ErrorKind {
@@ -72,6 +73,7 @@ impl Display for ErrorKind {
7273
ErrorKind::EmptyBody => write!(f, "if or else node has no children"),
7374
ErrorKind::InvalidStatement(stmt) => write!(f, "invalid statement: {stmt}"),
7475
ErrorKind::Io(err) => write!(f, "{err}"),
76+
ErrorKind::GlobalAlreadyAssigned(name) => write!(f, "global value `{name}` already assigned"),
7577
}
7678
}
7779
}

anathema-templates/src/statements/eval.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ impl Scope {
6161
}
6262
match is_global {
6363
false => _ = ctx.variables.define_local(binding, value),
64-
true => ctx.variables.define_global(binding, value),
64+
true => match ctx.variables.define_global(binding, value) {
65+
Ok(()) => (),
66+
Err(kind) => return Err(kind.to_error(ctx.template.path())),
67+
}
6568
}
6669
}
6770
Statement::ComponentSlot(slot_id) => {

anathema-templates/src/variables.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::collections::HashMap;
22

33
use anathema_store::slab::{Slab, SlabIndex};
44

5-
use crate::expressions::Expression;
5+
use crate::{error::ErrorKind, expressions::Expression};
66

77
#[derive(Debug, Default, Clone)]
88
pub(crate) struct Globals(HashMap<String, Expression>);
@@ -12,6 +12,10 @@ impl Globals {
1212
Self(HashMap::new())
1313
}
1414

15+
pub fn contains(&self, ident: &str) -> bool {
16+
self.0.contains_key(ident)
17+
}
18+
1519
pub fn get(&self, ident: &str) -> Option<&Expression> {
1620
self.0.get(ident)
1721
}
@@ -232,9 +236,15 @@ impl Variables {
232236
var_id
233237
}
234238

235-
pub fn define_global(&mut self, ident: impl Into<String>, value: impl Into<Expression>) {
239+
pub fn define_global(&mut self, ident: impl Into<String>, value: impl Into<Expression>) -> Result<(), ErrorKind> {
240+
let ident = ident.into();
241+
if self.globals.contains(&ident) {
242+
return Err(ErrorKind::GlobalAlreadyAssigned(ident));
243+
}
244+
236245
let value = value.into();
237-
self.globals.set(ident.into(), value)
246+
self.globals.set(ident, value);
247+
Ok(())
238248
}
239249

240250
pub fn define_local(&mut self, ident: impl Into<String>, value: impl Into<Expression>) -> VarId {

0 commit comments

Comments
 (0)