Skip to content

Commit b3e53e7

Browse files
feat(core): add AssetManager::iter (#8288)
This new function allows users to iterate on all embedded assets, important if you want to AssetManager::get an asset you are not sure exists.
1 parent 5046270 commit b3e53e7

File tree

5 files changed

+27
-0
lines changed

5 files changed

+27
-0
lines changed

.changes/asset-iter.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch:enhance
3+
---
4+
5+
Added `AssetResolver::iter` to iterate on all embedded assets.

core/tauri-utils/src/assets.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ pub trait Assets: Send + Sync + 'static {
109109
/// Get the content of the passed [`AssetKey`].
110110
fn get(&self, key: &AssetKey) -> Option<Cow<'_, [u8]>>;
111111

112+
/// Iterator for the assets.
113+
fn iter(&self) -> Box<dyn Iterator<Item = (&&str, &&[u8])> + '_>;
114+
112115
/// Gets the hashes for the CSP tag of the HTML on the given path.
113116
fn csp_hashes(&self, html_path: &AssetKey) -> Box<dyn Iterator<Item = CspHash<'_>> + '_>;
114117
}
@@ -163,6 +166,10 @@ impl Assets for EmbeddedAssets {
163166
.map(|a| Cow::Owned(a.to_vec()))
164167
}
165168

169+
fn iter(&self) -> Box<dyn Iterator<Item = (&&str, &&[u8])> + '_> {
170+
Box::new(self.assets.into_iter())
171+
}
172+
166173
fn csp_hashes(&self, html_path: &AssetKey) -> Box<dyn Iterator<Item = CspHash<'_>> + '_> {
167174
Box::new(
168175
self

core/tauri/src/app.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ impl<R: Runtime> AssetResolver<R> {
357357
pub fn get(&self, path: String) -> Option<Asset> {
358358
self.manager.get_asset(path).ok()
359359
}
360+
361+
/// Iterate on all assets.
362+
pub fn iter(&self) -> Box<dyn Iterator<Item = (&&str, &&[u8])> + '_> {
363+
self.manager.asset_iter()
364+
}
360365
}
361366

362367
/// A handle to the currently running application.

core/tauri/src/manager.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,10 @@ impl<R: Runtime> WindowManager<R> {
627627
})
628628
}
629629

630+
pub fn asset_iter(&self) -> Box<dyn Iterator<Item = (&&str, &&[u8])> + '_> {
631+
self.inner.assets.iter()
632+
}
633+
630634
pub fn get_asset(&self, mut path: String) -> Result<Asset, Box<dyn std::error::Error>> {
631635
let assets = &self.inner.assets;
632636
if path.ends_with('/') {

core/tauri/src/test/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ struct Ipc(Mutex<HashMap<IpcKey, Sender<std::result::Result<JsonValue, JsonValue
9999

100100
/// An empty [`Assets`] implementation.
101101
pub struct NoopAsset {
102+
assets: HashMap<&'static str, &'static [u8]>,
102103
csp_hashes: Vec<CspHash<'static>>,
103104
}
104105

@@ -107,6 +108,10 @@ impl Assets for NoopAsset {
107108
None
108109
}
109110

111+
fn iter(&self) -> Box<dyn Iterator<Item = (&&str, &&[u8])> + '_> {
112+
Box::new(self.assets.iter())
113+
}
114+
110115
fn csp_hashes(&self, html_path: &AssetKey) -> Box<dyn Iterator<Item = CspHash<'_>> + '_> {
111116
Box::new(self.csp_hashes.iter().copied())
112117
}
@@ -115,6 +120,7 @@ impl Assets for NoopAsset {
115120
/// Creates a new empty [`Assets`] implementation.
116121
pub fn noop_assets() -> NoopAsset {
117122
NoopAsset {
123+
assets: Default::default(),
118124
csp_hashes: Default::default(),
119125
}
120126
}

0 commit comments

Comments
 (0)