~exprez135/castor

ref: 0.8.4 castor/src/history.rs -rw-r--r-- 1.3 KiB
193de0c2Julien Blanchard [gopher] Fix parsing of h type 2 years ago
                                                                                
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
use std::sync::Mutex;
use url::Url;

lazy_static! {
    static ref HISTORY: Mutex<Vec<Url>> = Mutex::new(vec![]);
}

pub fn append(url: &str) {
    let url = Url::parse(url).unwrap();
    HISTORY.lock().unwrap().push(url)
}

pub fn get_current_host() -> Option<String> {
    let history = HISTORY.lock().unwrap();
    match history.last() {
        Some(current_url) => match current_url.host_str() {
            Some(host) => Some(String::from(host)),
            None => None,
        },
        None => None,
    }
}

pub fn get_current_url() -> Option<String> {
    let history = HISTORY.lock().unwrap();
    match history.last() {
        Some(current_url) => {
            let current_path = current_url.join("./");
            match current_path {
                Ok(path) => Some(path.to_string()),
                Err(_) => None,
            }
        }
        None => None,
    }
}

pub fn get_previous_url() -> Option<Url> {
    let mut history = HISTORY.lock().unwrap();

    if history.len() > 1 {
        history.pop(); // remove current

        if history.len() > 1 {
            history.pop() // return previous
        } else {
            history.iter().cloned().last()
        }
    } else {
        None
    }
}

#[cfg(test)]
pub(crate) fn clear() -> () {
    HISTORY.lock().unwrap().clear();
}