diff --git a/.changes/asset-iter.md b/.changes/asset-iter.md new file mode 100644 index 000000000000..1b95f4902ae1 --- /dev/null +++ b/.changes/asset-iter.md @@ -0,0 +1,5 @@ +--- +"tauri": patch:enhance +--- + +Added `AssetResolver::iter` to iterate on all embedded assets. diff --git a/core/tauri-utils/src/assets.rs b/core/tauri-utils/src/assets.rs index 5b035a38690e..ca7f3de698ac 100644 --- a/core/tauri-utils/src/assets.rs +++ b/core/tauri-utils/src/assets.rs @@ -109,6 +109,9 @@ pub trait Assets: Send + Sync + 'static { /// Get the content of the passed [`AssetKey`]. fn get(&self, key: &AssetKey) -> Option>; + /// Iterator for the assets. + fn iter(&self) -> Box + '_>; + /// Gets the hashes for the CSP tag of the HTML on the given path. fn csp_hashes(&self, html_path: &AssetKey) -> Box> + '_>; } @@ -163,6 +166,10 @@ impl Assets for EmbeddedAssets { .map(|a| Cow::Owned(a.to_vec())) } + fn iter(&self) -> Box + '_> { + Box::new(self.assets.into_iter()) + } + fn csp_hashes(&self, html_path: &AssetKey) -> Box> + '_> { Box::new( self diff --git a/core/tauri/src/app.rs b/core/tauri/src/app.rs index d9bd7635a9eb..c0f7911fd1bd 100644 --- a/core/tauri/src/app.rs +++ b/core/tauri/src/app.rs @@ -357,6 +357,11 @@ impl AssetResolver { pub fn get(&self, path: String) -> Option { self.manager.get_asset(path).ok() } + + /// Iterate on all assets. + pub fn iter(&self) -> Box + '_> { + self.manager.asset_iter() + } } /// A handle to the currently running application. diff --git a/core/tauri/src/manager.rs b/core/tauri/src/manager.rs index a6948afd31f4..372ffa14b6cb 100644 --- a/core/tauri/src/manager.rs +++ b/core/tauri/src/manager.rs @@ -627,6 +627,10 @@ impl WindowManager { }) } + pub fn asset_iter(&self) -> Box + '_> { + self.inner.assets.iter() + } + pub fn get_asset(&self, mut path: String) -> Result> { let assets = &self.inner.assets; if path.ends_with('/') { diff --git a/core/tauri/src/test/mod.rs b/core/tauri/src/test/mod.rs index eaadae04f649..3138d56fbf5e 100644 --- a/core/tauri/src/test/mod.rs +++ b/core/tauri/src/test/mod.rs @@ -99,6 +99,7 @@ struct Ipc(Mutex, csp_hashes: Vec>, } @@ -107,6 +108,10 @@ impl Assets for NoopAsset { None } + fn iter(&self) -> Box + '_> { + Box::new(self.assets.iter()) + } + fn csp_hashes(&self, html_path: &AssetKey) -> Box> + '_> { Box::new(self.csp_hashes.iter().copied()) } @@ -115,6 +120,7 @@ impl Assets for NoopAsset { /// Creates a new empty [`Assets`] implementation. pub fn noop_assets() -> NoopAsset { NoopAsset { + assets: Default::default(), csp_hashes: Default::default(), } }