Skip to main content

ValueCodec

Struct ValueCodec 

Source
pub struct ValueCodec;
Expand description

Protocol-agnostic value coercion utilities. Centralizes scaling, rounding, clamping, and common string/boolean parsing.

Implementations§

Source§

impl ValueCodec

Source

pub fn logical_to_wire_value( value: &NGValue, logical_dt: DataType, wire_dt: DataType, t: &Transform, ) -> DriverResult<NGValue>

Source

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 NGValue that matches wire_dt
  • Then call this function once to apply Transform and box into logical_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.

Source

pub fn apply_transform_f64(x: f64, t: &Transform) -> f64

Source

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.
Source

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.

Source

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 scale is None and the target is an integer type, this avoids any f64 roundtrip and therefore preserves full integer precision.
  • When scale is Some(_), we apply scaling in f64 and then delegate to coerce_f64_to_value for consistent rounding behavior.
Source

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.

Source

pub fn time_of_day_to_ms(t: NaiveTime) -> u64

Time helpers: centralize rendering to string or epoch-ms conversions for drivers.

Source

pub fn duration_to_ms(d: Duration) -> i64

Source

pub fn date_to_epoch_ms(d: NaiveDate) -> Option<i64>

Source

pub fn datetime_to_epoch_ms(ndt: NaiveDateTime) -> i64

Source

pub fn bytes_to_hex_string(bytes: &[u8]) -> String

Convert a byte slice into a lower-case hex string with “0x” prefix.

Source

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).

Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts 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>

Converts 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)

Converts &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)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_sync(self: Box<T>) -> Box<dyn Any + Send + Sync>

Converts Box<Trait> (where Trait: DowncastSync) to Box<dyn Any + Send + Sync>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Converts Arc<Trait> (where Trait: DowncastSync) to Arc<Any>, which can then be downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more