nash_protocol/
errors.rs

1//! Error types defined for nash_protocol
2
3use thiserror::Error;
4
5/// Expose custom Result type that wraps ProtocolError
6pub type Result<T> = std::result::Result<T, ProtocolError>;
7
8/// Wrapper over string that describes error condition
9#[derive(Debug, Error, Clone)]
10pub struct ProtocolError(pub &'static str);
11
12impl ProtocolError {
13    // FIXME: this is a terrible hack. Added temporarily because so much code was already relying
14    // upon &'static str creation of protocol errors, but migrate everything to String and allow
15    // construction of error messages dynamically
16    pub fn coerce_static_from_str(error_str: &str) -> Self {
17        let coerce_static = Box::leak(error_str.to_string().into_boxed_str());
18        ProtocolError(coerce_static)
19    }
20}
21
22impl std::fmt::Display for ProtocolError {
23    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
24        write!(f, "{}", self.0)
25    }
26}
27
28use bigdecimal::ParseBigDecimalError;
29
30impl From<ParseBigDecimalError> for ProtocolError {
31    fn from(_err: ParseBigDecimalError) -> Self {
32        ProtocolError("Error converting to BigDecimal")
33    }
34}