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
use std::{env, error, ffi::OsStr, fs, io, path::PathBuf};
lazy_static! {
static ref EXE_PATH: PathBuf = env::current_exe().unwrap();
}
pub trait Loadable
where
Self: Sized,
{
type LoadOptions;
type LoadError: error::Error + From<io::Error>;
fn load_from_bytes(data: &[u8], options: Self::LoadOptions) -> Result<Self, Self::LoadError>;
fn load_from_file<P: AsRef<OsStr>>(
path: P,
options: Self::LoadOptions,
) -> Result<Self, Self::LoadError> {
let mut full_path = EXE_PATH.clone();
full_path.set_file_name(path);
Self::load_from_bytes(&fs::read(&full_path)?, options)
}
}