digdigdig3-station 0.3.30

Consumer-facing builder over digdigdig3 ExchangeHub. Persistence, cache, replay, cure, orderbook tracker, multiplex, reconnect.
Documentation
use digdigdig3::core::types::StreamEvent;
use serde::{Deserialize, Serialize};

use crate::series::DataPoint;

/// One scalar (single-value) sample on a timeline. Used for
/// `Kind::CvdLine` and as the resampled output of `bar_align` for
/// state-style streams.
///
/// 16-byte fixed record (LE):
///   u64 ts_ms
///   f64 value
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct ScalarBarPoint {
    pub ts_ms: i64,
    pub value: f64,
}

const SIZE: usize = 16;

impl DataPoint for ScalarBarPoint {
    const RECORD_SIZE: usize = SIZE;

    fn encode(&self, out: &mut [u8]) {
        out[0..8].copy_from_slice(&(self.ts_ms as u64).to_le_bytes());
        out[8..16].copy_from_slice(&self.value.to_le_bytes());
    }

    fn decode(bytes: &[u8]) -> Option<Self> {
        if bytes.len() != SIZE { return None; }
        Some(Self {
            ts_ms: u64::from_le_bytes(bytes[0..8].try_into().ok()?) as i64,
            value: f64::from_le_bytes(bytes[8..16].try_into().ok()?),
        })
    }

    fn timestamp_ms(&self) -> i64 { self.ts_ms }

    fn from_stream_event(_ev: &StreamEvent) -> Option<Self> { None }
}