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
use std::{
collections::BTreeMap,
fs,
path::Path,
};
pub fn load_shaders<T>(path: T) -> Result<BTreeMap<String, String>, String>
where
T: AsRef<Path>,
{
let mut map = BTreeMap::new();
let files = fs::read_dir(path)
.map_err(|_| "Could not read shader directory".to_string())?;
for p in files {
let path = p.map_err(|_| "Got a bad file path".to_string())?.path();
if path.is_dir() {
continue;
}
match path.extension() {
Some(x) if x.to_str() == Some("frag") => (),
_ => {
continue;
},
}
let name = path
.file_stem()
.ok_or("Could not infer shader name from file name")?
.to_str()
.ok_or("Could not convert file name to UTF8 string")?
.to_string();
let contents = fs::read_to_string(path)
.map_err(|_| "Could not get shader contents")?;
map.insert(name, contents);
}
Ok(map)
}