Adjusted error handling
This commit is contained in:
@ -17,7 +17,7 @@ members = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
[workspace.package]
|
[workspace.package]
|
||||||
version = "0.2.0"
|
version = "0.2.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
repository = "https://git.bfpower.io/BFPOWER/physical"
|
repository = "https://git.bfpower.io/BFPOWER/physical"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
36
src/error.rs
36
src/error.rs
@ -14,33 +14,23 @@ pub enum CriticalError {
|
|||||||
InvalidValue(InvalidValue),
|
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
|
//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)) -> ! {
|
fn terminal(self, terminate: impl FnOnce(E)) -> T;
|
||||||
procedure(self);
|
|
||||||
//TODO: Remove this panic when we switch to ! return type
|
|
||||||
panic!()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// [Result] where error type is [CriticalError].
|
impl <T, E> Terminal<T, E> for Result<T, E> {
|
||||||
pub trait CriticalErrResult: Copy {
|
fn terminal(self, terminate: impl FnOnce(E)) -> T {
|
||||||
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 {
|
|
||||||
match self {
|
match self {
|
||||||
Ok(val) => val,
|
Ok(value) => value,
|
||||||
Err(error) => error.emergency_procedure(procedure),
|
Err(error) => {
|
||||||
|
//TODO: Remove panic when terminate returns -> !
|
||||||
|
terminate(error);
|
||||||
|
panic!()
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
pub mod transducer;
|
pub mod transducer;
|
||||||
pub mod control;
|
pub mod control;
|
||||||
pub mod cell;
|
pub mod cell;
|
||||||
mod error;
|
pub mod error;
|
||||||
|
|
||||||
pub use error::CriticalError;
|
pub use error::CriticalError;
|
||||||
|
Reference in New Issue
Block a user