omicron_zone_package/progress.rs
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! Describes utilities for relaying progress to end-users.
6
7use slog::Logger;
8use std::borrow::Cow;
9use std::sync::OnceLock;
10
11/// Trait for propagating progress information while constructing the package.
12pub trait Progress {
13 /// Updates the message displayed regarding progress constructing
14 /// the package.
15 fn set_message(&self, _msg: Cow<'static, str>) {}
16
17 /// Returns the debug logger
18 fn get_log(&self) -> &Logger;
19
20 /// Increments the number of things which need to be completed
21 fn increment_total(&self, _delta: u64) {}
22
23 /// Increments the number of things which have completed.
24 fn increment_completed(&self, _delta: u64) {}
25
26 /// Returns a new [`Progress`] which will report progress for a sub task.
27 fn sub_progress(&self, _total: u64) -> Box<dyn Progress> {
28 Box::new(NoProgress::new())
29 }
30}
31
32/// Implements [`Progress`] as a no-op.
33pub struct NoProgress {
34 log: OnceLock<slog::Logger>,
35}
36
37impl NoProgress {
38 pub const fn new() -> Self {
39 Self {
40 log: OnceLock::new(),
41 }
42 }
43}
44
45impl Default for NoProgress {
46 fn default() -> Self {
47 Self::new()
48 }
49}
50
51impl Progress for NoProgress {
52 fn get_log(&self) -> &Logger {
53 self.log
54 .get_or_init(|| slog::Logger::root(slog::Discard, slog::o!()))
55 }
56}