Adjusted error handling
This commit is contained in:
@ -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"
|
||||
|
36
src/error.rs
36
src/error.rs
@ -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!()
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,6 @@
|
||||
pub mod transducer;
|
||||
pub mod control;
|
||||
pub mod cell;
|
||||
mod error;
|
||||
pub mod error;
|
||||
|
||||
pub use error::CriticalError;
|
||||
|
Reference in New Issue
Block a user