1use crate::errors::*;
13
14use glob::glob;
15use lazy_static::lazy_static;
16use serde::de::DeserializeOwned;
17use std::{
18 fs::File,
19 future::Future,
20 io::{prelude::*, BufReader},
21};
22use tokio::runtime::Runtime;
23
24lazy_static! {
25 static ref RUNTIME: Runtime = Runtime::new().unwrap();
26}
27
28pub fn load_file<T>(file: &str) -> Result<T>
30where
31 T: DeserializeOwned,
32{
33 let mut contents = String::new();
34 let mut file = BufReader::new(File::open(file)?);
35 file.read_to_string(&mut contents)?;
36 Ok(toml::from_str(&contents)?)
37}
38
39pub fn load_path<T>(path: &str) -> Result<T>
42where
43 T: DeserializeOwned,
44{
45 let mut contents = String::new();
46 for entry in glob(&format!("{}/*.toml", path)).expect("Failed to read glob pattern") {
47 match entry {
48 Ok(path) => {
49 let mut file = BufReader::new(File::open(path)?);
50 file.read_to_string(&mut contents)?;
51 }
52 Err(e) => println!("{:?}", e),
53 }
54 }
55
56 Ok(toml::from_str(&contents)?)
57}
58
59pub trait FutureExt: Future
61where
62 Self: Sized,
63{
64 fn sync(self) -> Self::Output {
66 RUNTIME.block_on(self)
67 }
68}
69
70impl<F> FutureExt for F where F: Future + Sized {}