Crate dioxus_asset_resolver

Crate dioxus_asset_resolver 

Source
Expand description

The asset resolver for the Dioxus bundle format. Each platform has its own way of resolving assets. This crate handles resolving assets in a cross-platform way.

There are two broad locations for assets depending on the platform:

  • Web: Assets are stored on a remote server and fetched via HTTP requests.
  • Native: Assets are read from the local bundle. Each platform has its own bundle structure which may store assets as a file at a specific path or in an opaque format like Android’s AssetManager.

read_asset_bytes( abstracts over both of these methods, allowing you to read the bytes of an asset regardless of the platform.

If you know you are on a desktop platform, you can use asset_path to resolve the path of an asset and read the contents with std::fs.

§Example

use dioxus::prelude::*;

// Bundle the static JSON asset into the application
static JSON_ASSET: Asset = asset!("/assets/data.json");

// Read the bytes of the JSON asset
let bytes = dioxus::asset_resolver::read_asset_bytes(&JSON_ASSET).await.unwrap();

// Deserialize the JSON data
let json: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
assert_eq!(json["key"].as_str(), Some("value"));

Structs§

WebAssetResolveError
An error that occurs when resolving an asset on the web.

Enums§

AssetPathError
An error that can occur when resolving an asset to a path. Not all platforms can represent assets as paths, an error may mean that the asset doesn’t exist or it cannot be represented as a path.
AssetResolveError
An error that can occur when resolving an asset.
NativeAssetResolveError
An error that occurs when resolving a native asset.

Functions§

asset_path
Tries to resolve the path of an asset from a given URI path. Depending on the platform, this may return an error even if the asset exists because some platforms cannot represent assets as paths. You should prefer read_asset_bytes to read the asset bytes directly for cross-platform compatibility.
read_asset_bytes
Read the bytes of an asset. This will work on both web and native platforms. On the web, it will fetch the asset via HTTP, and on native platforms, it will read the asset from the filesystem or bundle.