1use std::path::Path;
9use std::fs::File;
10use std::io::{Read, BufReader};
11
12use crate::errors::*;
13
14use serde::de::DeserializeOwned;
15use serde_json;
16
17pub fn open_bytes<T: AsRef<Path>>(path: T) -> Result<Vec<u8>> {
19 let file = File::open(path)?;
20 let mut reader = BufReader::new(file);
21 let mut buffer: Vec<u8> = Vec::new();
22 reader.read_to_end(&mut buffer)?;
23
24 Ok(buffer)
25}
26
27pub fn open<T: AsRef<Path>>(path: T) -> Result<Box<dyn Read>> {
29 let file = Box::new(File::open(path)?);
30 Ok(file)
31}
32
33pub fn open_json<T: DeserializeOwned, P: AsRef<Path>>(path: P) -> Result<T> {
35 let file = File::open(path)?;
36 let reader = BufReader::new(file);
37 Ok(serde_json::from_reader(reader)?)
38}