Initial node implementation (#4)
Reviewed-on: #4 Co-authored-by: Zack <zack@bfpower.io> Co-committed-by: Zack <zack@bfpower.io>
This commit is contained in:
41
src/error.rs
Normal file
41
src/error.rs
Normal file
@ -0,0 +1,41 @@
|
||||
use crate::transducer::InvalidValue;
|
||||
|
||||
/// An error that it is likely impossible to recover from. This error should only be created in
|
||||
/// situations where attempts to recover have already been attempted and have failed. Error handling
|
||||
/// should consist of attempting to alert another system for maintenance and attempting to shut down
|
||||
/// any systems that depend on the correct functionality of the component having errors.
|
||||
///
|
||||
/// In many systems there can be a single function for handling any critical error as a critical
|
||||
/// error always means everything needs to be stopped.
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
||||
pub enum CriticalError {
|
||||
/// Critical communication failed and retries are either impossible or also failed.
|
||||
Communication,
|
||||
InvalidValue(InvalidValue),
|
||||
}
|
||||
|
||||
impl CriticalError {
|
||||
pub fn emergency_procedure(self, procedure: impl FnOnce(CriticalError) -> !) -> ! {
|
||||
procedure(self);
|
||||
}
|
||||
}
|
||||
|
||||
/// [Result] where error type is [CriticalError].
|
||||
pub trait CriticalErrResult: Copy {
|
||||
type Value: Copy;
|
||||
|
||||
/// 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;
|
||||
|
||||
fn err_emproc(self, procedure: impl FnOnce(CriticalError) -> !) -> Self::Value {
|
||||
match self {
|
||||
Ok(val) => val,
|
||||
Err(error) => error.emergency_procedure(procedure),
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user