From 3ab9778ea1a5c85a62cd1c04dfc70de8b947caa9 Mon Sep 17 00:00:00 2001 From: Able Date: Thu, 30 Mar 2023 05:21:18 -0500 Subject: [PATCH] error trait --- Cargo.lock | 3 +++ libraries/audio_interface/Cargo.toml | 1 + libraries/audio_interface/src/lib.rs | 27 ++++++++++++++++++++------- libraries/std/src/entry.rs | 8 ++++---- libraries/std/src/exit.rs | 18 ++++++++++++++++-- libraries/std/src/io.rs | 16 ++++++++-------- libraries/std/src/lib.rs | 15 +++------------ 7 files changed, 55 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5ac3906..9d3c9c0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -40,6 +40,9 @@ checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6" [[package]] name = "audio_interface" version = "0.1.0" +dependencies = [ + "std", +] [[package]] name = "base64" diff --git a/libraries/audio_interface/Cargo.toml b/libraries/audio_interface/Cargo.toml index 50cb4fc..11fe12f 100644 --- a/libraries/audio_interface/Cargo.toml +++ b/libraries/audio_interface/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +std = { path = "../std" } diff --git a/libraries/audio_interface/src/lib.rs b/libraries/audio_interface/src/lib.rs index 36bb60b..d5521f7 100644 --- a/libraries/audio_interface/src/lib.rs +++ b/libraries/audio_interface/src/lib.rs @@ -1,3 +1,5 @@ +#![no_std] + pub struct AudioObject { // Play one bit per this interval playback_speed: u64, @@ -11,22 +13,21 @@ impl AudioInterface for AudioObject { } fn play() -> Result<(), AudioErrors> { - todo!() + todo!(); + Ok(()) } fn stop() -> Result<(), AudioErrors> { - todo!() + todo!(); + Ok(()) } fn fill_playback_buffer(&mut self, playback_slice: [u8; 128]) -> Result<(), AudioErrors> { - todo!() + todo!(); + Ok(()) } } -pub enum AudioErrors { - EmptyPlaybackBuffer, -} - pub trait AudioInterface { fn pause(&mut self) -> Result<(), AudioErrors>; fn play() -> Result<(), AudioErrors>; @@ -36,3 +37,15 @@ pub trait AudioInterface { fn fill_playback_buffer(&mut self, playback_slice: [u8; 128]) -> Result<(), AudioErrors>; } + +pub enum AudioErrors { + EmptyPlaybackBuffer = 1, +} + +impl std::Error for AudioErrors { + fn id(&self) -> u128 { + match self { + AudioErrors::EmptyPlaybackBuffer => 1, + } + } +} diff --git a/libraries/std/src/entry.rs b/libraries/std/src/entry.rs index 97cef34..933653a 100644 --- a/libraries/std/src/entry.rs +++ b/libraries/std/src/entry.rs @@ -1,4 +1,4 @@ -use crate::{exit::exit, Error}; +use crate::exit::{exit, GenericError}; #[no_mangle] unsafe extern "C" fn _start() -> ! { @@ -16,10 +16,10 @@ unsafe extern "C" fn _start() -> ! { 1 => { // TODO: Get errorID location and error message - Some(Error { + Some(GenericError { error_id: 123456789, - location: None, - a_lil_message_as_a_treat: "Unknown Cause", + // location: None, + // a_lil_message_as_a_treat: "Unknown Cause", }) } _ => panic!("Program returned an invalid isize"), diff --git a/libraries/std/src/exit.rs b/libraries/std/src/exit.rs index aa15c06..044864f 100644 --- a/libraries/std/src/exit.rs +++ b/libraries/std/src/exit.rs @@ -1,4 +1,18 @@ -use crate::Error; +use crate::{Error, LocationInFile}; /// Exit a program -pub fn exit(error_code: Option) -> ! {} +pub fn exit(error_code: Option) -> ! { + loop {} +} + +pub struct GenericError { + pub error_id: u128, + // location: LocationInFile<'a>, + // a_lil_message_as_a_treat: &'a str, +} + +impl Error for GenericError { + fn id(&self) -> u128 { + self.error_id + } +} diff --git a/libraries/std/src/io.rs b/libraries/std/src/io.rs index 78fee16..b7092ce 100644 --- a/libraries/std/src/io.rs +++ b/libraries/std/src/io.rs @@ -2,11 +2,11 @@ pub enum IOErrors { UnknownError, } -pub struct Port { - inner: T, -} -impl Port { - pub fn read(&self) -> Result { - Ok(self.inner) - } -} +// pub struct Port { +// inner: T, +// } +// impl Port { +// pub fn read(&self) -> Result { +// Ok(self.inner) +// } +// } diff --git a/libraries/std/src/lib.rs b/libraries/std/src/lib.rs index 5801604..c54f1a6 100644 --- a/libraries/std/src/lib.rs +++ b/libraries/std/src/lib.rs @@ -58,21 +58,12 @@ pub struct LocationInFile<'a> { line: u16, column: u16, } -impl<'a> Display for Error<'a> { +impl<'a> Display for LocationInFile<'a> { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "{}{}{}", self.file_path, self.line, self.column) } } -pub struct Error<'a> { - error_id: u128, - // Optional location of the error - location: Option>, - a_lil_message_as_a_treat: &'a str, -} - -impl<'a> Display for Error<'a> { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - write!(f, "EID: {}\nSRC: {}", self.error_id, self.location) - } +pub trait Error { + fn id(&self) -> u128; }