pub struct ValueCodec;Expand description
Protocol-agnostic value coercion utilities. Centralizes scaling, rounding, clamping, and common string/boolean parsing.
Implementations§
Source§impl ValueCodec
impl ValueCodec
pub fn logical_to_wire_value( value: &NGValue, logical_dt: DataType, wire_dt: DataType, t: &Transform, ) -> DriverResult<NGValue>
Sourcepub fn wire_to_logical_value(
value: &NGValue,
wire_dt: DataType,
logical_dt: DataType,
t: &Transform,
) -> DriverResult<NGValue>
pub fn wire_to_logical_value( value: &NGValue, wire_dt: DataType, logical_dt: DataType, t: &Transform, ) -> DriverResult<NGValue>
Coerce a wire-layer NGValue into a logical-layer NGValue (uplink).
§Design goal
Keep driver code aligned with existing coerce_*_to_value APIs while enforcing a
single, consistent uplink policy:
- Decode protocol payload into a wire
NGValuethat matcheswire_dt - Then call this function once to apply
Transformand box intological_dt
§Safety policy (important)
When a non-identity numeric transform is configured, this conversion requires an f64
intermediate. For 64-bit integers beyond (2^{53}), f64 cannot represent all integers
exactly. To avoid silent corruption, we reject such cases.
pub fn apply_transform_f64(x: f64, t: &Transform) -> f64
Sourcepub fn coerce_bool_to_value(
value: bool,
expected: DataType,
t: &Transform,
) -> Option<NGValue>
pub fn coerce_bool_to_value( value: bool, expected: DataType, t: &Transform, ) -> Option<NGValue>
Apply optional numeric transform and return an NGValue in the expected DataType.
§Notes
- This is the recommended hot-path conversion API for drivers.
- Timestamp/Binary require protocol-specific parsing and are not supported here.
Sourcepub fn coerce_f64_to_value(
value: f64,
expected: DataType,
t: &Transform,
) -> Option<NGValue>
pub fn coerce_f64_to_value( value: f64, expected: DataType, t: &Transform, ) -> Option<NGValue>
Coerce a numeric value (f64) into an expected DataType with optional transform.
§Performance
This avoids serde_json::Value allocations and should be used in hot paths.
Sourcepub fn coerce_u64_to_value(
value: u64,
expected: DataType,
t: &Transform,
) -> Option<NGValue>
pub fn coerce_u64_to_value( value: u64, expected: DataType, t: &Transform, ) -> Option<NGValue>
Coerce an unsigned integer source into an expected DataType with optional transform.
§Performance & correctness
- When
scaleisNoneand the target is an integer type, this avoids anyf64roundtrip and therefore preserves full integer precision. - When
scaleisSome(_), we apply scaling inf64and then delegate tocoerce_f64_to_valuefor consistent rounding behavior.
Sourcepub fn coerce_i64_to_value(
value: i64,
expected: DataType,
t: &Transform,
) -> Option<NGValue>
pub fn coerce_i64_to_value( value: i64, expected: DataType, t: &Transform, ) -> Option<NGValue>
Coerce a signed integer source into an expected DataType with optional transform.
See coerce_u64_to_value for performance semantics.
Sourcepub fn time_of_day_to_ms(t: NaiveTime) -> u64
pub fn time_of_day_to_ms(t: NaiveTime) -> u64
Time helpers: centralize rendering to string or epoch-ms conversions for drivers.
pub fn duration_to_ms(d: Duration) -> i64
pub fn date_to_epoch_ms(d: NaiveDate) -> Option<i64>
pub fn datetime_to_epoch_ms(ndt: NaiveDateTime) -> i64
Sourcepub fn bytes_to_hex_string(bytes: &[u8]) -> String
pub fn bytes_to_hex_string(bytes: &[u8]) -> String
Convert a byte slice into a lower-case hex string with “0x” prefix.
Sourcepub fn hex_string_to_bytes(s: &str) -> Option<Vec<u8>>
pub fn hex_string_to_bytes(s: &str) -> Option<Vec<u8>>
Decode a hex string into bytes. Accepts optional “0x”/“0X” prefix and odd length (pads low nibble with 0).
Sourcepub fn json_to_timestamp_ms(v: &Value) -> Option<i64>
pub fn json_to_timestamp_ms(v: &Value) -> Option<i64>
Parse a JSON value as epoch milliseconds (UTC).
- String: RFC3339 parsed in any offset, converted to UTC ms
- Number: treated as epoch milliseconds
Auto Trait Implementations§
impl Freeze for ValueCodec
impl RefUnwindSafe for ValueCodec
impl Send for ValueCodec
impl Sync for ValueCodec
impl Unpin for ValueCodec
impl UnwindSafe for ValueCodec
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.