From 6746b18313f00d6cf350f1b3a914f43c6a2b28fc Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Tue, 9 May 2017 01:37:51 +0200 Subject: Move `open_stream()` to lib.rs Make `main.rs` responsible for only the `main()` function and command line option handling. Move the actual application code to `lib.rs` for clearer separation of concerns. --- src/main.rs | 46 +++------------------------------------------- 1 file changed, 3 insertions(+), 43 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index fcaca7a..937ec95 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,54 +1,14 @@ extern crate getopts; -extern crate url; + +extern crate hearurl; use getopts::Options; -use url::Url; use std::env; use std::io::{self, Write}; -use std::io::prelude::*; -use std::net::TcpListener; -use std::process::Command; const DEFAULT_PORT: u16 = 37705; -fn open_stream(browser: String, port: u16) -> io::Result<()> { - let listener = TcpListener::bind( - format!("127.0.0.1:{}", port) - )?; - - for stream in listener.incoming() { - match stream { - Ok(mut stream) => { - let mut url = String::new(); - match stream.read_to_string(&mut url) { - Ok(_) => {}, - Err(e) => writeln!(io::stderr(), "{}", e)?, - }; - - match Url::parse(url.as_str()) { - Ok(url) => { - match Command::new("open") - .arg("-a") - .arg(&browser) - .arg(&url.as_str()) - .spawn() { - Ok(_) => {}, - Err(e) => writeln!(io::stderr(), "{}", e)?, - }; - }, - Err(e) => writeln!(io::stderr(), "{}", e)?, - }; - } - Err(e) => { - writeln!(io::stderr(), "{}", e)?; - } - } - } - - Ok(()) -} - fn print_usage(opts: Options) { let brief = "Usage: hearurl -b BROWSER"; print!("{}", opts.usage(&brief)); @@ -85,7 +45,7 @@ fn main() { None => DEFAULT_PORT, }; - open_stream(browser, port).unwrap_or_else(|e| { + hearurl::open_stream(browser, port).unwrap_or_else(|e| { writeln!(io::stderr(), "{}", e) .expect("Failed printing to stderr"); }); -- cgit v1.2.3