From c83df4eb62f66af24985a881d75010a6e629eab3 Mon Sep 17 00:00:00 2001 From: dvkt Date: Mon, 16 Dec 2019 21:47:33 -0800 Subject: [PATCH] -raw --- src/gopher.rs | 6 ++++++ src/main.rs | 23 +++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/gopher.rs b/src/gopher.rs index 4b1658a..27abf1b 100644 --- a/src/gopher.rs +++ b/src/gopher.rs @@ -26,6 +26,12 @@ pub enum Type { } // Fetches a URL and returns a raw Gopher response. +pub fn fetch_url(url: &str) -> io::Result { + let (_, host, port, sel) = parse_url(url); + fetch(host, port, sel) +} + +// Fetches a URL by its component parts and returns a raw Gopher response. pub fn fetch(host: &str, port: &str, selector: &str) -> io::Result { let mut body = String::new(); let stream = TcpStream::connect(format!("{}:{}", host, port)) diff --git a/src/main.rs b/src/main.rs index d38759f..fc90826 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,11 +17,21 @@ fn main() { let url = args.get(1).unwrap(); - if url == "--version" || url == "-v" || url == "-version" { + if url == "-raw" || url == "-r" || url == "--raw" { + if args.len() > 2 { + let url = args.get(2).unwrap(); + print_raw(url); + } else { + eprintln!("-raw needs gopher-url"); + } + return; + } + + if url == "-version" || url == "-v" || url == "--version" { print_version(); return; } - if url == "--help" || url == "-h" || url == "-help" { + if url == "-help" || url == "-h" || url == "--help" { print_usage(); return; } @@ -44,3 +54,12 @@ fn print_usage() { phetch -version # Show phetch version." ); } + +fn print_raw(url: &str) { + gopher::fetch_url(url) + .and_then(|x| { + println!("{}", x); + Ok(()) + }) + .map_err(|e| eprintln!("{}", e)); +}