Skip to main content

flm_core/progress/
mod.rs

1//! The progress sheet of a build: what is being done and how far it is, the
2//! request to stop, and the log lines of the work.
3//!
4//! A host (the code that starts a build and shows its progress: a program, a
5//! preview, a site generator) creates a sheet with [`Sheet::start`] and gets
6//! two halves:
7//!
8//! - the [`Sheet`], the host's half, which reads and controls: it takes a
9//!   [`Snapshot`] of the rows whenever the host chooses, and asks the build to
10//!   stop ([`Sheet::cancel`]);
11//! - the root row as a [`Task`], the work's half, whose [`Scope`] the host lends
12//!   to the work, and which the host ends when the build is over.
13//!
14//! The work writes the sheet and never reads it. The one thing the work reads
15//! is whether the build was asked to stop.
16//!
17//! # Rows
18//!
19//! The sheet is a tree of rows, one row for each piece of work a person may
20//! wait for: a stage, a program run, a conversion, a network fetch. Copying
21//! one of many small files is not a row, but an increment of a row's count.
22//! Each row has an id ([`RowId`]), a parent (the row under which it was
23//! declared, except for the root), a [`Kind`] with an optional title, an
24//! optional label (the file, the citation key), a [`RowState`], an optional
25//! [`Count`], and the times at which it started and ended when the host's
26//! [`Observer`] gave them.
27//!
28//! Three rules make reading the sheet from time to time lose nothing: a row is
29//! never removed; a state only moves forward; the number done only grows, and
30//! the total never changes.
31//!
32//! # The work's handles
33//!
34//! Four types, so that each rule holds by the shape of the interface:
35//!
36//! - [`Scope`], lent to the code that works under a running row: it declares
37//!   rows under that row, asks whether the build is cancelled, and writes log
38//!   lines. It cannot start, count or end anything.
39//! - [`Pending`], a declared row: [`start`](Pending::start) gives a `Task`,
40//!   [`start_counted`](Pending::start_counted) a `Counted`. Dropped, it ends
41//!   as [`RowState::NotRun`].
42//! - [`Task`], a running row with nothing to count, and [`Counted`], a running
43//!   row with a count, which [`advance`](Counted::advance)s. Each gives its
44//!   scope and is ended by its owner with `finish`; dropped without it, by an
45//!   early return or a panic, it ends as [`RowState::Failed`].
46//!
47//! A total is therefore given once, when the row starts, before any
48//! increment, and only the owner of a row ends it:
49//!
50//! ```
51//! use flm_core::progress::{Kind, Observer, RowState, Scope, Sheet, Unit};
52//!
53//! const BUILD: Kind = Kind::new("example.build");
54//! const IMAGES: Kind = Kind::titled("example.images", "Converting images");
55//!
56//! fn convert(scope: &Scope, images: &[&str]) {
57//!     let row = scope.child(&IMAGES, None).start_counted(Unit::Items, images.len() as u64);
58//!     for _image in images {
59//!         if scope.cancelled() {
60//!             return; // `row` is dropped, and ends as failed
61//!         }
62//!         // ... convert the image ...
63//!         row.advance(1);
64//!     }
65//!     row.finish();
66//! }
67//!
68//! let (sheet, root) = Sheet::start(&BUILD, None, Observer::default(), None);
69//! convert(&root.scope(), &["a.pdf", "b.pdf"]);
70//! root.finish();
71//!
72//! let snapshot = sheet.snapshot();
73//! assert_eq!(snapshot.rows[1].state, RowState::Finished);
74//! assert_eq!(snapshot.rows[1].count.map(|count| count.done), Some(2));
75//! ```
76//!
77//! **Children of a counted row.** A row declared under a row that has a count
78//! stands for one of the counted units, shown in detail: under "3 of 12
79//! trees", a row "`intro.flm`: 7 of 20 images". The owner of the counted row
80//! advances it when that unit is done. A row declared under a row with no
81//! count is one part of that row's work.
82//!
83//! **Possible work is declared in advance.** Where an owner knows that a piece
84//! of work may be needed, it declares the row before it knows, and drops the
85//! [`Pending`] when the work is not needed. A row may still be declared at any
86//! time while its parent runs, for work that could not be foreseen.
87//!
88//! # Handles with no row
89//!
90//! A handle may have no row behind it; its row operations then do nothing, and
91//! the work calls them all the same, with no test of its own:
92//!
93//! - [`Scope::none()`] is the scope of a build that reports nowhere.
94//! - Past a depth limit ([`Pending::with_depth_limit`],
95//!   [`Scope::with_depth_limit`]), a handle has no row, but still answers
96//!   [`Scope::cancelled`] and still writes log lines, which are attributed to
97//!   the nearest row that exists. A host chooses with the limit how much
98//!   detail it wants.
99//! - A row declared under a row that has ended has no row behind it.
100//!
101//! # The observer and the log sink
102//!
103//! The host gives two optional things when it starts a sheet, and nothing
104//! else of the host's is ever called. The [`Observer`] holds three optional
105//! functions: one called when a row starts, one when it ends (each returns
106//! the time to store on the row, from the host's clock), and one before each
107//! question about the cancellation; none is called when a count advances. The
108//! [`LogSink`] receives the log lines that the work writes through its scopes
109//! ([`Scope::log`]). The sheet reads no clock of its own and never computes
110//! with the times.
111//!
112//! # Costs
113//!
114//! - [`Counted::advance`] is one atomic addition: no lock, no allocation, no
115//!   clock, no call to the host.
116//! - Any operation on a handle with no row is one test of that fact: no
117//!   allocation, no formatting, no lock.
118//! - Declaring a row costs one allocation for the row, one more for the copy
119//!   of its label when it has one, and a short lock; starting and ending it
120//!   each cost one test and, when the host set that function of the observer,
121//!   one call of it, made outside any lock.
122//! - [`Scope::cancelled`] costs one read of the flag, one test and, when the
123//!   host set a checkpoint, one call of it, also past a depth limit.
124//! - Passing a [`Kind`] copies no text; a label is copied only when a row is
125//!   behind the handle.
126//!
127//! The module uses `std::sync` only and reads no clock, so it builds for
128//! WebAssembly. Every handle is `Send` and `Sync`.
129//!
130//! # The estimate of the overall percentage
131//!
132//! The sheet holds facts only. The percentage of a build is an estimate, made
133//! by the host with two functions that keep no memory and read no clock:
134//! [`estimate`] weighs the rows of a snapshot by a table of expected durations
135//! ([`Expected`]), which [`measure`] reads from the last snapshot of an
136//! earlier build.
137
138mod estimate;
139mod handles;
140mod kind;
141mod log;
142mod sheet;
143
144pub use estimate::{estimate, measure, Expected, KindDurations};
145pub use handles::{Counted, Pending, Scope, Task};
146pub use kind::{Kind, SharedText};
147pub use log::{LogLevel, LogRecord, LogSink};
148pub use sheet::{
149    CheckpointFn, Count, Observer, RowFn, RowId, RowSnapshot, RowState, Sheet, Snapshot, Unit,
150};