error trait

This commit is contained in:
Able 2023-03-30 05:21:18 -05:00
parent f16c9de3d5
commit 3ab9778ea1
7 changed files with 55 additions and 33 deletions

3
Cargo.lock generated
View file

@ -40,6 +40,9 @@ checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6"
[[package]] [[package]]
name = "audio_interface" name = "audio_interface"
version = "0.1.0" version = "0.1.0"
dependencies = [
"std",
]
[[package]] [[package]]
name = "base64" name = "base64"

View file

@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
std = { path = "../std" }

View file

@ -1,3 +1,5 @@
#![no_std]
pub struct AudioObject { pub struct AudioObject {
// Play one bit per this interval // Play one bit per this interval
playback_speed: u64, playback_speed: u64,
@ -11,22 +13,21 @@ impl AudioInterface for AudioObject {
} }
fn play() -> Result<(), AudioErrors> { fn play() -> Result<(), AudioErrors> {
todo!() todo!();
Ok(())
} }
fn stop() -> Result<(), AudioErrors> { fn stop() -> Result<(), AudioErrors> {
todo!() todo!();
Ok(())
} }
fn fill_playback_buffer(&mut self, playback_slice: [u8; 128]) -> Result<(), AudioErrors> { fn fill_playback_buffer(&mut self, playback_slice: [u8; 128]) -> Result<(), AudioErrors> {
todo!() todo!();
Ok(())
} }
} }
pub enum AudioErrors {
EmptyPlaybackBuffer,
}
pub trait AudioInterface { pub trait AudioInterface {
fn pause(&mut self) -> Result<(), AudioErrors>; fn pause(&mut self) -> Result<(), AudioErrors>;
fn play() -> 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>; 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,
}
}
}

View file

@ -1,4 +1,4 @@
use crate::{exit::exit, Error}; use crate::exit::{exit, GenericError};
#[no_mangle] #[no_mangle]
unsafe extern "C" fn _start() -> ! { unsafe extern "C" fn _start() -> ! {
@ -16,10 +16,10 @@ unsafe extern "C" fn _start() -> ! {
1 => { 1 => {
// TODO: Get errorID location and error message // TODO: Get errorID location and error message
Some(Error { Some(GenericError {
error_id: 123456789, error_id: 123456789,
location: None, // location: None,
a_lil_message_as_a_treat: "Unknown Cause", // a_lil_message_as_a_treat: "Unknown Cause",
}) })
} }
_ => panic!("Program returned an invalid isize"), _ => panic!("Program returned an invalid isize"),

View file

@ -1,4 +1,18 @@
use crate::Error; use crate::{Error, LocationInFile};
/// Exit a program /// Exit a program
pub fn exit(error_code: Option<Error>) -> ! {} pub fn exit(error_code: Option<impl Error>) -> ! {
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
}
}

View file

@ -2,11 +2,11 @@ pub enum IOErrors {
UnknownError, UnknownError,
} }
pub struct Port<T> { // pub struct Port<T> {
inner: T, // inner: T,
} // }
impl<T> Port<T> { // impl<T> Port<T> {
pub fn read(&self) -> Result<T, IOErrors> { // pub fn read(&self) -> Result<T, IOErrors> {
Ok(self.inner) // Ok(self.inner)
} // }
} // }

View file

@ -58,21 +58,12 @@ pub struct LocationInFile<'a> {
line: u16, line: u16,
column: 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 { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}{}{}", self.file_path, self.line, self.column) write!(f, "{}{}{}", self.file_path, self.line, self.column)
} }
} }
pub struct Error<'a> { pub trait Error {
error_id: u128, fn id(&self) -> u128;
// Optional location of the error
location: Option<LocationInFile<'a>>,
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)
}
} }