wasmer_types/
progress.rs

1//! Types used to report and handle compilation progress.
2
3use crate::lib::std::{borrow::Cow, fmt, string::String, sync::Arc};
4use thiserror::Error;
5
6/// Indicates the current compilation progress.
7///
8/// All fields are kept private for forwards compatibility and future extension.
9/// Use the provided methods to access progress data.
10#[derive(Clone, Debug, Default)]
11pub struct CompilationProgress {
12    phase_name: Option<Cow<'static, str>>,
13    phase_step_count: Option<u64>,
14    phase_step: Option<u64>,
15}
16
17impl CompilationProgress {
18    /// Creates a new [`CompilationProgress`].
19    pub fn new(
20        phase_name: Option<Cow<'static, str>>,
21        phase_step_count: Option<u64>,
22        phase_step: Option<u64>,
23    ) -> Self {
24        Self {
25            phase_name,
26            phase_step_count,
27            phase_step,
28        }
29    }
30
31    /// Returns the name of the phase currently being executed.
32    pub fn phase_name(&self) -> Option<&str> {
33        self.phase_name.as_deref()
34    }
35
36    /// Returns the total number of steps in the current phase, if known.
37    pub fn phase_step_count(&self) -> Option<u64> {
38        self.phase_step_count
39    }
40
41    /// Returns the index of the current step within the phase, if known.
42    pub fn phase_step(&self) -> Option<u64> {
43        self.phase_step
44    }
45}
46
47/// Error returned when the user requests to abort an expensive computation.
48#[derive(Clone, Debug, Error)]
49#[error("{reason}")]
50pub struct UserAbort {
51    reason: String,
52}
53
54impl UserAbort {
55    /// Creates a new [`UserAbort`].
56    pub fn new(reason: impl Into<String>) -> Self {
57        Self {
58            reason: reason.into(),
59        }
60    }
61
62    /// Returns the configured reason.
63    pub fn reason(&self) -> &str {
64        &self.reason
65    }
66}
67
68/// Wraps a boxed callback that can receive compilation progress notifications.
69#[derive(Clone)]
70pub struct CompilationProgressCallback {
71    callback: Arc<dyn Fn(CompilationProgress) -> Result<(), UserAbort> + Send + Sync + 'static>,
72}
73
74impl CompilationProgressCallback {
75    /// Create a new callback wrapper.
76    ///
77    /// The provided callback will be invoked with progress updates during the compilation process,
78    /// and has to return a `Result<(), UserAbort>`.
79    ///
80    /// If the callback returns an error, the compilation will be aborted with a `CompileError::Aborted`.
81    pub fn new<F>(callback: F) -> Self
82    where
83        F: Fn(CompilationProgress) -> Result<(), UserAbort> + Send + Sync + 'static,
84    {
85        Self {
86            callback: Arc::new(callback),
87        }
88    }
89
90    /// Notify the callback about new progress information.
91    pub fn notify(&self, progress: CompilationProgress) -> Result<(), UserAbort> {
92        (self.callback)(progress)
93    }
94}
95
96impl fmt::Debug for CompilationProgressCallback {
97    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98        f.debug_struct("CompilationProgressCallback").finish()
99    }
100}