wasmer_wasix/runtime/module_cache/
progress.rs1use std::sync::Arc;
2
3use wasmer_types::UserAbort;
4
5#[derive(Clone, Debug)]
7#[non_exhaustive]
8pub enum ModuleLoadProgress {
9 LocalCacheHit,
11 DownloadingModule(DownloadProgress),
13 ArtifactCacheHit,
15 RemoteArtifact(RemoteArtifactProgress),
17 CompilingModule(wasmer_types::CompilationProgress),
19}
20
21#[derive(Clone, Debug)]
23#[non_exhaustive]
24pub enum RemoteArtifactProgress {
25 RemoteCacheCheck,
27 RemoteCacheHit,
29 LoadingArtifact(DownloadProgress),
31 ArtifactLoadFailed(ProgressError),
33}
34
35#[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 pub fn new(total_bytes: u64, downloaded_bytes: Option<u64>) -> Self {
46 Self {
47 total_bytes,
48 downloaded_bytes,
49 }
50 }
51}
52
53#[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#[derive(Clone)]
77pub struct ModuleLoadProgressReporter {
78 callback: Arc<dyn Fn(ModuleLoadProgress) -> Result<(), UserAbort> + Send + Sync>,
79}
80
81impl ModuleLoadProgressReporter {
82 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 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}