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:
2023-07-19 18:09:13 +00:00
committed by Zachary Sunforge
parent 886fbf0020
commit 6fc828e864
40 changed files with 2079 additions and 44 deletions

41
src/error.rs Normal file
View 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),
}
}
}