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
//! Getting WebDriver client binaries.

mod chromedriver;
mod geckodriver;
mod safaridriver;

use binary_install::Cache;
use failure;
use std::path::PathBuf;
use PBAR;

pub use self::{
    chromedriver::{get_or_install_chromedriver, install_chromedriver},
    geckodriver::{get_or_install_geckodriver, install_geckodriver},
    safaridriver::get_safaridriver,
};

// ------ driver helpers  ------

fn get_and_notify(
    cache: &Cache,
    installation_allowed: bool,
    name: &str,
    url: &str,
) -> Result<Option<PathBuf>, failure::Error> {
    if let Some(dl) = cache.download(false, name, &[name], &url)? {
        return Ok(Some(dl.binary(name)?));
    }
    if installation_allowed {
        PBAR.info(&format!("Getting {}...", name));
    }
    match cache.download(installation_allowed, name, &[name], &url)? {
        Some(dl) => Ok(Some(dl.binary(name)?)),
        None => Ok(None),
    }
}

struct Collector(Vec<u8>);

impl Collector {
    pub fn take_content(&mut self) -> Vec<u8> {
        // TODO: replace with `std::mem::take` once stable
        std::mem::replace(&mut self.0, Vec::default())
    }
}

impl curl::easy::Handler for Collector {
    fn write(&mut self, data: &[u8]) -> Result<usize, curl::easy::WriteError> {
        self.0.extend_from_slice(data);
        Ok(data.len())
    }
}