use crate::error::Result;
use crate::server::channel::Channel;
#[derive(Debug, Clone, Default)]
pub struct ClockInstallOptions {
pub time: Option<u64>,
}
#[derive(Clone)]
pub struct Clock {
channel: Channel,
}
impl std::fmt::Debug for Clock {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Clock").finish_non_exhaustive()
}
}
impl Clock {
pub fn new(channel: Channel) -> Self {
Self { channel }
}
pub async fn install(&self, options: Option<ClockInstallOptions>) -> Result<()> {
let mut params = serde_json::json!({});
if let Some(opts) = options
&& let Some(time) = opts.time
{
params["timeNumber"] = serde_json::Value::Number(time.into());
}
self.channel.send_no_result("clockInstall", params).await
}
pub async fn fast_forward(&self, ticks: u64) -> Result<()> {
self.channel
.send_no_result(
"clockFastForward",
serde_json::json!({ "ticksNumber": ticks }),
)
.await
}
pub async fn pause_at(&self, time: u64) -> Result<()> {
self.channel
.send_no_result("clockPauseAt", serde_json::json!({ "timeNumber": time }))
.await
}
pub async fn resume(&self) -> Result<()> {
self.channel
.send_no_result("clockResume", serde_json::json!({}))
.await
}
pub async fn set_fixed_time(&self, time: u64) -> Result<()> {
self.channel
.send_no_result(
"clockSetFixedTime",
serde_json::json!({ "timeNumber": time }),
)
.await
}
pub async fn set_system_time(&self, time: u64) -> Result<()> {
self.channel
.send_no_result(
"clockSetSystemTime",
serde_json::json!({ "timeNumber": time }),
)
.await
}
}