wasmer_wasix/runtime/module_cache/
progress.rs

1use std::sync::Arc;
2
3use wasmer_types::UserAbort;
4
5/// Progress during module loading.
6#[derive(Clone, Debug)]
7#[non_exhaustive]
8pub enum ModuleLoadProgress {
9    /// Module was found in local cache.
10    LocalCacheHit,
11    /// Module is being downloaded.
12    DownloadingModule(DownloadProgress),
13    /// Module artifact was found in local cache.
14    ArtifactCacheHit,
15    /// Module artifact is being loaded from remote source.
16    RemoteArtifact(RemoteArtifactProgress),
17    /// Module is being compiled.
18    CompilingModule(wasmer_types::CompilationProgress),
19}
20
21/// Progress during remote module artifact resolution.
22#[derive(Clone, Debug)]
23#[non_exhaustive]
24pub enum RemoteArtifactProgress {
25    /// Checking remote cache for artifact.
26    RemoteCacheCheck,
27    /// Artifact found in remote cache.
28    RemoteCacheHit,
29    /// Artifact is being downloaded.
30    LoadingArtifact(DownloadProgress),
31    /// Artifact loading failed - module will be compiled locally instead.
32    ArtifactLoadFailed(ProgressError),
33}
34
35/// Generic download progress.
36#[derive(Clone, Debug)]
37#[non_exhaustive]
38pub struct DownloadProgress {
39    pub total_bytes: u64,
40    pub downloaded_bytes: Option<u64>,
41}
42
43impl DownloadProgress {
44    /// Creates a new [`DownloadProgress`].
45    pub fn new(total_bytes: u64, downloaded_bytes: Option<u64>) -> Self {
46        Self {
47            total_bytes,
48            downloaded_bytes,
49        }
50    }
51}
52
53/// Error that can occur during module loading.
54#[derive(Clone, Debug)]
55pub struct ProgressError {
56    message: String,
57}
58
59impl ProgressError {
60    pub fn new(message: impl Into<String>) -> Self {
61        Self {
62            message: message.into(),
63        }
64    }
65}
66
67impl std::fmt::Display for ProgressError {
68    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69        write!(f, "{}", self.message)
70    }
71}
72
73/// Reports progress during module loading.
74///
75/// See [`ModuleLoadProgressReporter::new`] for details.
76#[derive(Clone)]
77pub struct ModuleLoadProgressReporter {
78    callback: Arc<dyn Fn(ModuleLoadProgress) -> Result<(), UserAbort> + Send + Sync>,
79}
80
81impl ModuleLoadProgressReporter {
82    /// Construct a new `ModuleLoadProgressReporter`.
83    ///
84    /// The callback function has the signature:
85    /// `Fn(ModuleLoadProgress) -> Result<(), UserAbort> + Send + Sync + 'static'.
86    ///
87    /// # Aborting module loading
88    ///
89    /// The callback has to return a `Result<(), UserAbort>`.
90    /// If an error is returned, the module loading process will be aborted.
91    pub fn new<F>(callback: F) -> Self
92    where
93        F: Fn(ModuleLoadProgress) -> Result<(), UserAbort> + Send + Sync + 'static,
94    {
95        Self {
96            callback: Arc::new(callback),
97        }
98    }
99
100    /// Notify the reporter about new progress information.
101    pub fn notify(&self, progress: ModuleLoadProgress) -> Result<(), UserAbort> {
102        (self.callback)(progress)
103    }
104}
105
106impl std::fmt::Debug for ModuleLoadProgressReporter {
107    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
108        f.debug_struct("ModuleLoadProgressReporter").finish()
109    }
110}