1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//! 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
//! ```rust
//! # async fn asset_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"));
//! # }
//! ```
use ;
/// 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.
/// 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.
///
/// ## Platform specific behavior
///
/// This function will only work on desktop platforms. It will always return an error in web and Android
/// bundles. On Android assets are bundled in the APK, and cannot be represented as paths. In web bundles,
/// Assets are fetched via HTTP requests and don't have a filesystem path.
///
/// ## Example
/// ```rust
/// use dioxus::prelude::*;
///
/// // Bundle the static JSON asset into the application
/// static JSON_ASSET: Asset = asset!("/assets/data.json");
///
/// // 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();
///
/// 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"));
/// ```
///
/// ## 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"));
/// # }
/// ```
/// An error that can occur when resolving an asset.
/// 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.
///
/// ## Errors
/// This function will return an error if the asset cannot be found or if it fails to read which may be due to I/O errors or
/// network issues.
///
/// ## Example
///
/// ```rust
/// # async fn asset_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"));
/// # }
/// ```
///
/// ## 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"));
/// # }
/// ```
pub async
/// An error that occurs when resolving a native asset.
/// An error that occurs when resolving an asset on the web.