tinywad/properties/
file.rs

1use crate::error::WadError;
2use std::{fs::File, io::Read, path::Path};
3
4pub struct PathWrap<P: AsRef<Path>>(P);
5
6impl Into<PathWrap<&str>> for &'static str {
7    fn into(self) -> PathWrap<&'static str> {
8        PathWrap(self)
9    }
10}
11
12impl TryFrom<PathWrap<&str>> for Vec<u8> {
13    type Error = WadError;
14
15    fn try_from(value: PathWrap<&str>) -> Result<Self, Self::Error> {
16        match File::open(value.0) {
17            Ok(mut file) => {
18                let mut data = Vec::<u8>::new();
19
20                match file.read_to_end(data.as_mut()) {
21                    Ok(_) => Ok(data),
22                    Err(e) => Err(WadError::Read(e.to_string())),
23                }
24            }
25            Err(e) => Err(WadError::Read(e.to_string())),
26        }
27    }
28}