#![allow(missing_debug_implementations, missing_copy_implementations)]
use super::messages;
use crate::AsOwned;
use std::fmt::Debug;
#[doc(hidden)]
pub trait Event<'a>: private::EventSealed {
type Parsed: crate::Parse<&'a crate::decode::Message<'a>> + AsOwned;
}
#[doc(hidden)]
pub trait EventMapped<'a, T>: private::EventMappedSealed<T>
where
T: Event<'a>,
{
type Owned: Clone + Debug + Send + Sync + 'static;
fn into_owned(data: T::Parsed) -> Self::Owned;
}
impl<'a, T> EventMapped<'a, T> for T
where
T: Event<'a>,
<T::Parsed as AsOwned>::Owned: Send + Sync + 'static,
<T::Parsed as AsOwned>::Owned: Clone + Debug,
{
type Owned = <T::Parsed as AsOwned>::Owned;
fn into_owned(data: T::Parsed) -> Self::Owned {
<T::Parsed as AsOwned>::as_owned(&data)
}
}
mod private {
use super::{Event, EventMapped};
pub trait EventSealed {}
impl<'a, T: Event<'a>> EventSealed for T {}
pub trait EventMappedSealed<E> {}
impl<'a, T: EventMapped<'a, E>, E: Event<'a>> EventMappedSealed<E> for T {}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn event_mapped() {
fn e<'a, T>(msg: &'a crate::decode::Message<'a>) -> T::Owned
where
T: Event<'a> + 'static,
T: EventMapped<'a, T>,
{
use crate::Parse as _;
T::into_owned(T::Parsed::parse(msg).unwrap())
}
let msg = crate::decode("PING :1234567890\r\n")
.next()
.unwrap()
.unwrap();
let msg: crate::messages::Ping<'static> = e::<Ping>(&msg);
assert_eq!(msg.token, "1234567890")
}
}
#[non_exhaustive]
pub struct Cap;
impl<'t> Event<'t> for Cap {
type Parsed = messages::Cap<'t>;
}
#[non_exhaustive]
pub struct ClearChat;
impl<'t> Event<'t> for ClearChat {
type Parsed = messages::ClearChat<'t>;
}
#[non_exhaustive]
pub struct ClearMsg;
impl<'t> Event<'t> for ClearMsg {
type Parsed = messages::ClearMsg<'t>;
}
#[non_exhaustive]
pub struct GlobalUserState;
impl<'t> Event<'t> for GlobalUserState {
type Parsed = messages::GlobalUserState<'t>;
}
#[non_exhaustive]
pub struct HostTarget;
impl<'t> Event<'t> for HostTarget {
type Parsed = messages::HostTarget<'t>;
}
#[non_exhaustive]
pub struct IrcReady;
impl<'t> Event<'t> for IrcReady {
type Parsed = messages::IrcReady<'t>;
}
#[non_exhaustive]
pub struct Join;
impl<'t> Event<'t> for Join {
type Parsed = messages::Join<'t>;
}
#[non_exhaustive]
pub struct Notice;
impl<'t> Event<'t> for Notice {
type Parsed = messages::Notice<'t>;
}
#[non_exhaustive]
pub struct Part;
impl<'t> Event<'t> for Part {
type Parsed = messages::Part<'t>;
}
#[non_exhaustive]
pub struct Ping;
impl<'t> Event<'t> for Ping {
type Parsed = messages::Ping<'t>;
}
#[non_exhaustive]
pub struct Pong;
impl<'t> Event<'t> for Pong {
type Parsed = messages::Pong<'t>;
}
#[non_exhaustive]
pub struct Privmsg;
impl<'t> Event<'t> for Privmsg {
type Parsed = messages::Privmsg<'t>;
}
#[non_exhaustive]
pub struct Raw;
impl<'t> Event<'t> for Raw {
type Parsed = messages::Raw<'t>;
}
#[non_exhaustive]
pub struct Ready;
impl<'t> Event<'t> for Ready {
type Parsed = messages::Ready<'t>;
}
#[non_exhaustive]
pub struct Reconnect;
impl<'t> Event<'t> for Reconnect {
type Parsed = messages::Reconnect;
}
#[non_exhaustive]
pub struct RoomState;
impl<'t> Event<'t> for RoomState {
type Parsed = messages::RoomState<'t>;
}
#[non_exhaustive]
pub struct UserNotice;
impl<'t> Event<'t> for UserNotice {
type Parsed = messages::UserNotice<'t>;
}
#[non_exhaustive]
pub struct UserState;
impl<'t> Event<'t> for UserState {
type Parsed = messages::UserState<'t>;
}
#[non_exhaustive]
pub struct Whisper;
impl<'t> Event<'t> for Whisper {
type Parsed = messages::Whisper<'t>;
}
#[non_exhaustive]
pub struct All;
impl<'t> Event<'t> for All {
type Parsed = messages::AllCommands<'t>;
}