Skip to main content

digdigdig3_station/data/
volatility_index.rs

1use digdigdig3::core::types::StreamEvent;
2use serde::{Deserialize, Serialize};
3
4use crate::series::DataPoint;
5
6/// 24 B record (LE):
7///   i64 ts_ms (8), f64 value (8), f64 _pad (8, NaN)
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct VolatilityIndexPoint {
10    pub ts_ms: i64,
11    pub value: f64,
12}
13
14const SIZE: usize = 24;
15
16impl DataPoint for VolatilityIndexPoint {
17    const RECORD_SIZE: usize = SIZE;
18
19    fn encode(&self, out: &mut [u8]) {
20        out[0..8].copy_from_slice(&(self.ts_ms as u64).to_le_bytes());
21        out[8..16].copy_from_slice(&self.value.to_le_bytes());
22        out[16..24].copy_from_slice(&f64::NAN.to_le_bytes());
23    }
24
25    fn decode(bytes: &[u8]) -> Option<Self> {
26        if bytes.len() != SIZE { return None; }
27        Some(Self {
28            ts_ms: u64::from_le_bytes(bytes[0..8].try_into().ok()?) as i64,
29            value: f64::from_le_bytes(bytes[8..16].try_into().ok()?),
30        })
31    }
32
33    fn timestamp_ms(&self) -> i64 { self.ts_ms }
34
35    fn from_stream_event(ev: &StreamEvent) -> Option<Self> {
36        if let StreamEvent::VolatilityIndex { symbol: _, value, timestamp } = ev {
37            Some(Self { ts_ms: *timestamp, value: *value })
38        } else {
39            None
40        }
41    }
42}