Skip to content

Commit

Permalink
feat(core): add AssetManager::iter (#8288)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lucasfernog-crabnebula authored Nov 23, 2023
1 parent 5046270 commit b3e53e7
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changes/asset-iter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch:enhance
---

Added `AssetResolver::iter` to iterate on all embedded assets.
7 changes: 7 additions & 0 deletions core/tauri-utils/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ pub trait Assets: Send + Sync + 'static {
/// Get the content of the passed [`AssetKey`].
fn get(&self, key: &AssetKey) -> Option<Cow<'_, [u8]>>;

/// Iterator for the assets.
fn iter(&self) -> Box<dyn Iterator<Item = (&&str, &&[u8])> + '_>;

/// Gets the hashes for the CSP tag of the HTML on the given path.
fn csp_hashes(&self, html_path: &AssetKey) -> Box<dyn Iterator<Item = CspHash<'_>> + '_>;
}
Expand Down Expand Up @@ -163,6 +166,10 @@ impl Assets for EmbeddedAssets {
.map(|a| Cow::Owned(a.to_vec()))
}

fn iter(&self) -> Box<dyn Iterator<Item = (&&str, &&[u8])> + '_> {
Box::new(self.assets.into_iter())
}

fn csp_hashes(&self, html_path: &AssetKey) -> Box<dyn Iterator<Item = CspHash<'_>> + '_> {
Box::new(
self
Expand Down
5 changes: 5 additions & 0 deletions core/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,11 @@ impl<R: Runtime> AssetResolver<R> {
pub fn get(&self, path: String) -> Option<Asset> {
self.manager.get_asset(path).ok()
}

/// Iterate on all assets.
pub fn iter(&self) -> Box<dyn Iterator<Item = (&&str, &&[u8])> + '_> {
self.manager.asset_iter()
}
}

/// A handle to the currently running application.
Expand Down
4 changes: 4 additions & 0 deletions core/tauri/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,10 @@ impl<R: Runtime> WindowManager<R> {
})
}

pub fn asset_iter(&self) -> Box<dyn Iterator<Item = (&&str, &&[u8])> + '_> {
self.inner.assets.iter()
}

pub fn get_asset(&self, mut path: String) -> Result<Asset, Box<dyn std::error::Error>> {
let assets = &self.inner.assets;
if path.ends_with('/') {
Expand Down
6 changes: 6 additions & 0 deletions core/tauri/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ struct Ipc(Mutex<HashMap<IpcKey, Sender<std::result::Result<JsonValue, JsonValue

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

Expand All @@ -107,6 +108,10 @@ impl Assets for NoopAsset {
None
}

fn iter(&self) -> Box<dyn Iterator<Item = (&&str, &&[u8])> + '_> {
Box::new(self.assets.iter())
}

fn csp_hashes(&self, html_path: &AssetKey) -> Box<dyn Iterator<Item = CspHash<'_>> + '_> {
Box::new(self.csp_hashes.iter().copied())
}
Expand All @@ -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(),
}
}
Expand Down

0 comments on commit b3e53e7

Please sign in to comment.