aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/factoids/utils.rs
blob: 89f5d6c566fafcc65f6a49d7582e64c76d2b1034 (plain) (blame)
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
extern crate reqwest;

use std::thread;
use std::time::Duration;

use super::rlua::Error as LuaError;
use super::rlua::Lua;
use utils::Url;
use utils::error::ErrorKind::Connection;

use failure::Fail;

pub fn download(_: &Lua, url: String) -> Result<String, LuaError> {
    let url = Url::from(url).max_kib(1024);
    match url.request() {
        Ok(v) => Ok(v),
        Err(e) => {
            let error = match e.kind() {
                Connection => e.cause().unwrap().to_string(),
                _ => e.to_string(),
            };

            Err(LuaError::RuntimeError(format!(
                "Failed to download {} - {}",
                url.as_str(),
                error
            )))
        }
    }
}

pub fn sleep(_: &Lua, dur: u64) -> Result<(), LuaError> {
    thread::sleep(Duration::from_millis(dur));
    Ok(())
}