robotnik/src/event.rs

39 lines
980 B
Rust
Raw Normal View History

2025-11-22 12:20:56 +00:00
//! Internal representations of incoming events.
use irc::proto::{Command, Message};
2025-11-09 06:02:38 +00:00
use serde::{Deserialize, Serialize};
2025-11-22 12:20:56 +00:00
/// Represents an event. Probably from IRC.
2025-11-09 06:02:38 +00:00
#[derive(Deserialize, Serialize)]
pub struct Event {
2025-11-22 12:20:56 +00:00
/// Who is the message from?
from: String,
2025-11-22 12:20:56 +00:00
/// What is the message?
2025-11-09 06:02:38 +00:00
message: String,
}
impl Event {
2025-11-22 12:20:56 +00:00
/// Creates a new message.
pub fn new(from: impl Into<String>, msg: impl Into<String>) -> Self {
2025-11-09 06:02:38 +00:00
Self {
from: from.into(),
2025-11-09 06:02:38 +00:00
message: msg.into(),
}
}
}
impl TryFrom<&Message> for Event {
type Error = &'static str;
fn try_from(value: &Message) -> Result<Self, Self::Error> {
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"),
}
}
}