[][src]Trait twitchchat::ToMessage

pub trait ToMessage {
    fn tags(&self) -> Option<TagType>;
fn prefix(&self) -> Option<&str>;
fn command(&self) -> Option<&str>;
fn args(&self) -> Option<ArgsType>;
fn data(&self) -> Option<&str>; }

Convert an IRC-like message type into something that the Twitch commands can be parsed from

Refer to this form when implementing this trait:

raw string form: @tags :prefix command args :data\r\n

Example:

use twitchchat::conversion::{TagType, ArgsType};
struct MyPrivMsg {
   tags: hashbrown::HashMap<String, String>,
   sender: String,
   channel: String,
   data: String,
}
impl MyPrivMsg {
   pub fn new<S: ToString>(channel: S, sender: S, data: S, tags: &[(S, S)]) -> Self {
       Self {
           tags: tags
               .into_iter()
               .map(|(k, v)| (k.to_string(), v.to_string()))
               .collect(),
           channel: channel.to_string(),
           sender: sender.to_string(),
           data: data.to_string(),
       }
   }
}

impl twitchchat::ToMessage for MyPrivMsg {
   fn tags(&self) -> Option<TagType<'_>> {
       Some(TagType::Map(&self.tags))
   }
   fn prefix(&self) -> Option<&str> {
       Some(self.sender.as_str())
   }
   fn command(&self) -> Option<&str> {
       Some("PRIVMSG")
   }
   fn args(&self) -> Option<ArgsType<'_>> {
       Some(ArgsType::Raw(self.channel.as_str()))
   }
   fn data(&self) -> Option<&str> {
       Some(self.data.as_str())
   }
}

let msg = MyPrivMsg::new(
   "test_channel",
   "museun",
   "hello world",
   &[("color", "#FF4500"), ("display-name", "Museun")],
);
let twitch_msg = twitchchat::Message::parse(msg);
let pm = match twitch_msg {
   twitchchat::Message::PrivMsg(pm) => pm,
   _ => unreachable!(),
};
assert_eq!(pm.user(), "museun");
assert_eq!(pm.channel(), "#test_channel");
assert_eq!(pm.message(), "hello world");
assert_eq!(pm.color().unwrap().kind, twitchchat::TwitchColor::OrangeRed);

Required methods

fn tags(&self) -> Option<TagType>

Get the tags portion of the IRC message

fn prefix(&self) -> Option<&str>

Get the prefix portion of the IRC message

fn command(&self) -> Option<&str>

Get the command portion of the IRC message

fn args(&self) -> Option<ArgsType>

Get the args portion of the IRC message

fn data(&self) -> Option<&str>

Get the data portion of the IRC message

Loading content...

Implementors

impl ToMessage for IrcMessage[src]

Loading content...