Skip to main content

tidepool_bridge/
error.rs

1use std::error::Error;
2use std::fmt;
3use tidepool_repr::DataConId;
4
5/// Errors that can occur when bridging between Rust types and Core Values.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum BridgeError {
8    /// The `DataConId` was not found in the `DataConTable`.
9    UnknownDataCon(DataConId),
10    /// The `DataConId` was found, but it has an unexpected name.
11    UnknownDataConName(String),
12    /// The number of fields in a constructor does not match the expected arity.
13    ArityMismatch {
14        /// The constructor identifier.
15        con: DataConId,
16        /// The expected number of fields.
17        expected: usize,
18        /// The actual number of fields received.
19        got: usize,
20    },
21    /// The value has an unexpected type (e.g., expected a Literal, got a Con).
22    TypeMismatch {
23        /// A description of the expected type.
24        expected: String,
25        /// A description of the actual type received.
26        got: String,
27    },
28    /// The type is not supported by the bridge.
29    UnsupportedType(String),
30    /// Internal invariant violation (should never happen).
31    InternalError(String),
32}
33
34impl fmt::Display for BridgeError {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        match self {
37            BridgeError::UnknownDataCon(id) => write!(f, "Unknown DataConId: {:?}", id),
38            BridgeError::UnknownDataConName(name) => write!(f, "Unknown DataCon name: {}", name),
39            BridgeError::ArityMismatch { con, expected, got } => {
40                write!(
41                    f,
42                    "Arity mismatch for DataCon {:?}: expected {}, got {}",
43                    con, expected, got
44                )
45            }
46            BridgeError::TypeMismatch { expected, got } => {
47                write!(f, "Type mismatch: expected {}, got {}", expected, got)
48            }
49            BridgeError::UnsupportedType(ty) => write!(f, "Unsupported type: {}", ty),
50            BridgeError::InternalError(msg) => write!(f, "Internal error: {}", msg),
51        }
52    }
53}
54
55impl Error for BridgeError {}