28 lines
838 B
Rust
28 lines
838 B
Rust
|
use cfg_if::cfg_if;
|
||
|
|
||
|
/// Load bytes from a file.
|
||
|
pub async fn load_bytes(file_name: &str) -> anyhow::Result<Vec<u8>> {
|
||
|
cfg_if! {
|
||
|
if #[cfg(target_arch = "wasm32")] {
|
||
|
let url = format_url(file_name);
|
||
|
let bytes = reqwest::get(url)
|
||
|
.await?
|
||
|
.bytes()
|
||
|
.await?;
|
||
|
} else {
|
||
|
let out_dir = std::env::var("OUT_DIR").unwrap_or(String::new());
|
||
|
let path = std::path::Path::new(&out_dir)
|
||
|
.join("res")
|
||
|
.join(file_name);
|
||
|
let bytes = std::fs::read(path)?;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Ok(bytes)
|
||
|
}
|
||
|
|
||
|
/// Load the string content from a file.
|
||
|
pub async fn load_string(file_name: &str) -> anyhow::Result<String> {
|
||
|
let bytes = load_bytes(file_name).await?;
|
||
|
Ok(String::from(std::str::from_utf8(&bytes)?))
|
||
|
}
|