use std::sync::Mutex;
use printobserver_core::{ActionId, ExecutionOutcome, InterventionOutcome, PolicyDecision};
use printobserver_printer_api::Adjustable;
use printobserver_types::{EventKind, PrintId};
#[derive(Debug, Clone, PartialEq)]
pub enum Call {
OpenPrint,
ReadPrint,
ReadPrintByProviderId,
ReadOpenPrints,
ReadPrints,
AttachObicoPrint(i64),
RecordJobSighting,
EndPrint(String),
RecordNarrowing(Adjustable),
AppendEvent(EventKind),
PutImage,
AppendEventWithImage(EventKind),
ReadImage,
RecordAction(PolicyDecision),
RecordExecution(ExecutionOutcome),
OpenIntervention(Adjustable),
SettleIntervention(InterventionOutcome),
ReadActiveInterventions,
PutManifest,
ReadManifest,
ReadHistory,
ReadAuditPage,
PutSession,
ReadSession,
Snapshot,
Job,
Start(String),
Pause,
Resume,
Cancel,
SetFeedrateFactor(f64),
SetFlowrateFactor(f64),
SetToolTargetC(i64, f64),
SetBedTargetC(f64),
SetFanPercent(f64),
FileFacts(String),
FilamentTypes(String),
PrintedFiles,
ExtrudedTo(String, i64),
Normalize,
FetchImage,
ClearDetection(i64),
RunTurn(PrintId),
CloseSession(PrintId, String),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Port {
Store,
Printer,
Vision,
Supervisor,
}
impl Call {
#[must_use]
pub const fn port(&self) -> Port {
match self {
Self::OpenPrint
| Self::ReadPrint
| Self::ReadPrintByProviderId
| Self::ReadOpenPrints
| Self::ReadPrints
| Self::AttachObicoPrint(_)
| Self::RecordJobSighting
| Self::EndPrint(_)
| Self::RecordNarrowing(_)
| Self::AppendEvent(_)
| Self::PutImage
| Self::AppendEventWithImage(_)
| Self::ReadImage
| Self::RecordAction(_)
| Self::RecordExecution(_)
| Self::OpenIntervention(_)
| Self::SettleIntervention(_)
| Self::ReadActiveInterventions
| Self::PutManifest
| Self::ReadManifest
| Self::ReadHistory
| Self::ReadAuditPage
| Self::PutSession
| Self::ReadSession => Port::Store,
Self::Snapshot
| Self::Job
| Self::Start(_)
| Self::Pause
| Self::Resume
| Self::Cancel
| Self::SetFeedrateFactor(_)
| Self::SetFlowrateFactor(_)
| Self::SetToolTargetC(_, _)
| Self::SetBedTargetC(_)
| Self::SetFanPercent(_)
| Self::FileFacts(_)
| Self::FilamentTypes(_)
| Self::PrintedFiles
| Self::ExtrudedTo(_, _) => Port::Printer,
Self::Normalize | Self::FetchImage | Self::ClearDetection(_) => Port::Vision,
Self::RunTurn(_) | Self::CloseSession(_, _) => Port::Supervisor,
}
}
#[must_use]
pub const fn is_printer_action(&self) -> bool {
matches!(self.port(), Port::Printer)
&& !matches!(
self,
Self::Snapshot
| Self::Job
| Self::FileFacts(_)
| Self::FilamentTypes(_)
| Self::PrintedFiles
| Self::ExtrudedTo(_, _)
)
}
#[must_use]
pub const fn is_store_write(&self) -> bool {
matches!(
self,
Self::OpenPrint
| Self::EndPrint(_)
| Self::RecordNarrowing(_)
| Self::AppendEvent(_)
| Self::PutImage
| Self::RecordAction(_)
| Self::RecordExecution(_)
| Self::OpenIntervention(_)
| Self::SettleIntervention(_)
| Self::PutManifest
| Self::PutSession
)
}
}
#[derive(Debug, Default)]
pub struct Journal {
calls: Mutex<Vec<Call>>,
licences: Mutex<Vec<ActionId>>,
violations: Mutex<Vec<String>>,
}
impl Journal {
pub fn record(&self, call: Call) {
self.calls.lock().expect("the journal holds").push(call);
}
pub fn licence(&self, action_id: ActionId) {
self.licences
.lock()
.expect("the journal holds")
.push(action_id);
}
pub fn spend_licence(&self, what: &str) {
if self
.licences
.lock()
.expect("the journal holds")
.pop()
.is_none()
{
self.violations
.lock()
.expect("the journal holds")
.push(format!(
"{what} reached the printer with no policy decision recorded for it"
));
}
}
#[must_use]
pub fn calls(&self) -> Vec<Call> {
self.calls.lock().expect("the journal holds").clone()
}
#[must_use]
pub fn at(&self, port: Port) -> Vec<Call> {
self.calls()
.into_iter()
.filter(|call| call.port() == port)
.collect()
}
#[must_use]
pub fn printer_actions(&self) -> Vec<Call> {
self.calls()
.into_iter()
.filter(Call::is_printer_action)
.collect()
}
#[must_use]
pub fn store_writes(&self) -> Vec<Call> {
self.calls()
.into_iter()
.filter(Call::is_store_write)
.collect()
}
#[must_use]
pub fn count(&self, call: &Call) -> usize {
self.calls().iter().filter(|seen| *seen == call).count()
}
#[must_use]
pub fn position(&self, call: &Call) -> Option<usize> {
self.calls().iter().position(|seen| seen == call)
}
pub fn clear(&self) {
self.calls.lock().expect("the journal holds").clear();
}
#[must_use]
pub fn violations(&self) -> Vec<String> {
self.violations.lock().expect("the journal holds").clone()
}
pub fn assert_no_violations(&self) {
let violations = self.violations();
assert!(
violations.is_empty(),
"the printer was acted on without a recorded decision: {violations:?}"
);
}
}