Skip to main content

printobserver_core/
kinds.rs

1//! The event kinds and source names the supervision domain writes.
2//!
3//! Each kind here is one the supervision loop, the action chokepoint or the
4//! expiry sweep raises itself, declared under a [`KIND`](EventPayload::KIND)
5//! of its own and written down through the envelope
6//! [`printobserver_types::event`] declares. Nothing outside this crate names
7//! these kinds to write them; a reader elsewhere reads one back through
8//! [`EventRecord::payload_as`](printobserver_types::EventRecord::payload_as)
9//! and is answered nothing for a record of another kind.
10//!
11//! The three source names are the actors this domain writes events on behalf
12//! of: the supervisor itself, a person, and the supervising agent.
13
14use crate::records::{
15    AcknowledgementDisposition, ActionId, Actor, InterventionId, InterventionOutcome,
16    PolicyDecision, PrintAction,
17};
18use printobserver_printer_api::Adjustable;
19use printobserver_supervisor_api::AgentAssessment;
20use printobserver_types::contract::Sample;
21use printobserver_types::schemars::JsonSchema;
22use printobserver_types::serde::{Deserialize, Serialize};
23use printobserver_types::{EventId, EventPayload, EventSource};
24
25/// The source name of an event the supervisor raised itself.
26pub const SYSTEM_SOURCE: &str = "system";
27
28/// The source name of an event a person raised.
29pub const OPERATOR_SOURCE: &str = "operator";
30
31/// The source name of an event the supervising agent raised.
32pub const AGENT_SOURCE: &str = "agent";
33
34/// The source of an event the supervisor raised itself.
35#[must_use]
36pub fn system_source() -> EventSource {
37    EventSource::new(SYSTEM_SOURCE)
38}
39
40/// The source of an event a person raised.
41#[must_use]
42pub fn operator_source() -> EventSource {
43    EventSource::new(OPERATOR_SOURCE)
44}
45
46/// The source of an event the supervising agent raised.
47#[must_use]
48pub fn agent_source() -> EventSource {
49    EventSource::new(AGENT_SOURCE)
50}
51
52/// An actor asked for an action.
53#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
54#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
55#[schemars(crate = "printobserver_types::schemars")]
56pub struct ActionRequestedPayload {
57    /// The action's identifier.
58    pub action_id: ActionId,
59    /// What was asked for.
60    pub action: PrintAction,
61    /// Who asked.
62    pub actor: Actor,
63}
64
65impl EventPayload for ActionRequestedPayload {
66    const KIND: &'static str = "action_requested";
67}
68
69impl Sample for ActionRequestedPayload {
70    fn sample_full() -> Self {
71        Self {
72            action_id: ActionId::sample_full(),
73            action: PrintAction::sample_full(),
74            actor: Actor::sample_full(),
75        }
76    }
77
78    fn sample_minimal() -> Self {
79        Self {
80            action: PrintAction::sample_minimal(),
81            ..Self::sample_full()
82        }
83    }
84}
85
86/// An accepted action reached the printer.
87#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
88#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
89#[schemars(crate = "printobserver_types::schemars")]
90pub struct ActionExecutedPayload {
91    /// The action's identifier.
92    pub action_id: ActionId,
93    /// The bounded intervention it opened, when it opened one.
94    #[serde(default, skip_serializing_if = "Option::is_none")]
95    pub intervention_id: Option<InterventionId>,
96}
97
98impl EventPayload for ActionExecutedPayload {
99    const KIND: &'static str = "action_executed";
100}
101
102impl Sample for ActionExecutedPayload {
103    fn sample_full() -> Self {
104        Self {
105            action_id: ActionId::sample_full(),
106            intervention_id: Some(InterventionId::sample_full()),
107        }
108    }
109
110    fn sample_minimal() -> Self {
111        Self {
112            action_id: ActionId::sample_full(),
113            intervention_id: None,
114        }
115    }
116}
117
118/// Policy refused an action.
119#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
120#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
121#[schemars(crate = "printobserver_types::schemars")]
122pub struct ActionRejectedPayload {
123    /// The action's identifier.
124    pub action_id: ActionId,
125    /// The whole decision, carrying which rejection it was.
126    pub decision: PolicyDecision,
127}
128
129impl EventPayload for ActionRejectedPayload {
130    const KIND: &'static str = "action_rejected";
131}
132
133impl Sample for ActionRejectedPayload {
134    fn sample_full() -> Self {
135        Self {
136            action_id: ActionId::sample_full(),
137            decision: PolicyDecision::sample_full(),
138        }
139    }
140}
141
142/// A bounded intervention expired.
143#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
144#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
145#[schemars(crate = "printobserver_types::schemars")]
146pub struct InterventionExpiredPayload {
147    /// The intervention's identifier.
148    pub intervention_id: InterventionId,
149    /// What it had changed.
150    pub adjustable: Adjustable,
151    /// What became of it.
152    pub outcome: InterventionOutcome,
153}
154
155impl EventPayload for InterventionExpiredPayload {
156    const KIND: &'static str = "intervention_expired";
157}
158
159impl Sample for InterventionExpiredPayload {
160    fn sample_full() -> Self {
161        Self {
162            intervention_id: InterventionId::sample_full(),
163            adjustable: Adjustable::Feedrate,
164            outcome: InterventionOutcome::Restored,
165        }
166    }
167}
168
169/// The agent wrote down what it made of a turn.
170#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
171#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
172#[schemars(crate = "printobserver_types::schemars")]
173pub struct AgentAssessmentPayload {
174    /// The session the turn ran in.
175    pub session_name: String,
176    /// What the agent answered with.
177    pub assessment: AgentAssessment,
178}
179
180impl EventPayload for AgentAssessmentPayload {
181    const KIND: &'static str = "agent_assessment";
182}
183
184impl Sample for AgentAssessmentPayload {
185    fn sample_full() -> Self {
186        Self {
187            session_name: "print-0191f0a0".to_owned(),
188            assessment: AgentAssessment::sample_full(),
189        }
190    }
191}
192
193/// An operator acknowledged an event.
194#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
195#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
196#[schemars(crate = "printobserver_types::schemars")]
197pub struct OperatorAcknowledgementPayload {
198    /// The event being acknowledged.
199    pub acknowledged_event_id: EventId,
200    /// What the operator asked for next.
201    pub disposition: AcknowledgementDisposition,
202}
203
204impl EventPayload for OperatorAcknowledgementPayload {
205    const KIND: &'static str = "operator_acknowledgement";
206}
207
208impl Sample for OperatorAcknowledgementPayload {
209    fn sample_full() -> Self {
210        Self {
211            acknowledged_event_id: EventId::sample_full(),
212            disposition: AcknowledgementDisposition::Watch,
213        }
214    }
215}
216
217/// Where a port failed while an event was being handled.
218///
219/// A closed set of exactly the sites at which a failure has nowhere else to be
220/// recorded. The printer's action methods record theirs on the
221/// [`ActionRecord`](crate::records::ActionRecord) the request minted, and a restoring
222/// call records its own on the [`Intervention`](crate::records::Intervention) it was
223/// expiring; those are not sites here, because a second record of them would be
224/// a second version of one fact.
225#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
226#[serde(
227    crate = "printobserver_types::serde",
228    rename_all = "snake_case",
229    deny_unknown_fields
230)]
231#[schemars(crate = "printobserver_types::schemars")]
232pub enum PortFailureSite {
233    /// Reading the printer's own state.
234    PrinterSnapshot,
235    /// Reading the job the printer reports it is running.
236    PrinterJob,
237    /// Writing the image the event arrived with.
238    ImageWrite,
239    /// Running the supervision turn the event prompted.
240    SupervisionTurn,
241}
242
243impl Sample for PortFailureSite {
244    fn sample_full() -> Self {
245        Self::PrinterSnapshot
246    }
247}
248
249/// A port failed while one event was being handled.
250///
251/// The event is named rather than implied, so that a reader holding an event's
252/// identifier reaches every failure recorded while that event was being
253/// handled. A failure recorded here is one the handling survived: the event is
254/// already in the history by the time any of these sites is reached, and the
255/// loop goes on to handle the next event.
256#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
257#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
258#[schemars(crate = "printobserver_types::schemars")]
259pub struct PortFailurePayload {
260    /// The event whose handling reached the failing call.
261    pub event_id: EventId,
262    /// Where it failed.
263    pub site: PortFailureSite,
264    /// What the port said about it, in the port's own words.
265    pub detail: String,
266}
267
268impl EventPayload for PortFailurePayload {
269    const KIND: &'static str = "port_failure";
270}
271
272impl Sample for PortFailurePayload {
273    fn sample_full() -> Self {
274        Self {
275            event_id: EventId::sample_full(),
276            site: PortFailureSite::sample_full(),
277            detail: "the printer is unreachable: connection refused".to_owned(),
278        }
279    }
280}