feat: Add ErrorContext trait implementation for anyhow::Result to enable context error handling with ChargingError conversion

- Add ChargingError import from super::types
- Add ErrorContext<T> implementation for anyhow::Result<T> with with_context method that maps errors to ChargingError::InternalError with formatted context message
This commit is contained in:
nutcas3
2026-04-25 00:48:16 +03:00
parent 5c03c48e22
commit 1f90b27d34
@@ -1,11 +1,20 @@
use anyhow::Context;
use super::types::ChargingError;
pub type ChargingResult<T> = Result<T, ChargingError>;
pub trait ErrorContext<T> {
fn with_context(self, msg: &str) -> ChargingResult<T>;
}
impl<T> ErrorContext<T> for anyhow::Result<T> {
fn with_context(self, msg: &str) -> ChargingResult<T> {
self.map_err(|e| ChargingError::InternalError(format!("{}: {}", msg, e)))
.map_err(|e| e.into())
}
}
impl<T, E> ErrorContext<T> for Result<T, E>
where
E: Into<ChargingError>,