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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use crate::errors::*;
use futures::{sync::oneshot::spawn, Future};
use glob::glob;
use lazy_static::lazy_static;
use serde::de::DeserializeOwned;
use std::{
fs::File,
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: Send + Sized + 'static,
Self::Item: Send + 'static,
Self::Error: Send + 'static,
{
fn sync(self) -> std::result::Result<Self::Item, Self::Error> {
spawn(self, &RUNTIME.executor()).wait()
}
}
impl<T: Send + 'static, I: Send + 'static, E: Send + 'static> FutureExt for T where
T: Future<Item = I, Error = E>
{
}