From 51f401394ad245884bc9fca249dbfa96377b4232 Mon Sep 17 00:00:00 2001 From: chris west Date: Tue, 14 Jan 2020 23:27:10 -0800 Subject: [PATCH] gopher::type_for_url() --- src/gopher.rs | 34 ++++++++++++++++++++++++++++++++++ src/main.rs | 2 +- src/menu.rs | 2 +- src/ui.rs | 4 ++-- 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/gopher.rs b/src/gopher.rs index 482894e..5f88b69 100644 --- a/src/gopher.rs +++ b/src/gopher.rs @@ -217,6 +217,27 @@ impl<'a> Url<'a> { } } +/// Given a Gopher URL, returns a gopher::Type. +pub fn type_for_url(url: &str) -> Type { + if url.starts_with("telnet://") { + return Type::Telnet; + } + + if url.starts_with("URL:") || url.starts_with("/URL:") { + return Type::HTML; + } + + if let Some(idx) = url.find('/') { + if url.len() > idx { + if let Some(t) = url.chars().nth(idx + 1) { + return Type::from(t).unwrap_or(Type::Menu); + } + } + } + + Type::Menu +} + /// Parses gopher URL into parts. /// Returns (Type, host, port, sel) pub fn parse_url<'a>(url: &'a str) -> Url<'a> { @@ -397,4 +418,17 @@ mod tests { assert_eq!(url.port, "6502"); assert_eq!(url.sel, "/"); } + + #[test] + fn test_type_for_url() { + assert_eq!(type_for_url("phkt.io"), Type::Menu); + assert_eq!(type_for_url("phkt.io/1"), Type::Menu); + assert_eq!(type_for_url("phkt.io/1/"), Type::Menu); + assert_eq!(type_for_url("phkt.io/0/info.txt"), Type::Text); + assert_eq!(type_for_url("URL:https://google.com"), Type::HTML); + assert_eq!( + type_for_url("telnet://bbs.inter.net:6502/connect"), + Type::Telnet + ); + } } diff --git a/src/main.rs b/src/main.rs index b80cadb..7b675e5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -123,7 +123,7 @@ fn print_raw(url: &str, tls: bool, tor: bool) -> i32 { /// (like a pipe). fn print_plain(url: &str, tls: bool, tor: bool) -> i32 { let mut out = String::new(); - let gopher::Url { typ, .. } = gopher::parse_url(url); + let typ = gopher::type_for_url(url); match gopher::fetch_url(url, tls, tor) { Ok((_, response)) => match typ { gopher::Type::Menu => { diff --git a/src/menu.rs b/src/menu.rs index 130f7ca..2ef8121 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -632,7 +632,7 @@ impl Menu { if let Some(line) = self.link(self.link) { let url = line.url.to_string(); - let gopher::Url { typ, .. } = gopher::parse_url(&url); + let typ = gopher::type_for_url(&url); match typ { Type::Search => { let prompt = format!("{}> ", line.text); diff --git a/src/ui.rs b/src/ui.rs index c99397b..eb20db8 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -188,7 +188,7 @@ impl UI { } // binary downloads - let gopher::Url { typ, .. } = gopher::parse_url(url); + let typ = gopher::type_for_url(url); if typ.is_download() { self.dirty = true; return if self.confirm(&format!("Download {}?", url)) { @@ -244,7 +244,7 @@ impl UI { } else { self.spinner("", move || gopher::fetch_url(&thread_url, tls, tor))?? }; - let gopher::Url { typ, .. } = gopher::parse_url(&url); + let typ = gopher::type_for_url(&url); match typ { Type::Menu | Type::Search => Ok(Box::new(Menu::from(url, res, tls, tor))), Type::Text | Type::HTML => Ok(Box::new(Text::from(url, res, tls, tor))),