use crate::errors::*;
use glob::glob;
use lazy_static::lazy_static;
use serde::de::DeserializeOwned;
use std::{
fs::File,
future::Future,
io::{prelude::*, BufReader},
};
use tokio::runtime::Runtime;
lazy_static! {
static ref RUNTIME: Runtime = Runtime::new().unwrap();
}
pub fn load_file<T>(file: &str) -> Result<T>
where
T: DeserializeOwned,
{
let mut contents = String::new();
let mut file = BufReader::new(File::open(file)?);
file.read_to_string(&mut contents)?;
Ok(toml::from_str(&contents)?)
}
pub fn load_path<T>(path: &str) -> Result<T>
where
T: DeserializeOwned,
{
let mut contents = String::new();
for entry in glob(&format!("{}/*.toml", path)).expect("Failed to read glob pattern") {
match entry {
Ok(path) => {
let mut file = BufReader::new(File::open(path)?);
file.read_to_string(&mut contents)?;
}
Err(e) => println!("{:?}", e),
}
}
Ok(toml::from_str(&contents)?)
}
pub trait FutureExt: Future
where
Self: Sized,
{
fn sync(self) -> Self::Output {
RUNTIME.block_on(self)
}
}
impl<F> FutureExt for F where F: Future + Sized {}