printobserver_core/
decision.rs1use crate::records::{
14 ActionKind, Actor, ActorClass, EffectiveBounds, PolicyDecision, PrintAction, PrintRecord,
15 RejectionReason, SafetyEnvelope,
16};
17use core::time::Duration;
18use printobserver_printer_api::{Adjustable, PrinterState};
19
20use printobserver_types::Timestamp;
21
22#[derive(Debug, Clone, Copy)]
24pub struct DecisionInput<'a> {
25 pub action: &'a PrintAction,
27 pub actor: &'a Actor,
29 pub requested_at: Timestamp,
31 pub print: Option<&'a PrintRecord>,
33 pub printer_state: Option<&'a PrinterState>,
42 pub bounds: &'a EffectiveBounds,
44 pub envelope: &'a SafetyEnvelope,
46 pub since_last_agent_action: Option<Duration>,
52}
53
54#[must_use]
59pub fn adjustment(action: &PrintAction) -> Option<(Adjustable, f64)> {
60 match action {
61 PrintAction::SetFeedrateFactor { factor, .. } => Some((Adjustable::Feedrate, *factor)),
62 PrintAction::SetFlowrateFactor { factor, .. } => Some((Adjustable::Flowrate, *factor)),
63 PrintAction::SetToolTargetC { tool, target_c, .. } => {
64 Some((Adjustable::ToolTarget { tool: *tool }, *target_c))
65 }
66 PrintAction::SetBedTargetC { target_c, .. } => Some((Adjustable::BedTarget, *target_c)),
67 PrintAction::SetFanPercent { percent, .. } => Some((Adjustable::Fan, *percent)),
68 PrintAction::Pause { .. }
69 | PrintAction::Resume { .. }
70 | PrintAction::Cancel { .. }
71 | PrintAction::StartPrint { .. }
72 | PrintAction::AcknowledgeFailure { .. } => None,
73 }
74}
75
76#[must_use]
82pub fn valid_from(kind: ActionKind, state: &PrinterState) -> bool {
83 match kind {
84 ActionKind::Pause => matches!(state, PrinterState::Printing),
85 ActionKind::Resume => matches!(state, PrinterState::Paused),
86 ActionKind::Cancel => matches!(state, PrinterState::Printing | PrinterState::Paused),
87 ActionKind::StartPrint => matches!(state, PrinterState::Operational),
88 ActionKind::SetFeedrateFactor
89 | ActionKind::SetFlowrateFactor
90 | ActionKind::SetToolTargetC
91 | ActionKind::SetBedTargetC
92 | ActionKind::SetFanPercent => {
93 matches!(state, PrinterState::Printing | PrinterState::Paused)
94 }
95 ActionKind::AcknowledgeFailure
96 | ActionKind::RegisterSpool
97 | ActionKind::LoadSpool
98 | ActionKind::UnloadSpool
99 | ActionKind::AdjustSpool
100 | ActionKind::RetireSpool => true,
101 }
102}
103
104#[must_use]
114pub const fn changes_the_machine(kind: ActionKind) -> bool {
115 !matches!(
116 kind,
117 ActionKind::AcknowledgeFailure
118 | ActionKind::RegisterSpool
119 | ActionKind::LoadSpool
120 | ActionKind::UnloadSpool
121 | ActionKind::AdjustSpool
122 | ActionKind::RetireSpool
123 )
124}
125
126fn is_active(print: Option<&PrintRecord>) -> bool {
128 print.is_some_and(|record| record.ended_at.is_none())
129}
130
131#[must_use]
137pub fn decide(input: &DecisionInput<'_>) -> PolicyDecision {
138 let kind = input.action.kind();
139 let actor_class = input.actor.class();
140
141 if !is_active(input.print) {
142 return PolicyDecision::Rejected(RejectionReason::NoActivePrint);
143 }
144
145 let granted = input.envelope.actions.get(&actor_class);
146 if !granted.is_some_and(|kinds| kinds.contains(&kind)) {
147 return PolicyDecision::Rejected(RejectionReason::ActorMayNotRequest {
148 actor_class,
149 action: kind,
150 });
151 }
152
153 let adjusting_a_paused_print = adjustment(input.action).is_some()
158 && matches!(input.printer_state, Some(PrinterState::Paused));
159 if actor_class == ActorClass::Agent
160 && changes_the_machine(kind)
161 && !adjusting_a_paused_print
162 && let Some(since_last) = input.since_last_agent_action
163 {
164 let since_last_s = i64::try_from(since_last.as_secs()).unwrap_or(i64::MAX);
165 if since_last_s < input.envelope.agent_min_interval_s {
166 return PolicyDecision::Rejected(RejectionReason::MinIntervalNotElapsed {
167 interval_s: input.envelope.agent_min_interval_s,
168 since_last_s,
169 });
170 }
171 }
172
173 if let Some(state) = input.printer_state
174 && !valid_from(kind, state)
175 {
176 return PolicyDecision::Rejected(RejectionReason::InvalidFromState {
177 state: state.clone(),
178 });
179 }
180
181 if let Some((adjustable, requested)) = adjustment(input.action) {
182 let Some(allowed) = input.bounds.allowed.get(&adjustable) else {
183 return PolicyDecision::Rejected(RejectionReason::UnsupportedAdjustable { adjustable });
184 };
185 if !allowed.contains(requested) {
186 return PolicyDecision::Rejected(RejectionReason::OutOfBounds {
187 adjustable,
188 requested,
189 allowed: *allowed,
190 });
191 }
192 }
193
194 PolicyDecision::Accepted
195}