pub mod types; use types::*; use alloc::string::{String, ToString}; use alloc::vec::Vec; use hashbrown::HashMap; #[derive(Debug)] pub enum ObjectErrors { PropertyNotFound, NoProperties, FailureToInsert, } pub struct Object { properties: HashMap, children: Option>, } impl Object { pub fn new(name: Types) -> Self { let mut obj = Self { properties: HashMap::new(), children: None, }; let _ = obj.set_value("name".into(), name); obj } pub fn get_value(&mut self, key: Types) -> Result { use ObjectErrors::*; if self.properties.is_empty() { return Err(NoProperties); } let ret = self.properties.get(&key).unwrap(); Ok(ret.clone()) } pub fn set_value(&mut self, key: Types, value: Types) -> Result<(), ObjectErrors> { let ret = self.properties.insert(key, value); match ret { Some(_) => {} None => return Err(ObjectErrors::FailureToInsert), } Ok(()) } } #[cfg(test)] mod tests { use super::{types::Types, Object}; #[test] fn object_creation() { let obj = Object::new(Types::new("hi")); } #[test] fn property_set() { let key = Types::Byte(8); let value = Types::Byte(9); let mut obj = Object::new("hi".into()); obj.set_value(key, value); } #[test] fn property_read() { let key = Types::new("name"); let mut obj = Object::new("hi".into()); obj.set_value("name".into(), "hi".into()).unwrap(); let ret = obj.get_value(key); match ret { Ok(val) => assert_eq!(val, Types::new("hi")), Err(_err) => todo!(), } } } #[inline] fn type_of(_: &T) -> &str { core::any::type_name::() }