use crate::{Duration, Timestamp};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ClockError {
PermissionDenied,
NoDevice,
NotSupported,
InvalidValue,
Unknown,
}
impl core::fmt::Display for ClockError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
ClockError::PermissionDenied => {
f.write_str("Insufficient permissions to perform the requested operation.")
}
ClockError::NoDevice => {
f.write_str("The underlying clock device is no longer available")
}
ClockError::NotSupported => {
f.write_str("The requested operation is not supported by the clock or OS.")
}
ClockError::InvalidValue => f.write_str("One of the provided values is invalid."),
ClockError::Unknown => {
f.write_str("An unknown error occured when interacting with the clock.")
}
}
}
}
impl core::error::Error for ClockError {}
pub trait Clock<Timescale>: Clone + Send + 'static {
fn now(&self) -> Result<Timestamp<Timescale>, ClockError>;
fn set_frequency(&self, freq: f64) -> Result<Timestamp<Timescale>, ClockError>;
fn get_frequency(&self) -> Result<f64, ClockError>;
fn max_frequency(&self) -> Result<f64, ClockError>;
fn step_clock(&self, offset: Duration) -> Result<Timestamp<Timescale>, ClockError>;
fn error_estimate_update(
&self,
est_error: Duration,
max_error: Duration,
) -> Result<(), ClockError>;
fn leap_update(&self, leap_status: LeapStatus) -> Result<(), ClockError>;
fn synchronization_update(&self, synchronized: bool) -> Result<(), ClockError>;
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum LeapStatus {
#[default]
None,
Leap59,
Leap61,
}