diff options
author | Teddy Wing | 2017-05-09 01:37:51 +0200 |
---|---|---|
committer | Teddy Wing | 2017-05-09 01:37:51 +0200 |
commit | 6746b18313f00d6cf350f1b3a914f43c6a2b28fc (patch) | |
tree | b6274f99df0054034bfbebcceb746d98884439b6 /src/main.rs | |
parent | f0d5da113c59f364ffea3e840a9e871882478051 (diff) | |
download | HearURL-6746b18313f00d6cf350f1b3a914f43c6a2b28fc.tar.bz2 |
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.
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 46 |
1 files changed, 3 insertions, 43 deletions
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"); }); |