Skip to main content

laminapdf_syntax/
job.rs

1//! Jobs ([ADR 0028] §3–§6): the budget a step may spend and what a step returns, shared by
2//! every core crate's jobs (`laminapdf-extract`) and the façade's.
3//!
4//! [ADR 0028]: https://github.com/laudebugs/laminapdf/blob/main/docs/decisions/0028-facade-rules-jobs-and-cancellation.md
5
6/// How much work one step of a job may do, in work units, not time ([ADR 0028] §4): the
7/// core has no clock, so the same budget gives the same step boundaries everywhere. One unit
8/// is one cheap, indivisible piece of work; each job documents its weights. Edges measure
9/// time between steps and adapt the next budget.
10///
11/// [ADR 0028]: https://github.com/laudebugs/laminapdf/blob/main/docs/decisions/0028-facade-rules-jobs-and-cancellation.md
12///
13/// Added in 0.2.0.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15pub struct Budget {
16    units: u32,
17}
18
19impl Budget {
20    /// No limit: the job runs to completion in one step.
21    ///
22    /// Added in 0.2.0.
23    pub const UNLIMITED: Self = Self { units: u32::MAX };
24
25    /// `units` work units (`u32`, a plain number in JS: ADR 0028 F5). A step always does at
26    /// least one unit, so a budget of 0 behaves as 1.
27    ///
28    /// Added in 0.2.0.
29    #[must_use]
30    pub const fn units(units: u32) -> Self {
31        Self { units }
32    }
33
34    /// The number of units.
35    ///
36    /// Added in 0.2.0.
37    #[must_use]
38    pub const fn get(self) -> u32 {
39        self.units
40    }
41}
42
43/// What a step returns: progress, or the job's result (Rust-only; edges expose a concrete
44/// record per job, ADR 0028 F3).
45///
46/// Added in 0.2.0.
47#[derive(Debug, Clone, PartialEq)]
48#[non_exhaustive]
49pub enum Step<T> {
50    /// The job is not done: `done` of `total` units of its current phase (`total` `None`
51    /// when unknown).
52    Progress {
53        /// Units done in this phase.
54        done: u32,
55        /// Units this phase has, when known.
56        total: Option<u32>,
57        /// What the job is doing.
58        phase: Phase,
59    },
60    /// The job finished with its result; stepping again fails with `job-finished`.
61    Done(T),
62}
63
64/// What a job is doing ([ADR 0028] §3).
65///
66/// [ADR 0028]: https://github.com/laudebugs/laminapdf/blob/main/docs/decisions/0028-facade-rules-jobs-and-cancellation.md
67///
68/// Added in 0.2.0.
69#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
70#[non_exhaustive]
71pub enum Phase {
72    /// Reading and parsing objects.
73    Parse,
74    /// Laying out text.
75    Layout,
76    /// Extracting content.
77    Extract,
78    /// Rendering.
79    Render,
80    /// Writing output.
81    Write,
82    /// Signing.
83    Sign,
84    /// Waiting for, or running, a layout model or OCR engine.
85    Model,
86}