From 4554b2a4533181886446985c9858f1e49deb4670 Mon Sep 17 00:00:00 2001 From: David Li Date: Sat, 31 Oct 2015 17:58:55 -0400 Subject: Restructure modules --- src/main.rs | 106 ++++++++------------------------------------------ src/message.rs | 103 ------------------------------------------------ src/message/mod.rs | 103 ++++++++++++++++++++++++++++++++++++++++++++++++ src/pushbullet/mod.rs | 54 +++++++++++++++++++++++++ 4 files changed, 174 insertions(+), 192 deletions(-) delete mode 100644 src/message.rs create mode 100644 src/message/mod.rs create mode 100644 src/pushbullet/mod.rs (limited to 'src') diff --git a/src/main.rs b/src/main.rs index b12f5a6..7570dcb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,63 +3,13 @@ use std::io::prelude::*; extern crate notify_rust; extern crate toml; -extern crate websocket; -extern crate rustc_serialize; use notify_rust::Notification; -mod message; +pub mod message; use message::{Message, Push}; -use websocket::Sender; -use websocket::Receiver; -use websocket::WebSocketStream; -use websocket::DataFrame; -use websocket::Client; - -struct PBClient { - client: websocket::client::Client, - websocket::client::receiver::Receiver> -} - -impl PBClient { - fn new(token: &str) -> PBClient { - use websocket::client::request::Url; - use websocket::Client; - - let wss_url = format!("wss://stream.pushbullet.com/websocket/{}", token); - let url = Url::parse(wss_url.as_ref()).unwrap(); - let request = Client::connect(url).unwrap(); - let response = request.send().unwrap(); - response.validate().unwrap(); - - PBClient { - client: response.begin() - } - } - - fn process_message(message: Result) -> Option { - let message = match message { - Ok(m) => m, - Err(e) => { - println!("Error: {:?}", e); - return None; - } - }; - - if let websocket::Message::Text(message) = message { - Message::parse(message.as_ref()) - } - else { - None - } - } - - fn messages<'a>(&'a mut self) -> Box + 'a> { - let mut receiver = self.client.get_mut_reciever(); // there is a typo in the API - Box::new(receiver.incoming_messages().filter_map(PBClient::process_message)) - } -} +pub mod pushbullet; +use pushbullet::PBClient; fn main() { let mut cfg_file = File::open("config.toml").expect("Could not find config.toml."); @@ -76,45 +26,23 @@ fn main() { .expect("'token' should be a string"); let mut client = PBClient::new(token.as_ref()); - let mut receiver = client.client.get_mut_reciever(); - - for message in receiver.incoming_messages() { - let message = match message { - Ok(m) => m, - Err(e) => { - println!("Error: {:?}", e); - return; - } - }; + for message in client.messages() { match message { - websocket::Message::Close(_) => { - return; - } - websocket::Message::Text(message) => { - let msg = Message::parse(message.as_ref()); - if let Some(msg) = msg { - match msg { - Message::Push(Push::Mirror { - title, - body, - application_name, - .. - }) => { - let title = format!("{}: {}", application_name, title); - Notification::new() - .body(body.as_ref()) - .summary(title.as_ref()) - .show() - .unwrap(); - } - _ => {} - } - } - } - _ => { - println!("Got {:?}", message); + Message::Push(Push::Mirror { + title, + body, + application_name, + .. + }) => { + let title = format!("{}: {}", application_name, title); + Notification::new() + .body(body.as_ref()) + .summary(title.as_ref()) + .show() + .unwrap(); } + _ => {} } } } diff --git a/src/message.rs b/src/message.rs deleted file mode 100644 index 0617c4d..0000000 --- a/src/message.rs +++ /dev/null @@ -1,103 +0,0 @@ -extern crate rustc_serialize; - -use self::rustc_serialize::{json, Decodable, Decoder}; - -#[derive(Debug)] -pub enum Push { - Mirror { - notification_id: i32, - title: String, - body: String, - application_name: String, - package_name: String, - icon: String - }, - Dismissal { - notification_id: i32 - } -} - -#[derive(Debug)] -pub enum Message { - Nop, - Push(Push) -} - -impl Message { - pub fn parse(message: &str) -> Option { - let json = json::Json::from_str(message).unwrap(); - let mut decoder = json::Decoder::new(json); - let result : Result = Decodable::decode(&mut decoder); - result.ok() - } -} - -impl Decodable for Push { - fn decode(d: &mut D) -> Result { - d.read_struct("root", 0, |d| { - let type_ : String = try!(d.read_struct_field("type", 0, Decodable::decode)); - match type_.as_ref() { - "mirror" => { - let id = try!( - d.read_struct_field("notification_id", 0, Decodable::decode)); - let title = try!( - d.read_struct_field("title", 0, Decodable::decode)); - let body = try!( - d.read_struct_field("body", 0, Decodable::decode)); - let app_name = try!( - d.read_struct_field("application_name", 0, Decodable::decode)); - let package_name = try!( - d.read_struct_field("package_name", 0, Decodable::decode)); - let icon = try!( - d.read_struct_field("icon", 0, Decodable::decode)); - - Ok(Push::Mirror { - title: title, - body: body, - application_name: app_name, - package_name: package_name, - notification_id: id, - icon: icon - }) - } - "dismissal" => { - let id = try!( - d.read_struct_field("notification_id", 0, Decodable::decode)); - - Ok(Push::Dismissal { - notification_id: id - }) - } - _ => { - let err_msg = format!( - "Invalid value for push type: {} (expected 'mirror' or 'dismissal')", - type_); - Err(d.error(err_msg.as_ref())) - } - } - }) - } -} - -impl Decodable for Message { - fn decode(d: &mut D) -> Result { - d.read_struct("root", 0, |d| { - let type_ : String = try!(d.read_struct_field("type", 0, Decodable::decode)); - - match type_.as_ref() { - "nop" => Ok(Message::Nop), - "push" => { - d.read_struct_field("push", 0, |d| { - Ok(Message::Push(try!(Decodable::decode(d)))) - }) - }, - _ => { - let err_msg = format!( - "Invalid value for message type: {}", - type_); - Err(d.error(err_msg.as_ref())) - } - } - }) - } -} diff --git a/src/message/mod.rs b/src/message/mod.rs new file mode 100644 index 0000000..0617c4d --- /dev/null +++ b/src/message/mod.rs @@ -0,0 +1,103 @@ +extern crate rustc_serialize; + +use self::rustc_serialize::{json, Decodable, Decoder}; + +#[derive(Debug)] +pub enum Push { + Mirror { + notification_id: i32, + title: String, + body: String, + application_name: String, + package_name: String, + icon: String + }, + Dismissal { + notification_id: i32 + } +} + +#[derive(Debug)] +pub enum Message { + Nop, + Push(Push) +} + +impl Message { + pub fn parse(message: &str) -> Option { + let json = json::Json::from_str(message).unwrap(); + let mut decoder = json::Decoder::new(json); + let result : Result = Decodable::decode(&mut decoder); + result.ok() + } +} + +impl Decodable for Push { + fn decode(d: &mut D) -> Result { + d.read_struct("root", 0, |d| { + let type_ : String = try!(d.read_struct_field("type", 0, Decodable::decode)); + match type_.as_ref() { + "mirror" => { + let id = try!( + d.read_struct_field("notification_id", 0, Decodable::decode)); + let title = try!( + d.read_struct_field("title", 0, Decodable::decode)); + let body = try!( + d.read_struct_field("body", 0, Decodable::decode)); + let app_name = try!( + d.read_struct_field("application_name", 0, Decodable::decode)); + let package_name = try!( + d.read_struct_field("package_name", 0, Decodable::decode)); + let icon = try!( + d.read_struct_field("icon", 0, Decodable::decode)); + + Ok(Push::Mirror { + title: title, + body: body, + application_name: app_name, + package_name: package_name, + notification_id: id, + icon: icon + }) + } + "dismissal" => { + let id = try!( + d.read_struct_field("notification_id", 0, Decodable::decode)); + + Ok(Push::Dismissal { + notification_id: id + }) + } + _ => { + let err_msg = format!( + "Invalid value for push type: {} (expected 'mirror' or 'dismissal')", + type_); + Err(d.error(err_msg.as_ref())) + } + } + }) + } +} + +impl Decodable for Message { + fn decode(d: &mut D) -> Result { + d.read_struct("root", 0, |d| { + let type_ : String = try!(d.read_struct_field("type", 0, Decodable::decode)); + + match type_.as_ref() { + "nop" => Ok(Message::Nop), + "push" => { + d.read_struct_field("push", 0, |d| { + Ok(Message::Push(try!(Decodable::decode(d)))) + }) + }, + _ => { + let err_msg = format!( + "Invalid value for message type: {}", + type_); + Err(d.error(err_msg.as_ref())) + } + } + }) + } +} diff --git a/src/pushbullet/mod.rs b/src/pushbullet/mod.rs new file mode 100644 index 0000000..1f2254e --- /dev/null +++ b/src/pushbullet/mod.rs @@ -0,0 +1,54 @@ +extern crate websocket; +extern crate rustc_serialize; + +use message::Message; + +use self::websocket::Sender; +use self::websocket::Receiver; +use self::websocket::WebSocketStream; +use self::websocket::DataFrame; +use self::websocket::Client; +use self::websocket::client::request::Url; + +pub struct PBClient { + client: websocket::client::Client, + websocket::client::receiver::Receiver> +} + +impl PBClient { + pub fn new(token: &str) -> PBClient { + + let wss_url = format!("wss://stream.pushbullet.com/websocket/{}", token); + let url = Url::parse(wss_url.as_ref()).unwrap(); + let request = Client::connect(url).unwrap(); + let response = request.send().unwrap(); + response.validate().unwrap(); + + PBClient { + client: response.begin() + } + } + + fn process_message(message: Result) -> Option { + let message = match message { + Ok(m) => m, + Err(e) => { + println!("Error: {:?}", e); + return None; + } + }; + + if let websocket::Message::Text(message) = message { + Message::parse(message.as_ref()) + } + else { + None + } + } + + pub fn messages<'a>(&'a mut self) -> Box + 'a> { + let mut receiver = self.client.get_mut_reciever(); // there is a typo in the API + Box::new(receiver.incoming_messages().filter_map(PBClient::process_message)) + } +} -- cgit v1.2.3