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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! Describes utilities for relaying progress to end-users.

use slog::Logger;
use std::borrow::Cow;
use std::sync::OnceLock;

/// Trait for propagating progress information while constructing the package.
pub trait Progress {
    /// Updates the message displayed regarding progress constructing
    /// the package.
    fn set_message(&self, _msg: Cow<'static, str>) {}

    /// Returns the debug logger
    fn get_log(&self) -> &Logger;

    /// Increments the number of things which need to be completed
    fn increment_total(&self, _delta: u64) {}

    /// Increments the number of things which have completed.
    fn increment_completed(&self, _delta: u64) {}

    /// Returns a new [`Progress`] which will report progress for a sub task.
    fn sub_progress(&self, _total: u64) -> Box<dyn Progress> {
        Box::new(NoProgress::new())
    }
}

/// Implements [`Progress`] as a no-op.
pub struct NoProgress {
    log: OnceLock<slog::Logger>,
}

impl NoProgress {
    pub const fn new() -> Self {
        Self {
            log: OnceLock::new(),
        }
    }
}

impl Default for NoProgress {
    fn default() -> Self {
        Self::new()
    }
}

impl Progress for NoProgress {
    fn get_log(&self) -> &Logger {
        self.log
            .get_or_init(|| slog::Logger::root(slog::Discard, slog::o!()))
    }
}