Skip to main content

armillae_simulate/
clock.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize, de::DeserializeOwned};
3
4use crate::{ClockErrorCode, ClockInstanceId, ClockTypeId};
5
6#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema, thiserror::Error)]
7#[error("clock transition rejected: {code}: {message}")]
8pub struct ClockTransitionError {
9    pub code: ClockErrorCode,
10    pub message: String,
11}
12
13pub trait Clock: Clone + Send + Sync + Serialize + DeserializeOwned + JsonSchema + 'static {
14    type Step: Clone + Send + Sync + Serialize + DeserializeOwned + JsonSchema + 'static;
15
16    fn validate(&self) -> Result<(), ClockTransitionError> {
17        Ok(())
18    }
19
20    fn advance(&self, step: &Self::Step) -> Result<Self, ClockTransitionError>;
21}
22
23#[derive(Clone, Debug, PartialEq)]
24pub struct TypedAdvanceTarget<S> {
25    pub instance: ClockInstanceId,
26    pub step: S,
27}
28
29#[derive(Clone, Debug, PartialEq)]
30pub struct TypedAdvanceRequest<S> {
31    pub targets: Vec<TypedAdvanceTarget<S>>,
32}
33
34#[derive(Clone, Debug, PartialEq)]
35pub struct TypedClockTransition<C: Clock> {
36    pub instance: ClockInstanceId,
37    pub before: C,
38    pub step: C::Step,
39    pub after: C,
40}
41
42#[derive(Clone)]
43pub struct TypedAdvanceOutcome<C: Clock> {
44    pub clock_type: ClockTypeId,
45    pub transitions: Vec<TypedClockTransition<C>>,
46}
47
48impl<C> std::fmt::Debug for TypedAdvanceOutcome<C>
49where
50    C: Clock + std::fmt::Debug,
51    C::Step: std::fmt::Debug,
52{
53    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54        formatter
55            .debug_struct("TypedAdvanceOutcome")
56            .field("clock_type", &self.clock_type)
57            .field("transitions", &self.transitions)
58            .finish()
59    }
60}
61
62impl<C> PartialEq for TypedAdvanceOutcome<C>
63where
64    C: Clock + PartialEq,
65    C::Step: PartialEq,
66{
67    fn eq(&self, other: &Self) -> bool {
68        self.clock_type == other.clock_type && self.transitions == other.transitions
69    }
70}