qubit-progress 0.5.0

Generic progress reporting abstractions for Qubit Rust libraries
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
# Qubit Progress User Guide

`qubit-progress` is a small progress reporting layer for Rust libraries and
applications. It does not own your work state. Instead, your code keeps domain
state, converts it into progress counters, and sends immutable progress events
to a reporter.

This guide explains how to use the crate, how to choose the right API, and how
to extend it with custom reporters such as GUI progress bars or JSON-lines
reporters.

## Installation

```toml
[dependencies]
qubit-progress = "0.5"
```

If you want to serialize progress events yourself, also add `serde_json` or
another serde-compatible format crate:

```toml
[dependencies]
serde_json = "1"
```

If you use the consumer-based extension examples directly, add `qubit-function`:

```toml
[dependencies]
qubit-function = "0.15"
```

Some concurrent examples in this guide use `qubit-atomic` to avoid explicit
standard-library memory ordering parameters:

```toml
[dependencies]
qubit-atomic = "0.13"
```

## Core Model

A progress stream has five main concepts:

| Concept | Type | Meaning |
| --- | --- | --- |
| Schema | `ProgressSchema` | The metric dictionary for one operation. |
| Metric | `ProgressMetric` | A stable metric id and a display name. |
| Counter | `ProgressCounter` | The current numbers for one metric. |
| Event | `ProgressEvent` | One immutable progress snapshot. |
| Metric snapshot | `ProgressMetricSnapshot` | One event counter flattened with metric metadata and event context. |
| Reporter | `ProgressReporter` | A sink that receives events. |

A `Progress` value ties these concepts together for one logical operation. It
keeps the operation start time, the report interval, the optional stage, and the
reporter reference.

The event is self-describing: it carries its schema. This makes serialized JSON
usable by logs, databases, agents, and external consumers without requiring a
separate schema registry.

When a reporter wants to handle one metric at a time, it can call
`ProgressEvent::metric_snapshots()`. Each `ProgressMetricSnapshot` contains the
complete `ProgressMetric`, event phase, optional stage, flattened counter
values, and elapsed time.

## Quick Start

The common workflow is:

1. Define a schema.
2. Choose a reporter.
3. Create a `Progress` run.
4. Report `started`, `running`, and terminal events.

```rust
use std::time::Duration;

use qubit_progress::{
    Progress,
    ProgressMetric,
    ProgressSchema,
    StderrProgressReporter,
};

let schema = ProgressSchema::new(vec![
    ProgressMetric::new("entries", "Entries"),
    ProgressMetric::new("bytes", "Bytes"),
]);
let reporter = StderrProgressReporter::new();
let mut progress = Progress::new(&reporter, Duration::from_secs(1), schema);

progress.report_started(|event| {
    event
        .counter("entries", |counter| counter.total(3))
        .counter("bytes", |counter| counter.total(1_024))
});

progress.report_running(|event| {
    event
        .counter("entries", |counter| counter.total(3).completed(1).active(1))
        .counter("bytes", |counter| counter.total(1_024).completed(512))
});

progress.report_finished(|event| {
    event
        .counter("entries", |counter| counter.total(3).completed(3).succeeded(3))
        .counter("bytes", |counter| counter.total(1_024).completed(1_024))
});
```

## Defining Metrics and Schemas

Use one metric for each unit that should be tracked independently. Common
metric ids are `entries`, `files`, `dirs`, `bytes`, `objects`, `requests`, and
`tasks`.

```rust
use qubit_progress::{
    ProgressMetric,
    ProgressSchema,
};

let schema = ProgressSchema::new(vec![
    ProgressMetric::new("files", "Files"),
    ProgressMetric::new("bytes", "Bytes"),
]);

assert_eq!(schema.metric_name("files"), Some("Files"));
assert!(schema.contains_metric("bytes"));
```

For a single-metric operation, use `ProgressSchema::single` or
`Progress::single_metric`:

```rust
use std::time::Duration;

use qubit_progress::{
    NoOpProgressReporter,
    Progress,
    ProgressSchema,
};

let schema = ProgressSchema::single("entries", "Entries");
assert_eq!(schema.metric_name("entries"), Some("Entries"));

let reporter = NoOpProgressReporter;
let progress = Progress::single_metric(&reporter, Duration::from_secs(1), "entries", "Entries");
assert_eq!(progress.schema().metric_name("entries"), Some("Entries"));
```

`ProgressSchema::validate_counter` and `ProgressSchema::validate_counters` are
lightweight checks. They verify that counter metric ids exist in the schema and
that an event does not contain duplicate metric ids. They intentionally do not
validate numeric relationships such as `completed <= total`, because retry,
compensation, and dynamically discovered totals are domain-specific.

## Counters

`ProgressCounter` stores `u64` values. This keeps serialized output stable and
avoids platform-dependent `usize` widths.

```rust
use qubit_progress::ProgressCounter;

let counter = ProgressCounter::new("bytes")
    .total(10_000)
    .completed(4_000)
    .active(1)
    .succeeded(3_999)
    .failed(1);

assert_eq!(counter.total_count(), Some(10_000));
assert_eq!(counter.completed_count(), 4_000);
assert_eq!(counter.remaining_count(), Some(5_999));
assert_eq!(counter.progress_percent(), Some(40.0));
```

Use `unknown_total` or omit `total` when the operation is open-ended:

```rust
use qubit_progress::ProgressCounter;

let counter = ProgressCounter::new("records").completed(12);
assert_eq!(counter.total_count(), None);
assert_eq!(counter.progress_percent(), None);
```

## Building Events Directly

If you only need a data model and do not need `Progress` to track elapsed time
or report intervals, build events directly:

```rust
use std::time::Duration;

use qubit_progress::{
    ProgressEvent,
    ProgressMetric,
    ProgressPhase,
    ProgressSchema,
};

let schema = ProgressSchema::new(vec![ProgressMetric::new("entries", "Entries")]);
let event = ProgressEvent::builder(schema)
    .running()
    .counter("entries", |counter| counter.total(10).completed(4))
    .elapsed(Duration::from_millis(250))
    .build();

assert_eq!(event.phase(), ProgressPhase::Running);
assert_eq!(event.counter("entries").map(|counter| counter.completed_count()), Some(4));
```

There are also direct constructors for lifecycle phases:

```rust
use std::time::Duration;

use qubit_progress::{
    ProgressCounter,
    ProgressEvent,
    ProgressSchema,
};

let event = ProgressEvent::finished(
    ProgressSchema::single("entries", "Entries"),
    vec![ProgressCounter::new("entries").total(3).completed(3)],
    Duration::from_secs(2),
);

assert!(event.phase().is_terminal());
```

## Lifecycle Phases

`ProgressPhase` has five values:

| Phase | Meaning |
| --- | --- |
| `Started` | The operation has started. |
| `Running` | The operation is still running. |
| `Finished` | The operation completed successfully. |
| `Failed` | The operation failed. |
| `Canceled` | The operation was canceled. |

`Finished`, `Failed`, and `Canceled` are terminal phases.

```rust
use qubit_progress::ProgressPhase;

assert_eq!(ProgressPhase::Running.as_str(), "running");
assert!(ProgressPhase::Failed.is_terminal());
```

## Stages

Use `ProgressStage` for multi-step operations such as scan, copy, verify, and
publish. Stage metadata is optional. It can include an id, display name, index,
total stage count, and relative weight.

```rust
use std::time::Duration;

use qubit_progress::{
    NoOpProgressReporter,
    Progress,
    ProgressSchema,
    ProgressStage,
};

let reporter = NoOpProgressReporter;
let progress = Progress::new(
    &reporter,
    Duration::from_secs(1),
    ProgressSchema::single("files", "Files"),
)
.with_stage(
    ProgressStage::new("copy", "Copy files")
        .with_index(1)
        .with_total_stages(3)
        .with_weight(0.7),
);

let event = progress.report_started(|event| event.counter("files", |counter| counter.total(10)));
assert_eq!(event.stage().map(|stage| stage.id()), Some("copy"));
```

A stage can be attached to the `Progress` run or overridden per event through
the event builder.

## Reporting Cadence

Use `report_running` when you want to emit immediately. Use
`report_running_if_due` on hot paths where repeated reporting should be
throttled.

```rust
use std::time::Duration;

use qubit_progress::{
    NoOpProgressReporter,
    Progress,
    ProgressSchema,
};

let reporter = NoOpProgressReporter;
let mut progress = Progress::new(
    &reporter,
    Duration::from_secs(60),
    ProgressSchema::single("entries", "Entries"),
);

let not_due = progress.report_running_if_due(|event| {
    event.counter("entries", |counter| counter.total(10).completed(1))
});
assert!(not_due.is_none());

let emitted = progress.report_running(|event| {
    event.counter("entries", |counter| counter.total(10).completed(1))
});
assert_eq!(emitted.counter("entries").map(|counter| counter.completed_count()), Some(1));
```

When the interval is `Duration::ZERO`, every `report_running_if_due` call is due.

## Background Running Reporter

Use `Progress::spawn_running_reporter` when worker threads update shared domain
state and a coordinator should emit periodic `running` events.

The second argument is the `snapshot` closure:

```rust
FnMut() -> Vec<ProgressCounter>
```

It must return the complete current counter snapshot for the next `running`
event. It does not return a delta, a percentage, or a `ProgressEvent`.
`Progress` will wrap the returned counters with the current schema, stage,
elapsed time, and `ProgressPhase::Running`.

The returned counters should normally use metric ids declared in the
`ProgressSchema` used to create the `Progress` run.

```rust
// Good: full current state.
vec![ProgressCounter::new("entries").total(100).completed(42)]

// Not the intended meaning: "one more item since last report".
vec![ProgressCounter::new("entries").completed(1)]
```

The snapshot closure is called by the background reporter thread when a running
event may be due.

```rust
use std::{
    thread,
    time::Duration,
};

use qubit_atomic::ArcAtomic;
use qubit_progress::{
    NoOpProgressReporter,
    Progress,
    ProgressCounter,
    ProgressSchema,
};

let reporter = NoOpProgressReporter;
let completed = ArcAtomic::new(0u64);
let progress = Progress::new(
    &reporter,
    Duration::ZERO,
    ProgressSchema::single("entries", "Entries"),
);

thread::scope(|scope| {
    let snapshot_completed = completed.clone();
    let running = progress.spawn_running_reporter(scope, move || {
        // Return a full current snapshot for the running event.
        // This says "3 total, currently 1 completed", not "completed 1 more".
        vec![ProgressCounter::new("entries")
            .total(3)
            .completed(snapshot_completed.load())]
    });
    let point = running.point_handle();

    let worker = scope.spawn({
        let completed = completed.clone();
        let point = point.clone();
        move || {
            completed.fetch_add(1);
            assert!(point.report());
        }
    });
    worker.join().expect("worker should complete");

    running.stop_and_join();
});
```

Important rules:

- Always call `stop_and_join` before the thread scope exits.
- Worker handles can only report points; they cannot stop the loop.
- With a zero interval, `RunningProgressPointHandle::report` wakes the reporter loop.
- With a positive interval, worker point reporting is a cheap no-op and the loop wakes on timeout.
- Panics from the reporter thread are propagated by `stop_and_join`.

## Built-in Reporters

| Reporter | Use case |
| --- | --- |
| `NoOpProgressReporter` | Tests, optional progress, or disabled reporting. |
| `MetricSnapshotProgressReporter` | Send structured `ProgressMetricSnapshot` objects to a consumer. |
| `FormattedProgressReporter` | Format each metric snapshot and send strings to a consumer. |
| `HumanReadableProgressReporter` | Send human-readable metric snapshot strings to a consumer. |
| `JsonProgressReporter` | Send JSON metric snapshot strings to a consumer. |
| `WriterProgressReporter<W>` | Write human-readable metric snapshot lines to any `Write + Send` sink. |
| `StdoutProgressReporter` | Command-line progress to stdout. |
| `StderrProgressReporter` | Command-line progress to stderr. |
| `LoggerProgressReporter` | Emit progress through the `log` crate. |
| `JsonWriterProgressReporter<W>` | Write JSON metric snapshot lines to any `Write + Send` sink. |
| `JsonStdoutProgressReporter` | Command-line JSON progress to stdout. |
| `JsonStderrProgressReporter` | Command-line JSON progress to stderr. |
| `JsonLoggerProgressReporter` | Emit JSON metric snapshots through the `log` crate. |

Example with an in-memory writer:

```rust
use std::{
    io::Cursor,
    sync::{Arc, Mutex},
    time::Duration,
};

use qubit_progress::{
    Progress,
    ProgressSchema,
    WriterProgressReporter,
};

let output = Arc::new(Mutex::new(Cursor::new(Vec::new())));
let reporter = WriterProgressReporter::new(output.clone());
let progress = Progress::new(
    &reporter,
    Duration::ZERO,
    ProgressSchema::single("entries", "Entries"),
);

progress.report_finished(|event| {
    event.counter("entries", |counter| counter.total(2).completed(2).succeeded(2))
});

let text = String::from_utf8(
    output.lock().expect("output should lock").get_ref().clone(),
)
.expect("progress output should be UTF-8");
assert!(text.contains("finished"));
assert!(text.contains("Entries 2/2"));
```

## JSON Serialization

`ProgressEvent`, `ProgressSchema`, `ProgressMetric`, `ProgressCounter`,
`ProgressPhase`, and `ProgressStage` are serde-serializable. The `elapsed`
field uses `qubit-serde` duration strings such as `110ms`.

```rust
use std::time::Duration;

use qubit_progress::{
    ProgressEvent,
    ProgressMetric,
    ProgressSchema,
};

let event = ProgressEvent::builder(ProgressSchema::new(vec![
    ProgressMetric::new("entries", "Entries"),
]))
.running()
.counter("entries", |counter| counter.total(5).completed(2))
.elapsed(Duration::from_millis(110))
.build();

let json = serde_json::to_string(&event).expect("event should serialize");
assert_eq!(
    json,
    concat!(
        r#"{"schema":{"metrics":["#,
        r#"{"id":"entries","name":"Entries"}"#,
        r#"]},"phase":"running","counters":["#,
        r#"{"metric_id":"entries","total_count":5,"completed_count":2,"#,
        r#""active_count":0,"succeeded_count":0,"failed_count":0}"#,
        r#"],"elapsed":"110ms"}"#,
    ),
);
```

This representation is useful for agent-readable logs because each event
contains both the metric ids and their display names.

For line-oriented structured output, prefer the built-in JSON metric snapshot
reporters. They write one JSON object per metric counter:

```rust
use std::{
    io::Cursor,
    sync::{Arc, Mutex},
    time::Duration,
};

use qubit_progress::{
    JsonWriterProgressReporter,
    ProgressCounter,
    ProgressEvent,
    ProgressReporter,
    ProgressSchema,
};

let output = Arc::new(Mutex::new(Cursor::new(Vec::new())));
let reporter = JsonWriterProgressReporter::new(output.clone());
reporter.report(&ProgressEvent::running(
    ProgressSchema::single("entries", "Entries"),
    vec![ProgressCounter::new("entries").total(5).completed(2)],
    Duration::from_millis(110),
));

let text = String::from_utf8(
    output.lock().expect("output should lock").get_ref().clone(),
)
.expect("JSON output should be UTF-8");
assert!(text.contains(r#""metric":{"id":"entries","name":"Entries"}"#));
assert!(text.contains(r#""elapsed":"110ms""#));
```

Use `JsonProgressReporter` when you already have a
`qubit_function::Consumer<String>`, `JsonWriterProgressReporter` for any
`Write` sink, and `JsonLoggerProgressReporter` for `log` output.

## Consuming Metric Snapshots Directly

Some integrations should not format progress as strings at all. GUI progress
bars, metrics collectors, and database writers often want structured objects.
Use `MetricSnapshotProgressReporter` for those cases:

```rust
use std::{
    sync::{Arc, Mutex},
    time::Duration,
};

use qubit_function::ArcConsumer;
use qubit_progress::{
    MetricSnapshotProgressReporter,
    ProgressCounter,
    ProgressEvent,
    ProgressMetricSnapshot,
    ProgressReporter,
    ProgressSchema,
};

let snapshots = Arc::new(Mutex::new(Vec::<ProgressMetricSnapshot>::new()));
let captured = Arc::clone(&snapshots);
let consumer = ArcConsumer::new(move |snapshot: &ProgressMetricSnapshot| {
    captured
        .lock()
        .expect("snapshot list should lock")
        .push(snapshot.clone());
});
let reporter = MetricSnapshotProgressReporter::new(consumer);

reporter.report(&ProgressEvent::running(
    ProgressSchema::single("entries", "Entries"),
    vec![ProgressCounter::new("entries").total(5).completed(2)],
    Duration::from_millis(110),
));

let snapshots = snapshots.lock().expect("snapshot list should lock");
assert_eq!(snapshots[0].metric_id(), "entries");
assert_eq!(snapshots[0].completed_count(), 2);
```

## Implementing a Custom Reporter

A reporter implements one trait:

```rust
use qubit_progress::{
    ProgressEvent,
    ProgressReporter,
};

struct MyReporter;

impl ProgressReporter for MyReporter {
    fn report(&self, event: &ProgressEvent) {
        println!("phase={}", event.phase());
    }
}
```

`ProgressReporter` requires `Send + Sync`, so reporters can be shared between
worker and reporter threads. If your reporter stores mutable state, use a
thread-safe primitive such as `Mutex`, `RwLock`, an atomic type, or a channel.

### Recording Reporter for Tests

```rust
use std::sync::Mutex;

use qubit_progress::{
    ProgressEvent,
    ProgressReporter,
};

#[derive(Default)]
struct RecordingReporter {
    events: Mutex<Vec<ProgressEvent>>,
}

impl RecordingReporter {
    fn events(&self) -> Vec<ProgressEvent> {
        self.events
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .clone()
    }
}

impl ProgressReporter for RecordingReporter {
    fn report(&self, event: &ProgressEvent) {
        self.events
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .push(event.clone());
    }
}
```

This style is useful for tests because `ProgressEvent` is immutable and cloneable.

### JSON Lines Reporter

A JSON-lines reporter is often the best format for agents, batch jobs, and
external monitoring processes.

```rust
use std::{
    io::Write,
    sync::Mutex,
};

use qubit_progress::{
    ProgressEvent,
    ProgressReporter,
};

struct JsonLinesProgressReporter<W> {
    writer: Mutex<W>,
}

impl<W> JsonLinesProgressReporter<W> {
    fn new(writer: W) -> Self {
        Self {
            writer: Mutex::new(writer),
        }
    }
}

impl<W> ProgressReporter for JsonLinesProgressReporter<W>
where
    W: Write + Send,
{
    fn report(&self, event: &ProgressEvent) {
        let mut writer = self
            .writer
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        serde_json::to_writer(&mut *writer, event).expect("progress event should serialize");
        writeln!(writer).expect("progress event should write");
    }
}
```

For production code, decide explicitly whether reporter I/O failures should
panic, be ignored, or be sent to a fallback error channel.

## GUI Progress Bar Reporter

GUI toolkits usually require UI updates to happen on the UI thread. A reporter
should therefore avoid touching widgets directly from `report`. Instead, send a
small message to the UI thread and let the UI thread update the progress bar.

The following example uses `std::sync::mpsc` to model that boundary:

```rust
use std::sync::mpsc::Sender;

use qubit_progress::{
    ProgressEvent,
    ProgressPhase,
    ProgressReporter,
};

#[derive(Debug, Clone)]
struct GuiProgressMessage {
    phase: ProgressPhase,
    stage_name: Option<String>,
    completed: u64,
    total: Option<u64>,
    percent: Option<f64>,
}

#[derive(Clone)]
struct GuiProgressReporter {
    sender: Sender<GuiProgressMessage>,
    primary_metric_id: String,
}

impl GuiProgressReporter {
    fn new(sender: Sender<GuiProgressMessage>, primary_metric_id: &str) -> Self {
        Self {
            sender,
            primary_metric_id: primary_metric_id.to_owned(),
        }
    }
}

impl ProgressReporter for GuiProgressReporter {
    fn report(&self, event: &ProgressEvent) {
        let Some(counter) = event.counter(&self.primary_metric_id) else {
            return;
        };
        let message = GuiProgressMessage {
            phase: event.phase(),
            stage_name: event.stage().map(|stage| stage.name().to_owned()),
            completed: counter.completed_count(),
            total: counter.total_count(),
            percent: counter.progress_percent(),
        };
        let _ = self.sender.send(message);
    }
}
```

The UI side can map the message to widget state:

```rust
use std::sync::mpsc::Receiver;

fn handle_gui_messages(receiver: Receiver<GuiProgressMessage>) {
    while let Ok(message) = receiver.recv() {
        match message.total {
            Some(total) => {
                println!(
                    "set progress bar to {}/{} ({:?}%)",
                    message.completed,
                    total,
                    message.percent,
                );
            }
            None => {
                println!("show indeterminate progress: {} completed", message.completed);
            }
        }

        if message.phase.is_terminal() {
            println!("enable close button");
            break;
        }
    }
}
```

For a real GUI crate, replace the `println!` calls with toolkit-specific UI
updates. Keep the same architecture: reporter sends messages, UI thread owns
widgets.

## Extension Guidelines

When implementing a reporter:

- Keep `report` short. Expensive work should be offloaded to another thread or queue.
- Do not block worker hot paths unless progress reporting is part of the operation contract.
- Treat one reporter stream as one logical operation unless you explicitly add multiplexing.
- Use `event.schema()` to resolve display names instead of hard-coding metric labels.
- Use `event.counter("metric_id")` for a primary metric and `event.counters()` for multi-metric displays.
- Decide the failure policy: panic, ignore, log, or send error messages elsewhere.
- Avoid holding locks while calling external code.
- Prefer sending compact messages to GUI or async runtimes instead of storing UI handles inside the reporter.

When designing metric ids:

- Use stable lowercase ids such as `entries`, `bytes`, `files`, or `requests`.
- Keep ids machine-readable; use `ProgressMetric::name` for display labels.
- Use separate metrics for separate units. Do not mix bytes and files in one counter.
- Keep the same schema for the whole operation.

When reporting counters:

- Use known totals when available.
- Use unknown totals for streams, discovery phases, and open-ended work.
- Use `active` for currently in-flight units.
- Use `succeeded` and `failed` when callers need final outcome counts.
- Keep domain state outside `Progress`; build fresh counters when reporting.

## Choosing an API

| Need | Recommended API |
| --- | --- |
| Build one standalone progress payload | `ProgressEvent::builder` |
| Track elapsed time for one operation | `Progress` |
| Emit periodic running updates from a loop | `report_running_if_due` |
| Emit immediate running updates | `report_running` |
| Worker threads update state and a coordinator reports | `spawn_running_reporter` |
| Disable progress | `NoOpProgressReporter` |
| Human-readable CLI output | `StdoutProgressReporter` or `StderrProgressReporter` |
| Structured logs | custom JSON-lines reporter |
| GUI progress bar | custom channel-based reporter |

## Crate Boundary

`qubit-progress` intentionally stays small. It provides:

- progress data models;
- operation-scoped lifecycle helpers;
- background running-report helpers;
- reporter traits and a few built-in reporters.

It does not provide:

- terminal UI widgets;
- GUI toolkit integration;
- async runtime integration;
- task scheduling;
- tracing infrastructure;
- metrics databases or dashboards.

Those integrations should live in downstream crates or applications by
implementing `ProgressReporter`.