diff --git a/src/lib.rs b/src/lib.rs index e69de29..8147462 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -0,0 +1,47 @@ +use std::{cell::RefCell, collections::HashMap, hash::Hash, rc::Rc}; + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub enum ValueInner { + Number(i64), + String(String), + Table(Table), +} + +pub type Value = Option; + +#[derive(Debug, Clone, Default)] +struct TableInner { + content: HashMap, +} + +#[derive(Debug, Clone, Default)] +pub struct Table(Rc>); + +impl PartialEq for Table { + fn eq(&self, other: &Self) -> bool { + std::ptr::eq(self.0.as_ptr(), other.0.as_ptr()) + } +} + +impl Eq for Table {} + +impl Hash for Table { + fn hash(&self, state: &mut H) { + std::ptr::hash(self.0.as_ptr(), state) + } +} + +impl Table { + pub fn get(&self, key: ValueInner) -> Value { + self.0.borrow().content.get(&key).cloned() + } + + pub fn set(&self, key: ValueInner, value: Value) { + let mut this = self.0.borrow_mut(); + if let Some(value) = value { + this.content.insert(key, value); + } else { + this.content.remove(&key); + } + } +}