~julienxx/asuka

f62c618a541e89507819ede56302f3820b5a76a7 — Arav K a month ago e21d90f
Replace `absolute::make` with `Url::join`

`Url::join` implements all the functionality of `absolute::make` when it
is given the current URL as the base.  As such, the `absolute` module
has been removed in its entirety, and now-unused functions removed.

In addition, some functions have been transitioned to using `Url`s
instead of normal strings for URLs.
3 files changed, 16 insertions(+), 129 deletions(-)

D src/absolute.rs
M src/history.rs
M src/main.rs
D src/absolute.rs => src/absolute.rs +0 -89
@@ 1,89 0,0 @@
use url::Url;

pub fn make(url: &str) -> Result<url::Url, url::ParseError> {
    // Creates an absolute link if needed
    match super::history::get_current_host() {
        Some(host) => {
            if url.starts_with("gemini://") {
                Url::parse(url)
            } else if url.starts_with("//") {
                Url::parse(&format!("gemini:{}", url))
            } else if url.starts_with('/') {
                Url::parse(&format!("gemini://{}{}", host, url))
            } else {
                let current_host_path = super::history::get_current_url().unwrap();
                Url::parse(&format!("{}{}", current_host_path, url))
            }
        }
        None => {
            if url.starts_with("gemini://") {
                Url::parse(url)
            } else if url.starts_with("//") {
                Url::parse(&format!("gemini:{}", url))
            } else {
                Url::parse(&format!("gemini://{}", url))
            }
        }
    }
}

#[test]
fn test_make_absolute_full_url() {
    super::history::append("gemini://typed-hole.org");
    let url = "gemini://typed-hole.org/foo";
    let expected_url = Url::parse("gemini://typed-hole.org/foo").unwrap();
    let absolute_url = make(&url).unwrap();
    assert_eq!(expected_url, absolute_url);
}
#[test]
fn test_make_absolute_full_url_no_protocol() {
    super::history::append("gemini://typed-hole.org");
    let url = "//typed-hole.org/foo";
    let expected_url = Url::parse("gemini://typed-hole.org/foo").unwrap();
    let absolute_url = make(&url).unwrap();
    assert_eq!(expected_url, absolute_url);
}
#[test]
fn test_make_absolute_slash_path() {
    super::history::append("gemini://typed-hole.org");
    let url = "/foo";
    let expected_url = Url::parse("gemini://typed-hole.org/foo").unwrap();
    let absolute_url = make(&url).unwrap();
    assert_eq!(expected_url, absolute_url);
}
#[test]
fn test_make_absolute_just_path() {
    super::history::append("gemini://typed-hole.org");
    let url = "foo";
    let expected_url = Url::parse("gemini://typed-hole.org/foo").unwrap();
    let absolute_url = make(&url).unwrap();
    assert_eq!(expected_url, absolute_url);
}
#[test]
fn test_make_absolute_full_url_no_current_host() {
    let url = "gemini://typed-hole.org/foo";
    let expected_url = Url::parse("gemini://typed-hole.org/foo").unwrap();
    let absolute_url = make(&url).unwrap();
    assert_eq!(expected_url, absolute_url);
}
#[test]
fn test_make_absolute_full_url_no_protocol_no_current_host() {
    let url = "//typed-hole.org/foo";
    let expected_url = Url::parse("gemini://typed-hole.org/foo").unwrap();
    let absolute_url = make(&url).unwrap();
    assert_eq!(expected_url, absolute_url);
}
#[test]
fn test_make_absolute_slash_path_no_current_host() {
    let url = "/foo";
    let expected_url = Url::parse("gemini://typed-hole.org/foo").unwrap();
    let absolute_url = make(&url).unwrap();
    assert_eq!(expected_url, absolute_url);
}
#[test]
fn test_make_absolute_just_path_no_current_host() {
    let url = "foo";
    let expected_url = Url::parse("gemini://typed-hole.org/foo").unwrap();
    let absolute_url = make(&url).unwrap();
    assert_eq!(expected_url, absolute_url);
}

M src/history.rs => src/history.rs +2 -22
@@ 10,29 10,9 @@ pub fn append(url: &str) {
    HISTORY.lock().unwrap().push(url)
}

pub fn get_current_host() -> Option<String> {
pub fn get_current_url() -> Option<Url> {
    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,
    }
    history.last().cloned()
}

pub fn get_previous_url() -> Option<Url> {

M src/main.rs => src/main.rs +14 -18
@@ 23,7 23,6 @@ use status::Status;
mod link;
use link::Link;

mod absolute;
mod bookmarks;
mod content;
mod history;


@@ 134,7 133,7 @@ fn go_back(s: &mut Cursive) {
fn add_bookmark(s: &mut Cursive) {
    let current_url = history::get_current_url();
    if let Some(url) = current_url {
        bookmarks::add(&url);
        bookmarks::add(&url.to_string());
        s.add_layer(Dialog::info("Bookmark added."));
    }
}


@@ 171,20 170,15 @@ fn visit_url(s: &mut Cursive, url: &Url) {
        s.pop_layer();
    }

    match absolute::make(url.as_str()) {
        Ok(url) => match content::get_data(&url) {
            Ok((meta, new_content)) => {
                history::append(url.as_str());
                // handle meta header
                let response = handle_response_status(s, &url, meta, new_content);
                draw_content(s, &url, response);
            }
            Err(msg) => {
                s.add_layer(Dialog::info(msg));
            }
        },
        Err(_) => {
            s.add_layer(Dialog::info(format!("Could not parse {}", url.as_str())));
    match content::get_data(&url) {
        Ok((meta, new_content)) => {
            history::append(url.as_str());
            // handle meta header
            let response = handle_response_status(s, &url, meta, new_content);
            draw_content(s, &url, response);
        }
        Err(msg) => {
            s.add_layer(Dialog::info(msg));
        }
    }
}


@@ 355,7 349,8 @@ fn follow_line(s: &mut Cursive, line: &str) {

    if let Ok(data) = parsed {
        if link::is_gemini(&data) {
            let next_url = absolute::make(&data["url"].to_string()).expect("Not an URL");
            let current_url = history::get_current_url().unwrap();
            let next_url = current_url.join(&data["url"].to_string()).expect("Not a URL");
            visit_url(s, &next_url)
        } else {
            open::that(data["url"].to_string()).unwrap();


@@ 364,6 359,7 @@ fn follow_line(s: &mut Cursive, line: &str) {
}

fn follow_link(s: &mut Cursive, link: &str) {
    let next_url = absolute::make(link).expect("Not an URL");
    let current_url = history::get_current_url().unwrap();
    let next_url = current_url.join(link).expect("Not a URL");
    visit_url(s, &next_url)
}