Adjusted error handling

This commit is contained in:
Zachary Sunforge
2024-06-14 12:38:33 -07:00
parent 3f79ef86d8
commit 3db6393320
2 changed files with 14 additions and 24 deletions

View File

@ -17,7 +17,7 @@ members = [
]
[workspace.package]
version = "0.2.0"
version = "0.2.1"
edition = "2021"
repository = "https://git.bfpower.io/BFPOWER/physical"
readme = "README.md"

View File

@ -14,33 +14,23 @@ pub enum CriticalError {
InvalidValue(InvalidValue),
}
impl CriticalError {
/// A state of this type may mean the program has encountered an error that prevents it from continuing to run
/// and should attempt to enter a safe terminal state.
/// e.g. Certain [Err]s
pub trait Terminal<T, E> {
//TODO: Switch to using ! as the return type for the FnOnce when the feature is stabilized
pub fn emergency_procedure(self, procedure: impl FnOnce(CriticalError)) -> ! {
procedure(self);
//TODO: Remove this panic when we switch to ! return type
panic!()
}
fn terminal(self, terminate: impl FnOnce(E)) -> T;
}
/// [Result] where error type is [CriticalError].
pub trait CriticalErrResult: Copy {
type Value: Copy;
//TODO: Switch to using ! as the return type for the FnOnce when the feature is stabilized
/// Execute emergency procedure in the event of a critical, the emergency procedure cannot
/// return. It should usually terminate the program, potentially rebooting the device in some sort of recovery mode.
fn err_emproc(self, procedure: impl FnOnce(CriticalError)) -> Self::Value;
}
impl<T: Copy> CriticalErrResult for Result<T, CriticalError> {
type Value = T;
//TODO: Switch to using ! as the return type for the FnOnce when the ! feature is stabilized
fn err_emproc(self, procedure: impl FnOnce(CriticalError)) -> Self::Value {
impl <T, E> Terminal<T, E> for Result<T, E> {
fn terminal(self, terminate: impl FnOnce(E)) -> T {
match self {
Ok(val) => val,
Err(error) => error.emergency_procedure(procedure),
Ok(value) => value,
Err(error) => {
//TODO: Remove panic when terminate returns -> !
terminate(error);
panic!()
},
}
}
}