From 32928f79132aebd56275e31d60cd6087f6aab7c2 Mon Sep 17 00:00:00 2001 From: Able Date: Thu, 30 Mar 2023 13:53:52 -0500 Subject: [PATCH] Hacked together object system --- Cargo.lock | 1 + libraries/audio_interface/src/lib.rs | 7 ++- libraries/std/Cargo.toml | 1 + libraries/std/src/lib.rs | 7 +++ libraries/std/src/object/mod.rs | 84 ++++++++++++++++++++++++++++ libraries/std/src/object/types.rs | 48 ++++++++++++++++ 6 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 libraries/std/src/object/mod.rs create mode 100644 libraries/std/src/object/types.rs diff --git a/Cargo.lock b/Cargo.lock index 9d3c9c0..b88af32 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -330,6 +330,7 @@ dependencies = [ name = "std" version = "0.1.0" dependencies = [ + "hashbrown 0.13.1", "versioning", ] diff --git a/libraries/audio_interface/src/lib.rs b/libraries/audio_interface/src/lib.rs index d5521f7..66398d4 100644 --- a/libraries/audio_interface/src/lib.rs +++ b/libraries/audio_interface/src/lib.rs @@ -17,8 +17,9 @@ impl AudioInterface for AudioObject { Ok(()) } - fn stop() -> Result<(), AudioErrors> { - todo!(); + fn stop(&mut self) -> Result<(), AudioErrors> { + self.stop(); + self.playback_ring = [0; 2048]; Ok(()) } @@ -33,7 +34,7 @@ pub trait AudioInterface { fn play() -> Result<(), AudioErrors>; // Stop will clear the buffer and stop playback - fn stop() -> Result<(), AudioErrors>; + fn stop(&mut self) -> Result<(), AudioErrors>; fn fill_playback_buffer(&mut self, playback_slice: [u8; 128]) -> Result<(), AudioErrors>; } diff --git a/libraries/std/Cargo.toml b/libraries/std/Cargo.toml index d6a73f3..7c75d11 100644 --- a/libraries/std/Cargo.toml +++ b/libraries/std/Cargo.toml @@ -7,3 +7,4 @@ edition = "2021" [dependencies] versioning = { path = "../versioning" } +hashbrown = "0.13" diff --git a/libraries/std/src/lib.rs b/libraries/std/src/lib.rs index c54f1a6..1b09fe3 100644 --- a/libraries/std/src/lib.rs +++ b/libraries/std/src/lib.rs @@ -2,12 +2,19 @@ #![feature(panic_info_message)] #![no_std] +#[cfg(not(test))] mod entry; + pub mod env; pub mod exit; pub mod io; +pub mod object; + +#[cfg(not(test))] pub mod panic; +extern crate alloc; + use core::{arch::asm, fmt::Display}; #[prelude_import] diff --git a/libraries/std/src/object/mod.rs b/libraries/std/src/object/mod.rs new file mode 100644 index 0000000..32b6aa4 --- /dev/null +++ b/libraries/std/src/object/mod.rs @@ -0,0 +1,84 @@ +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::() +} diff --git a/libraries/std/src/object/types.rs b/libraries/std/src/object/types.rs new file mode 100644 index 0000000..5c33d25 --- /dev/null +++ b/libraries/std/src/object/types.rs @@ -0,0 +1,48 @@ +use core::fmt::Debug; +use core::fmt::Display; + +use alloc::string::{String, ToString}; + +use super::type_of; + +#[derive(PartialEq, Eq, Hash, Debug, Clone)] +pub enum Types { + Null, + Byte(u8), + String(String), +} +impl Types { + pub fn new(value: T) -> Self { + let type_of = type_of(&value); + match type_of { + "&str" => return Self::String(value.to_string()), + "alloc::string::String" => return Self::String(value.to_string()), + type_name => { + panic!( + " + Invalid type: {} + {}", + value, type_name + ); + } + } + } +} + +impl From for Types { + fn from(value: u8) -> Self { + Self::Byte(value) + } +} + +impl From for Types { + fn from(value: String) -> Self { + Self::String(value) + } +} + +impl From<&str> for Types { + fn from(value: &str) -> Self { + Self::String(value.to_string()) + } +}