tidepool_bridge/traits.rs
1use crate::error::BridgeError;
2use tidepool_eval::Value;
3use tidepool_repr::DataConTable;
4
5/// Convert a Core Value (from evaluation) to a Rust type.
6///
7/// This trait is used to extract native Rust values from evaluated Core expressions.
8/// Implementations should handle potential type mismatches and arity errors.
9pub trait FromCore: Sized {
10 /// Convert a Value to this type using the provided DataConTable for lookups.
11 ///
12 /// # Errors
13 ///
14 /// Returns `BridgeError::TypeMismatch` if the value's variant doesn't match the expected type.
15 /// Returns `BridgeError::UnknownDataCon` or `BridgeError::UnknownDataConName` if a required
16 /// constructor is missing from the table.
17 /// Returns `BridgeError::ArityMismatch` if a constructor has the wrong number of fields.
18 fn from_value(value: &Value, table: &DataConTable) -> Result<Self, BridgeError>;
19}
20
21/// Convert a Rust type to a Core Value (for interpolation into CoreExpr or evaluation).
22///
23/// This trait is used to inject Rust values into the Core evaluator.
24pub trait ToCore {
25 /// Convert this type to a Value using the provided DataConTable for lookups.
26 ///
27 /// # Errors
28 ///
29 /// Returns `BridgeError::UnknownDataConName` if a required constructor is missing from the table.
30 fn to_value(&self, table: &DataConTable) -> Result<Value, BridgeError>;
31}