binex/message/record/solutions/frame/
temporal.rs1use crate::{utils::Utils, Error};
3
4#[derive(Debug, Clone, PartialEq)]
5pub struct TemporalSolution {
6 pub offset_s: f64,
8 pub drift_s_s: Option<f64>,
10}
11
12impl TemporalSolution {
13 pub(crate) fn encoding_size(&self) -> usize {
14 if self.drift_s_s.is_some() {
15 16
16 } else {
17 8
18 }
19 }
20 pub(crate) fn encode(&self, big_endian: bool, buf: &mut [u8]) -> Result<usize, Error> {
21 let size = self.encoding_size();
22 if buf.len() < size {
23 return Err(Error::NotEnoughBytes);
24 }
25
26 let bytes = if big_endian {
27 self.offset_s.to_be_bytes()
28 } else {
29 self.offset_s.to_le_bytes()
30 };
31
32 buf[..8].copy_from_slice(&bytes);
33
34 if let Some(drift) = self.drift_s_s {
35 let bytes = if big_endian {
36 drift.to_be_bytes()
37 } else {
38 drift.to_le_bytes()
39 };
40 buf[8..16].copy_from_slice(&bytes);
41 }
42
43 Ok(size)
44 }
45
46 pub(crate) fn decode_without_drift(big_endian: bool, buf: &[u8]) -> Result<Self, Error> {
47 if buf.len() < 8 {
48 return Err(Error::NotEnoughBytes);
49 }
50 let offset_s = Utils::decode_f64(big_endian, buf)?;
51 Ok(Self {
52 offset_s,
53 drift_s_s: None,
54 })
55 }
56
57 pub(crate) fn decode_with_drift(big_endian: bool, buf: &[u8]) -> Result<Self, Error> {
58 if buf.len() < 16 {
59 return Err(Error::NotEnoughBytes);
60 }
61 let offset_s = Utils::decode_f64(big_endian, buf)?;
62 let drift_s_s = Utils::decode_f64(big_endian, &buf[8..])?;
63 Ok(Self {
64 offset_s,
65 drift_s_s: Some(drift_s_s),
66 })
67 }
68}