lyra-engine/src/resources.rs

28 lines
838 B
Rust
Raw Normal View History

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 {
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)?))
}