pub trait FromMessage<'a>: Sized {
// Required method
fn from_message(msg: &Message<'a>) -> Result<Self, DeError>;
// Provided method
fn from_str(s: &'a str) -> Result<Self, DeError> { ... }
}Expand description
Extract custom data structures from IRC messages.
This trait allows you to convert IRC messages into your own types, providing type-safe access to message fields.
use ircv3_parse::{message::de::FromMessage, DeError};
struct BasicMessage<'a> {
color: Option<&'a str>,
nick: &'a str,
message: &'a str,
}
impl<'a> FromMessage<'a> for BasicMessage<'a> {
fn from_message(msg: &ircv3_parse::Message<'a>) -> Result<Self, ircv3_parse::DeError> {
let command = msg.command();
if !command.is_privmsg() {
return Err(DeError::invalid_command("PRIVMSG", command.as_str()));
}
let tags = msg.tags().ok_or(DeError::missing_tags())?;
let color = tags.get("color").map(|v| v.as_str());
let source = msg.source().ok_or(DeError::missing_source())?;
let nick = source.name;
let params = msg.params();
let message = params.trailing.as_str();
Ok(Self {
color,
nick,
message
})
}
}
let input = "@color=#FF0000 :nick!user@host PRIVMSG #rust :Hello!";
let msg = BasicMessage::from_str(input)?;
// or
let msg: BasicMessage = ircv3_parse::from_str(input)?;
assert_eq!("nick", msg.nick);
assert_eq!(Some("#FF0000"), msg.color);
Required Methods§
fn from_message(msg: &Message<'a>) -> Result<Self, DeError>
Provided Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.