summaryrefslogtreecommitdiff
path: root/src/browser/mod.rs
blob: 528849d8f0bdeaca81614b82fa9fed5acdef211b (plain)
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
75
76
77
78
79
80
81
82
83
84
use std::collections::HashMap;

use self::web_browser::{Browser, ROOMS_URL, WEB_BROWSER};
use crate::Config;
use thirtyfour::prelude::WebDriverResult;

mod web_browser;

/// Create a new instance of `Browser` and associate it with the static variable `WEB_BROWSER`.
/// This is an unsecure type of usage, so the block is inside the `unsafe` block
pub async fn init(driver_url: &String) {
    unsafe {
        WEB_BROWSER = Some(Browser::new(driver_url).await);
    }
}

/// Login using the credentials from the `Config`. 'Cause its kind of nature
/// this is an `unsafe` block, so the function is defined like that
pub async unsafe fn login(credentials: &Config) -> WebDriverResult<()> {
    if let Some(driver) = &WEB_BROWSER {
        driver._login(credentials).await?;
    }

    Ok(())
}

/// Get the faculties available for booking a room
pub async unsafe fn get_faculties() -> WebDriverResult<Option<HashMap<String, String>>> {
    if let Some(driver) = &WEB_BROWSER {
        if let Some(faculties) = driver.get_options("dipartimento", ROOMS_URL).await? {
            return Ok(Some(faculties));
        }
    }

    Ok(None)
}

/// Get the spaces (rooms) available to book
pub async unsafe fn get_spaces() -> WebDriverResult<Option<HashMap<String, String>>> {
    if let Some(driver) = &WEB_BROWSER {
        if let Some(spaces) = driver.get_options("space", "").await? {
            return Ok(Some(spaces));
        }
    }

    Ok(None)
}

pub async unsafe fn select_option(
    klass: &str,
    property_name: &str,
    property_value: &str,
) -> WebDriverResult<bool> {
    if let Some(driver) = &WEB_BROWSER {
        let result = driver
            .select_option_from_list(klass, property_name, property_value)
            .await?;

        return Ok(result);
    }

    Ok(false)
}

/// Get the timetable of available rooms
pub async unsafe fn get_timetable() -> WebDriverResult<Option<HashMap<String, String>>> {
    if let Some(driver) = &WEB_BROWSER {
        if let Some(timetable) = driver.get_timetable().await? {
            return Ok(Some(timetable));
        }
    }

    Ok(None)
}

pub async unsafe fn select_table_row(index: &str) -> WebDriverResult<bool> {
    if let Some(driver) = &WEB_BROWSER {
        let result = driver.select_timetable_row(index).await?;

        return Ok(result);
    }

    Ok(false)
}