Relax asset resolver bounds and document resolving folder assets (#4859)
* relax asset bounds and document resolving folder assets * fix docs for folder asset_path
This commit is contained in:
1 parent
5559e463c8
commit
566dab939f
3 files changed
+48
-8
No files matched your search
Generated
-1
@@ -5300,7 +5300,6 @@ dependencies = [
|
|||||||
"infer",
|
"infer",
|
||||||
"jni 0.21.1",
|
"jni 0.21.1",
|
||||||
"js-sys",
|
"js-sys",
|
||||||
"manganis-core",
|
|
||||||
"ndk 0.9.0",
|
"ndk 0.9.0",
|
||||||
"ndk-context",
|
"ndk-context",
|
||||||
"ndk-sys 0.6.0+11769913",
|
"ndk-sys 0.6.0+11769913",
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ rust-version = "1.79.0"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
thiserror = { workspace = true }
|
thiserror = { workspace = true }
|
||||||
manganis-core = { workspace = true }
|
|
||||||
|
|
||||||
# native/fs resolver dependencies
|
# native/fs resolver dependencies
|
||||||
http = { workspace = true, optional = true }
|
http = { workspace = true, optional = true }
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
//! use dioxus::prelude::*;
|
//! use dioxus::prelude::*;
|
||||||
//!
|
//!
|
||||||
//! // Bundle the static JSON asset into the application
|
//! // Bundle the static JSON asset into the application
|
||||||
//! static JSON_ASSET: Asset = asset!("assets/data.json");
|
//! static JSON_ASSET: Asset = asset!("/assets/data.json");
|
||||||
//!
|
//!
|
||||||
//! // Read the bytes of the JSON asset
|
//! // Read the bytes of the JSON asset
|
||||||
//! let bytes = dioxus::asset_resolver::read_asset_bytes(&JSON_ASSET).await.unwrap();
|
//! let bytes = dioxus::asset_resolver::read_asset_bytes(&JSON_ASSET).await.unwrap();
|
||||||
@@ -30,7 +30,6 @@
|
|||||||
//! # }
|
//! # }
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
use manganis_core::Asset;
|
|
||||||
use std::{fmt::Debug, path::PathBuf};
|
use std::{fmt::Debug, path::PathBuf};
|
||||||
|
|
||||||
#[cfg(feature = "native")]
|
#[cfg(feature = "native")]
|
||||||
@@ -69,7 +68,7 @@ pub enum AssetPathError {
|
|||||||
/// use dioxus::prelude::*;
|
/// use dioxus::prelude::*;
|
||||||
///
|
///
|
||||||
/// // Bundle the static JSON asset into the application
|
/// // Bundle the static JSON asset into the application
|
||||||
/// static JSON_ASSET: Asset = asset!("assets/data.json");
|
/// static JSON_ASSET: Asset = asset!("/assets/data.json");
|
||||||
///
|
///
|
||||||
/// // Resolve the path of the asset. This will not work in web or Android bundles
|
/// // Resolve the path of the asset. This will not work in web or Android bundles
|
||||||
/// let path = dioxus::asset_resolver::asset_path(&JSON_ASSET).unwrap();
|
/// let path = dioxus::asset_resolver::asset_path(&JSON_ASSET).unwrap();
|
||||||
@@ -83,8 +82,32 @@ pub enum AssetPathError {
|
|||||||
/// let json: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
|
/// let json: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
|
||||||
/// assert_eq!(json["key"].as_str(), Some("value"));
|
/// assert_eq!(json["key"].as_str(), Some("value"));
|
||||||
/// ```
|
/// ```
|
||||||
|
///
|
||||||
|
/// ## Resolving assets from a folder
|
||||||
|
///
|
||||||
|
/// To resolve an asset from a folder, you can pass the path of the file joined with your folder asset as a string:
|
||||||
|
/// ```rust
|
||||||
|
/// # async fn asset_example() {
|
||||||
|
/// use dioxus::prelude::*;
|
||||||
|
///
|
||||||
|
/// // Bundle the whole assets folder into the application
|
||||||
|
/// static ASSETS: Asset = asset!("/assets");
|
||||||
|
///
|
||||||
|
/// // Resolve the path of the asset. This will not work in web or Android bundles
|
||||||
|
/// let path = dioxus::asset_resolver::asset_path(format!("{ASSETS}/data.json")).unwrap();
|
||||||
|
///
|
||||||
|
/// println!("Asset path: {:?}", path);
|
||||||
|
///
|
||||||
|
/// // Read the bytes of the JSON asset
|
||||||
|
/// let bytes = std::fs::read(path).unwrap();
|
||||||
|
///
|
||||||
|
/// // Deserialize the JSON data
|
||||||
|
/// let json: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
|
||||||
|
/// assert_eq!(json["key"].as_str(), Some("value"));
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
pub fn asset_path(asset: &Asset) -> Result<PathBuf, AssetPathError> {
|
pub fn asset_path(asset: impl ToString) -> Result<PathBuf, AssetPathError> {
|
||||||
#[cfg(all(feature = "web", target_arch = "wasm32"))]
|
#[cfg(all(feature = "web", target_arch = "wasm32"))]
|
||||||
return Err(AssetPathError::CannotRepresentAsPath);
|
return Err(AssetPathError::CannotRepresentAsPath);
|
||||||
|
|
||||||
@@ -125,7 +148,7 @@ pub enum AssetResolveError {
|
|||||||
/// use dioxus::prelude::*;
|
/// use dioxus::prelude::*;
|
||||||
///
|
///
|
||||||
/// // Bundle the static JSON asset into the application
|
/// // Bundle the static JSON asset into the application
|
||||||
/// static JSON_ASSET: Asset = asset!("assets/data.json");
|
/// static JSON_ASSET: Asset = asset!("/assets/data.json");
|
||||||
///
|
///
|
||||||
/// // Read the bytes of the JSON asset
|
/// // Read the bytes of the JSON asset
|
||||||
/// let bytes = dioxus::asset_resolver::read_asset_bytes(&JSON_ASSET).await.unwrap();
|
/// let bytes = dioxus::asset_resolver::read_asset_bytes(&JSON_ASSET).await.unwrap();
|
||||||
@@ -135,8 +158,27 @@ pub enum AssetResolveError {
|
|||||||
/// assert_eq!(json["key"].as_str(), Some("value"));
|
/// assert_eq!(json["key"].as_str(), Some("value"));
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
|
///
|
||||||
|
/// ## Loading assets from a folder
|
||||||
|
///
|
||||||
|
/// To load an asset from a folder, you can pass the path of the file joined with your folder asset as a string:
|
||||||
|
/// ```rust
|
||||||
|
/// # async fn asset_example() {
|
||||||
|
/// use dioxus::prelude::*;
|
||||||
|
///
|
||||||
|
/// // Bundle the whole assets folder into the application
|
||||||
|
/// static ASSETS: Asset = asset!("/assets");
|
||||||
|
///
|
||||||
|
/// // Read the bytes of the JSON asset
|
||||||
|
/// let bytes = dioxus::asset_resolver::read_asset_bytes(format!("{ASSETS}/data.json")).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"));
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
pub async fn read_asset_bytes(asset: &Asset) -> Result<Vec<u8>, AssetResolveError> {
|
pub async fn read_asset_bytes(asset: impl ToString) -> Result<Vec<u8>, AssetResolveError> {
|
||||||
let path = asset.to_string();
|
let path = asset.to_string();
|
||||||
|
|
||||||
#[cfg(feature = "web")]
|
#[cfg(feature = "web")]
|
||||||
|
|||||||
Reference in new issue
Block a user