2023-04-15 04:01:35 +00:00
|
|
|
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 {
|
2023-10-23 01:49:31 +00:00
|
|
|
let out_dir = std::env::var("OUT_DIR").unwrap_or_default();
|
2023-04-15 04:01:35 +00:00
|
|
|
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)?))
|
|
|
|
}
|