Skip to main content

armillae_simulate/
protocol.rs

1use std::collections::BTreeSet;
2
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6use crate::{
7    BackendId, CapabilityId, Clock, ClockInstanceId, ClockTypeId, ExecuteEntryId, ModuleId,
8    SemanticVersion, SystemId, VersionRequirement,
9};
10
11pub const SIMULATE_API_VERSION: &str = "armillae.simulate/v1alpha1";
12
13#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
14pub struct ExecuteRequest {
15    pub entry: ExecuteEntryId,
16    pub input: serde_json::Value,
17}
18
19#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
20pub struct ExecuteOutcome {
21    pub entry: ExecuteEntryId,
22    pub output: Option<serde_json::Value>,
23}
24
25#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
26pub struct ClockKey {
27    pub clock_type: ClockTypeId,
28    pub instance: ClockInstanceId,
29}
30
31#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
32pub struct ClockState {
33    pub key: ClockKey,
34    pub value: serde_json::Value,
35}
36
37#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
38pub struct AdvanceTarget {
39    pub instance: ClockInstanceId,
40    pub step: serde_json::Value,
41}
42
43#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
44pub struct AdvanceRequest {
45    pub clock_type: ClockTypeId,
46    #[serde(default)]
47    pub targets: Vec<AdvanceTarget>,
48}
49
50#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
51pub struct ClockTransition {
52    pub instance: ClockInstanceId,
53    pub before: serde_json::Value,
54    pub step: serde_json::Value,
55    pub after: serde_json::Value,
56}
57
58#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
59pub struct AdvanceOutcome {
60    pub clock_type: ClockTypeId,
61    #[serde(default)]
62    pub transitions: Vec<ClockTransition>,
63}
64
65#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
66pub struct ModuleDescriptor {
67    pub api_version: String,
68    pub id: ModuleId,
69    pub version: SemanticVersion,
70    #[serde(default)]
71    pub dependencies: Vec<ModuleDependency>,
72    pub execution: ExecutionPlane,
73    #[serde(default)]
74    pub required_capabilities: BTreeSet<CapabilityId>,
75    #[serde(default)]
76    pub execute_entries: Vec<ExecuteEntryDefinition>,
77    #[serde(default)]
78    pub clocks: Vec<ClockDefinition>,
79    #[serde(default)]
80    pub systems: Vec<SystemDefinition>,
81}
82
83#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
84pub struct ModuleDependency {
85    pub id: ModuleId,
86    pub version: VersionRequirement,
87}
88
89#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
90#[serde(tag = "type", rename_all = "snake_case")]
91#[non_exhaustive]
92pub enum ExecutionPlane {
93    Native {
94        backend: BackendId,
95        adapter: VersionRequirement,
96    },
97    Hosted,
98}
99
100#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
101pub struct ExecuteEntryDefinition {
102    pub id: ExecuteEntryId,
103    pub input_schema: serde_json::Value,
104    pub output_schema: Option<serde_json::Value>,
105}
106
107impl ExecuteEntryDefinition {
108    pub fn for_input<I: JsonSchema>(id: ExecuteEntryId) -> Self {
109        Self {
110            id,
111            input_schema: schema_value::<I>(),
112            output_schema: None,
113        }
114    }
115
116    pub fn for_input_output<I: JsonSchema, O: JsonSchema>(id: ExecuteEntryId) -> Self {
117        Self {
118            id,
119            input_schema: schema_value::<I>(),
120            output_schema: Some(schema_value::<O>()),
121        }
122    }
123}
124
125#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
126pub struct ClockDefinition {
127    pub id: ClockTypeId,
128    pub value_schema: serde_json::Value,
129    pub step_schema: serde_json::Value,
130}
131
132impl ClockDefinition {
133    pub fn for_clock<C: Clock>(id: ClockTypeId) -> Self {
134        Self {
135            id,
136            value_schema: schema_value::<C>(),
137            step_schema: schema_value::<C::Step>(),
138        }
139    }
140}
141
142fn schema_value<T: JsonSchema>() -> serde_json::Value {
143    schemars::schema_for!(T).to_value()
144}
145
146#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
147#[serde(tag = "type", rename_all = "snake_case")]
148#[non_exhaustive]
149pub enum SystemTrigger {
150    Execute { entry: ExecuteEntryId },
151    Advance { clock_type: ClockTypeId },
152}
153
154#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
155pub struct SystemDefinition {
156    pub id: SystemId,
157    pub trigger: SystemTrigger,
158    #[serde(default)]
159    pub before: Vec<SystemId>,
160    #[serde(default)]
161    pub after: Vec<SystemId>,
162}
163
164#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
165pub struct EngineInfo {
166    pub name: String,
167    pub version: SemanticVersion,
168}
169
170#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
171pub struct BackendInfo {
172    pub id: BackendId,
173    pub adapter_version: SemanticVersion,
174    pub engine: Option<EngineInfo>,
175}
176
177#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
178pub struct SimulationCapabilities {
179    pub backend: BackendInfo,
180    #[serde(default)]
181    pub supported: BTreeSet<CapabilityId>,
182}
183
184impl SimulationCapabilities {
185    pub fn supports(&self, capability: &CapabilityId) -> bool {
186        self.supported.contains(capability)
187    }
188}