//! Internal representations of incoming events. use irc::proto::{Command, Message}; use serde::{Deserialize, Serialize}; /// Represents an event. Probably from IRC. #[derive(Deserialize, Serialize)] pub struct Event { /// Who is the message from? from: String, /// What is the message? message: String, } impl Event { /// Creates a new message. pub fn new(from: impl Into, msg: impl Into) -> Self { Self { from: from.into(), message: msg.into(), } } } impl TryFrom<&Message> for Event { type Error = &'static str; fn try_from(value: &Message) -> Result { let from = value.response_target().unwrap_or("unknown").to_string(); match &value.command { Command::PRIVMSG(_channel, message) => Ok(Event { from, message: message.clone(), }), _ => Err("Not a PRIVMSG"), } } }