~julienxx/asuka

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch

[PATCH] Replace `absolute::make` with `Url::join`

Details
Message ID
<20200903161613.26663-1-nothien@uber.space>
DKIM signature
missing
Download raw message
Patch: +16 -129
`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.
---
 src/absolute.rs | 89 -------------------------------------------------
 src/history.rs  | 24 ++-----------
 src/main.rs     | 32 ++++++++----------
 3 files changed, 16 insertions(+), 129 deletions(-)
 delete mode 100644 src/absolute.rs

diff --git a/src/absolute.rs b/src/absolute.rs
deleted file mode 100644
index 7e5bfcf..0000000
--- a/src/absolute.rs
@@ -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);
}
diff --git a/src/history.rs b/src/history.rs
index f93b180..7883697 100644
--- a/src/history.rs
+++ b/src/history.rs
@@ -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> {
diff --git a/src/main.rs b/src/main.rs
index 39b9269..50280a0 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -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)
}
-- 
2.28.0
Reply to thread Export thread (mbox)