gopher::type_for_url()

pull/14/head
chris west 4 years ago
parent d66a9c610c
commit 51f401394a

@ -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
);
}
}

@ -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 => {

@ -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);

@ -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))),

Loading…
Cancel
Save