diff --git a/Cargo.toml b/Cargo.toml index 27bb1f0..b899963 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/error.rs b/src/error.rs index a761418..a368f05 100644 --- a/src/error.rs +++ b/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 { //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 CriticalErrResult for Result { - 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 Terminal for Result { + 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!() + }, } } }