crux_time/protocol/
mod.rs

1#[cfg(feature = "chrono")]
2pub mod chrono;
3pub mod duration;
4pub mod instant;
5
6use crux_core::capability::Operation;
7use serde::{Deserialize, Serialize};
8
9use duration::Duration;
10use instant::Instant;
11
12#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
13#[serde(rename_all = "camelCase")]
14pub enum TimeRequest {
15    Now,
16    NotifyAt { id: TimerId, instant: Instant },
17    NotifyAfter { id: TimerId, duration: Duration },
18    Clear { id: TimerId },
19}
20
21#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
22pub struct TimerId(pub usize);
23
24#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
25#[serde(rename_all = "camelCase")]
26pub enum TimeResponse {
27    Now { instant: Instant },
28    InstantArrived { id: TimerId },
29    DurationElapsed { id: TimerId },
30    Cleared { id: TimerId },
31}
32
33impl Operation for TimeRequest {
34    type Output = TimeResponse;
35
36    #[cfg(feature = "typegen")]
37    fn register_types(generator: &mut crux_core::typegen::TypeGen) -> crux_core::typegen::Result {
38        generator.register_type::<Instant>()?;
39        generator.register_type::<Duration>()?;
40        generator.register_type::<Self>()?;
41        generator.register_type::<Self::Output>()?;
42        Ok(())
43    }
44}