Files
physical/src/error.rs
2025-03-09 12:13:14 -07:00

42 lines
1.7 KiB
Rust

/// Indicates the transducer value is known to be impossible.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct InvalidValue;
/// Indicates that the encoded data is not valid for the type.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct InvalidEncoding;
/// 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),
}
/// 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
fn terminal(self, terminate: impl FnOnce(E)) -> T;
}
impl <T, E> Terminal<T, E> for Result<T, E> {
fn terminal(self, terminate: impl FnOnce(E)) -> T {
match self {
Ok(value) => value,
Err(error) => {
//TODO: Remove panic when terminate returns -> !
terminate(error);
panic!()
},
}
}
}