From 0733877c7140b37affcd08f5d8e3755efe74e1ef Mon Sep 17 00:00:00 2001 From: iximeow Date: Sun, 1 Oct 2017 23:25:46 -0700 Subject: yank out more parts, decouple events and display --- tw/events.rs | 44 +++++++++++++++++ tw/mod.rs | 157 +---------------------------------------------------------- 2 files changed, 46 insertions(+), 155 deletions(-) create mode 100644 tw/events.rs (limited to 'tw') diff --git a/tw/events.rs b/tw/events.rs new file mode 100644 index 0000000..0541b0a --- /dev/null +++ b/tw/events.rs @@ -0,0 +1,44 @@ +extern crate serde_json; + +pub enum Event { + Deleted { user_id: String, twete_id: String }, + RT_RT { user_id: String, twete_id: String }, + Fav_RT { user_id: String, twete_id: String }, + Fav { user_id: String, twete_id: String }, + Unfav { user_id: String, twete_id: String }, + Followed { user_id: String }, + Unfollowed { user_id: String } +} + +impl Event { + pub fn from_json(structure: serde_json::Map) -> Option { + match &structure["event"].as_str().unwrap() { + &"follow" => Some(Event::Followed { + user_id: structure["source"]["id_str"].as_str().unwrap().to_owned() + }), + &"unfollow" => Some(Event::Unfollowed { + user_id: structure["source"]["id_str"].as_str().unwrap().to_owned() + }), + &"favorite" => Some(Event::Fav { + user_id: structure["source"]["id_str"].as_str().unwrap().to_owned(), + twete_id: structure["target_object"]["id_str"].as_str().unwrap().to_owned() + }), + &"unfavorite" => Some(Event::Unfav { + user_id: structure["source"]["id_str"].as_str().unwrap().to_owned(), + twete_id: structure["target_object"]["id_str"].as_str().unwrap().to_owned() + }), + &"favorited_retweet" => Some(Event::Fav_RT { + user_id: structure["source"]["id_str"].as_str().unwrap().to_owned(), + twete_id: structure["target_object"]["id_str"].as_str().unwrap().to_owned() + }), + &"retweeted_retweet" => Some(Event::RT_RT { + user_id: structure["source"]["id_str"].as_str().unwrap().to_owned(), + twete_id: structure["target_object"]["id_str"].as_str().unwrap().to_owned() + }), +// &"blocked" => Blocked { }, +// &"unblocked" => Unblocked { }, +// &"quoted_tweet" => ???, + e => { println!("unrecognized event: {}", e); None } + } + } +} diff --git a/tw/mod.rs b/tw/mod.rs index efc070b..540d4d0 100644 --- a/tw/mod.rs +++ b/tw/mod.rs @@ -11,6 +11,8 @@ use std::io::Write; use std::fs::OpenOptions; +pub mod events; + #[derive(Debug, Serialize, Deserialize)] pub struct User { pub id: String, @@ -28,161 +30,6 @@ impl Default for User { } } -pub mod events { - extern crate termion; - use self::termion::color; - - extern crate serde_json; - - pub struct Deleted { - user_id: String, - twete_id: String - } - - pub struct RT_RT { - user_id: String, - twete_id: String - } - - pub struct Fav_RT { - user_id: String, - twete_id: String - } - - pub struct Fav { - user_id: String, - twete_id: String - } - - pub struct Unfav { - user_id: String, - twete_id: String - } - - pub struct Followed { - user_id: String - } - - pub struct Unfollowed { - user_id: String - } - - impl Event for Deleted { - fn render(self: Box, _tweeter: &::tw::TwitterCache) { } - } - impl Event for RT_RT { - fn render(self: Box, tweeter: &::tw::TwitterCache) { - println!("---------------------------------"); - { - let user = tweeter.retrieve_user(&self.user_id).unwrap(); - println!(" +rt_rt : {} (@{})", user.name, user.handle); - } - { - ::render_twete(&self.twete_id, tweeter); - } - println!(""); - } - } - impl Event for Fav_RT { - fn render(self: Box, tweeter: &::tw::TwitterCache) { - println!("---------------------------------"); - { - let user = tweeter.retrieve_user(&self.user_id).unwrap(); - println!(" +rt_fav : {} (@{})", user.name, user.handle); - } - { - ::render_twete(&self.twete_id, tweeter); - } - println!(""); - } - } - impl Event for Fav { - fn render(self: Box, tweeter: &::tw::TwitterCache) { - println!("---------------------------------"); - { - let user = tweeter.retrieve_user(&self.user_id).unwrap(); - println!("{} +fav : {} (@{}){}", color::Fg(color::Yellow), user.name, user.handle, color::Fg(color::Reset)); - } - { - ::render_twete(&self.twete_id, tweeter); - } - println!(""); - } - } - impl Event for Unfav { - fn render(self: Box, tweeter: &::tw::TwitterCache) { - println!("---------------------------------"); - { - let user = tweeter.retrieve_user(&self.user_id).unwrap(); - println!("{} -fav : {} (@{}){}", color::Fg(color::Yellow), user.name, user.handle, color::Fg(color::Reset)); - } - { - ::render_twete(&self.twete_id, tweeter); - } - println!(""); - } - } - impl Event for Followed { - fn render(self: Box, tweeter: &::tw::TwitterCache) { - let user = tweeter.retrieve_user(&self.user_id).unwrap(); - println!("---------------------------------"); - println!(" +fl : {} (@{})", user.name, user.handle); - println!(""); - } - } - impl Event for Unfollowed { - fn render(self: Box, tweeter: &::tw::TwitterCache) { - let user = tweeter.retrieve_user(&self.user_id).unwrap(); - println!("---------------------------------"); - println!(" -fl : {} (@{})", user.name, user.handle); - println!(""); - } - } - - /* - impl Event for Blocked { - - } - */ - - pub trait Event { - fn render(self: Box, tweeter: &::tw::TwitterCache); - } - - impl Event { - pub fn from_json(structure: serde_json::Map) -> Option> { - match &structure["event"].as_str().unwrap() { - &"follow" => Some(Box::new(Followed { - user_id: structure["source"]["id_str"].as_str().unwrap().to_owned() - })), - &"unfollow" => Some(Box::new(Unfollowed { - user_id: structure["source"]["id_str"].as_str().unwrap().to_owned() - })), - &"favorite" => Some(Box::new(Fav { - user_id: structure["source"]["id_str"].as_str().unwrap().to_owned(), - twete_id: structure["target_object"]["id_str"].as_str().unwrap().to_owned() - })), - &"unfavorite" => Some(Box::new(Unfav { - user_id: structure["source"]["id_str"].as_str().unwrap().to_owned(), - twete_id: structure["target_object"]["id_str"].as_str().unwrap().to_owned() - })), - &"favorited_retweet" => Some(Box::new(Fav_RT { - user_id: structure["source"]["id_str"].as_str().unwrap().to_owned(), - twete_id: structure["target_object"]["id_str"].as_str().unwrap().to_owned() - })), - &"retweeted_retweet" => Some(Box::new(RT_RT { - user_id: structure["source"]["id_str"].as_str().unwrap().to_owned(), - twete_id: structure["target_object"]["id_str"].as_str().unwrap().to_owned() - })), -// &"blocked" => Blocked { }, -// &"unblocked" => Unblocked { }, -// &"quoted_tweet" => ???, - e => { println!("unrecognized event: {}", e); None } - } - } - } -} - impl User { pub fn from_json(json: serde_json::Value) -> Option { if let serde_json::Value::Object(json_map) = json { -- cgit v1.1