pragma_common/entries/
volume.rs1#[cfg(feature = "proto")]
2use prost::Message;
3
4use crate::{InstrumentType, Pair};
5#[cfg(feature = "proto")]
6use crate::{ProtoDeserialize, ProtoSerialize};
7
8#[derive(Debug, Clone, PartialEq)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10#[cfg_attr(
11 feature = "borsh",
12 derive(borsh::BorshSerialize, borsh::BorshDeserialize)
13)]
14#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
15pub struct VolumeEntry {
16 pub source: String,
17 pub instrument_type: InstrumentType,
18 pub pair: Pair,
19 pub volume_daily: f64,
20 pub timestamp_ms: i64,
21}
22
23#[cfg(feature = "proto")]
24impl VolumeEntry {
25 fn to_proto(&self) -> crate::schema::VolumeEntry {
26 crate::schema::VolumeEntry {
27 source: self.source.clone(),
28 instrument_type: match self.instrument_type {
29 InstrumentType::Spot => crate::schema::InstrumentType::Spot as i32,
30 InstrumentType::Perp => crate::schema::InstrumentType::Perp as i32,
31 },
32 pair: Some(crate::schema::Pair {
33 base: self.pair.base.clone(),
34 quote: self.pair.quote.clone(),
35 }),
36 volume_daily: self.volume_daily,
37 timestamp_ms: self.timestamp_ms,
38 }
39 }
40
41 fn from_proto(proto: crate::schema::VolumeEntry) -> Result<Self, prost::DecodeError> {
42 let pair = proto
43 .pair
44 .ok_or_else(|| prost::DecodeError::new("Missing pair field in VolumeEntry"))?;
45
46 let instrument_type = match proto.instrument_type {
47 x if x == crate::schema::InstrumentType::Spot as i32 => InstrumentType::Spot,
48 x if x == crate::schema::InstrumentType::Perp as i32 => InstrumentType::Perp,
49 _ => {
50 return Err(prost::DecodeError::new(format!(
51 "Invalid instrument_type value: {}",
52 proto.instrument_type,
53 )))
54 }
55 };
56
57 Ok(VolumeEntry {
58 source: proto.source,
59 instrument_type,
60 pair: Pair {
61 base: pair.base,
62 quote: pair.quote,
63 },
64 volume_daily: proto.volume_daily,
65 timestamp_ms: proto.timestamp_ms,
66 })
67 }
68}
69
70#[cfg(feature = "proto")]
71impl ProtoSerialize for VolumeEntry {
72 fn to_proto_bytes(&self) -> Vec<u8> {
73 let proto = self.to_proto();
74 let mut buf = Vec::new();
75 proto
76 .encode(&mut buf)
77 .expect("Failed to encode VolumeEntry to protobuf");
78 buf
79 }
80}
81
82#[cfg(feature = "proto")]
83impl ProtoDeserialize for VolumeEntry {
84 fn from_proto_bytes(bytes: &[u8]) -> Result<Self, prost::DecodeError> {
85 let proto = crate::schema::VolumeEntry::decode(bytes)?;
86 Self::from_proto(proto)
87 }
88}