webdriver_install/
geckodriver.rs

1use crate::DriverFetcher;
2use eyre::{eyre, Result};
3use url::Url;
4
5pub struct Geckodriver;
6
7impl DriverFetcher for Geckodriver {
8    const BASE_URL: &'static str = "https://github.com/mozilla/geckodriver/releases";
9
10    /// Returns the latest version of the driver
11    fn latest_version(&self) -> Result<String> {
12        let latest_release_url = format!("{}/latest", Self::BASE_URL);
13        let resp = reqwest::blocking::get(&latest_release_url)?;
14        let url = resp.url();
15        Ok(url.path_segments().unwrap().last().unwrap().to_string())
16    }
17
18    /// Returns the download url for the driver executable
19    fn direct_download_url(&self, version: &str) -> Result<Url> {
20        Ok(Url::parse(&format!(
21            "{}/download/{version}/geckodriver-{version}-{platform}",
22            Self::BASE_URL,
23            version = version,
24            platform = Self::platform()?
25        ))?)
26    }
27}
28
29impl Geckodriver {
30    pub fn new() -> Self {
31        Self {}
32    }
33
34    fn platform() -> Result<String> {
35        match sys_info::os_type()?.as_str() {
36            "Linux" => Ok(format!("linux{}.tar.gz", Self::pointer_width())),
37            "Darwin" => Ok(String::from("macos.tar.gz")),
38            "Windows" => Ok(format!("win{}.zip", Self::pointer_width())),
39            other => Err(eyre!(
40                "webdriver-install doesn't support '{}' currently",
41                other
42            )),
43        }
44    }
45
46    const fn pointer_width() -> usize {
47        #[cfg(target_pointer_width = "32")]
48        {
49            32
50        }
51        #[cfg(target_pointer_width = "64")]
52        {
53            64
54        }
55    }
56}
57
58#[test]
59fn direct_download_url_test() {
60    #[cfg(target_os = "linux")]
61    assert_eq!(
62        "https://github.com/mozilla/geckodriver/releases/download/v1/geckodriver-v1-linux64.tar.gz",
63        Geckodriver::new()
64            .direct_download_url("v1")
65            .unwrap()
66            .to_string()
67    );
68    #[cfg(target_os = "macos")]
69    assert_eq!(
70        "https://github.com/mozilla/geckodriver/releases/download/v1/geckodriver-v1-macos.tar.gz",
71        Geckodriver::new()
72            .direct_download_url("v1")
73            .unwrap()
74            .to_string()
75    );
76    #[cfg(target_os = "windows")]
77    assert_eq!(
78        "https://github.com/mozilla/geckodriver/releases/download/v1/geckodriver-v1-win64.zip",
79        Geckodriver::new()
80            .direct_download_url("v1")
81            .unwrap()
82            .to_string()
83    );
84}