Skip to content

Commit

Permalink
feat(types): support booleans and nulls as runtime value.
Browse files Browse the repository at this point in the history
  • Loading branch information
watcol committed Jul 26, 2022
1 parent 311979a commit b18788e
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions drake-types/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ pub enum Value<L> {
Integer(u64),
/// A literal float
Float(f64),
/// A boolean
Boolean(bool),
/// A null
Null,
/// An array
Array(Vec<Value<L>>),
/// A table
Table(Table<L>),
}


impl<L> Value<L> {
/// Returns a kind of the value
pub fn kind(&self) -> Kind {
Expand All @@ -39,6 +42,8 @@ impl<L> Value<L> {
Self::String(_) => Kind::String,
Self::Integer(_) => Kind::Integer,
Self::Float(_) => Kind::Float,
Self::Boolean(_) => Kind::Boolean,
Self::Null => Kind::Null,
Self::Array(_) => Kind::Array,
Self::Table(_) => Kind::Table,
}
Expand All @@ -53,6 +58,8 @@ pub enum Kind {
String,
Integer,
Float,
Boolean,
Null,
Array,
Table,
}
Expand Down Expand Up @@ -90,21 +97,30 @@ impl<L> Table<L> {
}

/// Inserts an element
pub fn insert(&mut self, key: Key<L>, value: Value<L>) -> Result<(), Error<L>> where L: Clone {
pub fn insert(&mut self, key: Key<L>, value: Value<L>) -> Result<(), Error<L>>
where
L: Clone,
{
let (table, used) = match key.kind {
KeyKind::Normal => (&mut self.global, true),
KeyKind::Local => (&mut self.global, false),
};

if table.contains_key(&key.name) && !table[&key.name].default {
Err(Error::DuplicateKey { existing: table[&key.name].defined.clone(), found: key.span })
Err(Error::DuplicateKey {
existing: table[&key.name].defined.clone(),
found: key.span,
})
} else {
table.insert(key.name, Element {
value,
defined: key.span,
default: false,
used,
});
table.insert(
key.name,
Element {
value,
defined: key.span,
default: false,
used,
},
);
Ok(())
}
}
Expand Down

0 comments on commit b18788e

Please sign in to comment.