aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2017-05-09 01:37:51 +0200
committerTeddy Wing2017-05-09 01:37:51 +0200
commit6746b18313f00d6cf350f1b3a914f43c6a2b28fc (patch)
treeb6274f99df0054034bfbebcceb746d98884439b6
parentf0d5da113c59f364ffea3e840a9e871882478051 (diff)
downloadHearURL-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.
-rw-r--r--src/lib.rs45
-rw-r--r--src/main.rs46
2 files changed, 48 insertions, 43 deletions
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..19dcc97
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,45 @@
+extern crate url;
+
+use url::Url;
+
+use std::io::{self, Write};
+use std::io::prelude::*;
+use std::net::TcpListener;
+use std::process::Command;
+
+pub 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(())
+}
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");
});