rusty_time_clock/
virtual_driver.rs1use crate::{ClockDrive, ClockError};
6use rusty_time_core::{ClockCommand, VirtualClock};
7
8#[derive(Debug, Default)]
9pub struct VirtualDriver {
10 vclock: VirtualClock,
11 stepped: f64,
13 freq_ppm: f64,
15}
16
17impl VirtualDriver {
18 pub fn new() -> Self {
19 Self::default()
20 }
21
22 pub fn vclock(&mut self) -> &mut VirtualClock {
25 &mut self.vclock
26 }
27
28 pub fn freq_ppm(&self) -> f64 {
29 self.freq_ppm
30 }
31
32 pub fn stepped_total(&self) -> f64 {
33 self.stepped
34 }
35
36 pub fn update_measurement(
39 &mut self,
40 mono: f64,
41 offset: f64,
42 skew_ppm: Option<f64>,
43 error_bound: f64,
44 ) {
45 self.vclock.update(mono, offset, skew_ppm, error_bound);
46 }
47}
48
49impl ClockDrive for VirtualDriver {
50 fn apply(&mut self, cmd: &ClockCommand) -> Result<(), ClockError> {
51 match *cmd {
52 ClockCommand::Step { add_seconds } => {
53 self.stepped += add_seconds;
54 Ok(())
55 }
56 ClockCommand::Slew { freq_ppm, .. } => {
57 self.freq_ppm = freq_ppm;
60 Ok(())
61 }
62 }
63 }
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn commands_are_absorbed() {
72 let mut d = VirtualDriver::new();
73 d.apply(&ClockCommand::Step { add_seconds: 0.5 })
74 .expect("step");
75 d.apply(&ClockCommand::Slew {
76 freq_ppm: -12.0,
77 drain_offset: 0.001,
78 drain_rate_ppm: 500.0,
79 })
80 .expect("slew");
81 assert!((d.stepped_total() - 0.5).abs() < 1e-12);
82 assert!((d.freq_ppm() + 12.0).abs() < 1e-12);
83 }
84}