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
use std::io::Error;
use std::path::Path;
#[derive(Debug)]
pub enum FileLoadError {
Io(std::io::Error),
Custom(String),
}
impl From<std::io::Error> for FileLoadError {
fn from(e: Error) -> Self {
Self::Io(e)
}
}
#[cfg(target_arch = "wasm32")]
impl From<wasm_bindgen::JsValue> for FileLoadError {
fn from(value: wasm_bindgen::JsValue) -> Self {
let string = match js_sys::JSON::stringify(&value) {
Ok(string) => String::from(string),
Err(_) => format!("{:?}", value),
};
Self::Custom(string)
}
}
pub async fn load_file<P: AsRef<Path>>(path: P) -> Result<Vec<u8>, FileLoadError> {
#[cfg(not(target_arch = "wasm32"))]
{
use std::fs::File;
use std::io::Read;
let mut file = File::open(path)?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;
Ok(buffer)
}
#[cfg(target_arch = "wasm32")]
{
use js_sys::Uint8Array;
use wasm_bindgen::JsCast;
use wasm_bindgen_futures::JsFuture;
match web_sys::window() {
Some(window) => {
let resp_value =
JsFuture::from(window.fetch_with_str(path.as_ref().to_str().unwrap())).await?;
let resp: web_sys::Response = resp_value.dyn_into().unwrap();
let data = JsFuture::from(resp.array_buffer().unwrap()).await?;
let bytes = Uint8Array::new(&data).to_vec();
Ok(bytes)
}
None => Err(FileLoadError::Custom("Window not found!".to_owned())),
}
}
}
pub async fn exists<P: AsRef<Path>>(path: P) -> bool {
#[cfg(not(target_arch = "wasm32"))]
{
path.as_ref().exists()
}
#[cfg(target_arch = "wasm32")]
{
use wasm_bindgen::JsCast;
use wasm_bindgen_futures::JsFuture;
match web_sys::window() {
Some(window) => {
if let Ok(resp_value) =
JsFuture::from(window.fetch_with_str(path.as_ref().to_str().unwrap())).await
{
let resp: web_sys::Response = resp_value.dyn_into().unwrap();
resp.status() == 200
} else {
false
}
}
None => false,
}
}
}