aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib.rs
diff options
context:
space:
mode:
authorTeddy Wing2017-05-09 01:37:51 +0200
committerTeddy Wing2017-05-09 01:37:51 +0200
commit6746b18313f00d6cf350f1b3a914f43c6a2b28fc (patch)
treeb6274f99df0054034bfbebcceb746d98884439b6 /src/lib.rs
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.
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs45
1 files changed, 45 insertions, 0 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(())
+}