Improve error handling, ensure robust defaults, and eliminate unsafe unwraps/expectations across modules.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
use alloc::format;
|
||||
use alloc::string::{String, ToString};
|
||||
use chrono::format::ParseErrorKind;
|
||||
use chrono_tz::ParseError;
|
||||
use core::convert::Infallible;
|
||||
use core::fmt;
|
||||
use core::fmt::Debug;
|
||||
@@ -149,6 +151,23 @@ impl<T> ContextExt<T> for Option<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, E> ContextExt<T> for Result<T, E>
|
||||
where
|
||||
E: fmt::Debug,
|
||||
{
|
||||
fn context<C>(self, context: C) -> Result<T, FatError>
|
||||
where
|
||||
C: AsRef<str>,
|
||||
{
|
||||
match self {
|
||||
Ok(value) => Ok(value),
|
||||
Err(err) => Err(FatError::String {
|
||||
error: format!("{}: {:?}", context.as_ref(), err),
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Error<Infallible>> for FatError {
|
||||
fn from(error: Error<Infallible>) -> Self {
|
||||
FatError::OneWireError { error }
|
||||
@@ -283,7 +302,7 @@ impl<E: fmt::Debug> From<ShuntVoltageReadError<I2cDeviceError<E>>> for FatError
|
||||
|
||||
impl From<Infallible> for FatError {
|
||||
fn from(value: Infallible) -> Self {
|
||||
panic!("Infallible error: {:?}", value)
|
||||
match value {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -336,3 +355,27 @@ impl From<BmsProtocolError> for FatError {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ParseError> for FatError {
|
||||
fn from(value: ParseError) -> Self {
|
||||
FatError::String {
|
||||
error: format!("Parsing error: {value:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ParseErrorKind> for FatError {
|
||||
fn from(value: ParseErrorKind) -> Self {
|
||||
FatError::String {
|
||||
error: format!("Parsing error: {value:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<chrono::format::ParseError> for FatError {
|
||||
fn from(value: chrono::format::ParseError) -> Self {
|
||||
FatError::String {
|
||||
error: format!("Parsing error: {value:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user