Skip to content

Commit

Permalink
Rename enum Message to Event
Browse files Browse the repository at this point in the history
  • Loading branch information
cesare committed Mar 5, 2021
1 parent 5c961db commit e39e2ef
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/bin/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tokio_tungstenite::tungstenite::Error as WsError;
use tokio_tungstenite::tungstenite::Message as WsMessage;

use slack_client::client::SlackApiClient;
use slack_client::events::{Message, MessageEvent};
use slack_client::events::{Event, MessageEvent};
use slack_client::requests::{PostMessageRequest, RtmConnectRequest};
use slack_client::responses::RtmConnect;

Expand Down Expand Up @@ -46,12 +46,12 @@ impl MessageHandler for PingMessageHandler {
}

struct MessageListener {
rx: Mutex<Receiver<Message>>,
rx: Mutex<Receiver<Event>>,
ping_handler: PingMessageHandler,
}

impl MessageListener {
fn new(rx: Receiver<Message>) -> Self {
fn new(rx: Receiver<Event>) -> Self {
MessageListener {
rx: Mutex::new(rx),
ping_handler: PingMessageHandler::new(),
Expand All @@ -66,9 +66,9 @@ impl MessageListener {
}
}

async fn handle_message(&self, msg: &Message) -> Result<()> {
async fn handle_message(&self, msg: &Event) -> Result<()> {
match msg {
Message::Message { channel, user, text, ..} => {
Event::Message { channel, user, text, ..} => {
let event = MessageEvent::new(channel, user, text);
if self.ping_handler.matches(&event) {
self.ping_handler.handle(&event).await?;
Expand All @@ -80,12 +80,12 @@ impl MessageListener {
}
}

async fn wait_for_messages<S>(stream: &mut S, tx: &mut Sender<Message>)
async fn wait_for_messages<S>(stream: &mut S, tx: &mut Sender<Event>)
where S: Stream<Item = Result<WsMessage, WsError>> + Unpin + Send
{
while let Some(Ok(message)) = stream.next().await {
if let WsMessage::Text(text) = message {
if let Ok(msg) = serde_json::from_str::<Message>(&text) {
if let Ok(msg) = serde_json::from_str::<Event>(&text) {
let _ = tx.send(msg).await;
}
}
Expand All @@ -100,7 +100,7 @@ async fn main() -> Result<()> {
let rtm_connect: RtmConnect = client.request(&request).await?;
println!("{:?}", rtm_connect);

let (mut tx, rx) = channel::<Message>(100);
let (mut tx, rx) = channel::<Event>(100);
let msg_handle = tokio::spawn(async move {
MessageListener::new(rx).run().await;
});
Expand Down
2 changes: 1 addition & 1 deletion src/bin/rtm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async fn wait_for_events(stream: &mut (dyn Stream<Item = Result<Message, WsError
Message::Text(text) => {
let json: Value = serde_json::from_str(&text)?;
let original_json = json.clone();
if let Ok(msg) = serde_json::from_value::<events::Message>(json) {
if let Ok(msg) = serde_json::from_value::<events::Event>(json) {
println!("{:?}", msg);
} else {
println!("{}", original_json);
Expand Down
2 changes: 1 addition & 1 deletion src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::Deserialize;

#[derive(Deserialize, Debug)]
#[serde(tag = "type")]
pub enum Message {
pub enum Event {
#[serde(rename = "hello")]
Hello {},

Expand Down

0 comments on commit e39e2ef

Please sign in to comment.