Skip to content

Commit

Permalink
refactor: simplify Incoming and Outgoing
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianSpeitel committed Jul 11, 2024
1 parent dae7ca7 commit 0be0a0f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 39 deletions.
34 changes: 15 additions & 19 deletions src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,45 +174,41 @@ pub(crate) struct Pinger {

/// Bevy Event for incoming IRC messages and commands
#[derive(Event, Debug, Clone)]
pub struct Incoming<T = irc::Message>(pub(crate) T);
pub struct Incoming(pub(crate) irc::Message);

impl<T> Deref for Incoming<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
impl AsRef<irc::Message> for Incoming {
fn as_ref(&self) -> &irc::Message {
&self.0
}
}

impl<T> AsRef<T> for Incoming<T> {
fn as_ref(&self) -> &T {
&self.0
impl AsRef<irc::Command> for Incoming {
fn as_ref(&self) -> &irc::Command {
&self.0.command
}
}

/// Bevy Event for outgoing IRC messages and commands
#[derive(Event, Debug, Clone)]
pub struct Outgoing<T = irc::Message>(pub(crate) T);
pub struct Outgoing(pub(crate) irc::Message);

impl<T> Deref for Outgoing<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
impl AsRef<irc::Message> for Outgoing {
fn as_ref(&self) -> &irc::Message {
&self.0
}
}

impl<T> AsRef<T> for Outgoing<T> {
fn as_ref(&self) -> &T {
&self.0
impl AsRef<irc::Command> for Outgoing {
fn as_ref(&self) -> &irc::Command {
&self.0.command
}
}

impl<T> Outgoing<T> {
impl Outgoing {
/// Create a new outgoing command event
#[inline]
#[must_use]
pub fn new(msg: T) -> Self {
Self(msg)
pub fn new(msg: impl Into<irc::Message>) -> Self {
Self(msg.into())
}
}
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ impl bevy_app::Plugin for IRCPlugin {

app.add_event::<components::Incoming>();

app.observe(observers::send::<irc_prelude::Message>)
.observe(observers::send::<irc_prelude::Command>)
app.observe(observers::send)
.observe(observers::on_ping)
.observe(observers::on_welcome);

Expand Down
21 changes: 10 additions & 11 deletions src/observers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ use bevy_utils::tracing::{debug, error, info, trace};

use crate::irc_prelude as irc;

pub fn send<T: Into<irc::Message> + std::fmt::Debug + Clone>(
trigger: Trigger<Outgoing<T>>,
sender: Query<&Sender>,
mut commands: Commands,
) {
pub fn send(trigger: Trigger<Outgoing>, sender: Query<&Sender>, mut commands: Commands) {
let msg = &trigger.event().0;
let id = trigger.entity();
let sender = match sender.get(id) {
Expand All @@ -26,20 +22,23 @@ pub fn send<T: Into<irc::Message> + std::fmt::Debug + Clone>(
}
}

pub fn on_ping(trigger: Trigger<Incoming<irc::Command>>, mut commands: Commands) {
pub fn on_ping(trigger: Trigger<Incoming>, mut commands: Commands) {
let cmd = &trigger.event().0;
let id = trigger.entity();
if let irc::Command::PING(srv, ..) = cmd {
if let irc::Command::PING(srv, ..) = &cmd.command {
debug!("Received PING");
let pong = irc::Command::PONG(srv.to_owned(), None);
commands.trigger_targets(Outgoing(pong), id);
commands.trigger_targets(Outgoing::new(pong), id);
}
}

pub fn on_welcome(trigger: Trigger<Incoming<irc::Command>>, mut commands: Commands) {
pub fn on_welcome(trigger: Trigger<Incoming>, mut commands: Commands) {
let msg = &trigger.event().0;
if let irc::Command::Response(irc::Response::RPL_WELCOME, args) = msg {
info!(message = "Registered", ?args);
if let irc::Command::Response(irc::Response::RPL_WELCOME, args) = &msg.command {
info!(
message = "Registered",
args = ?args,
);
if let Some(mut entity) = commands.get_entity(trigger.entity()) {
entity.remove::<Identifying>();
entity.insert(Registered);
Expand Down
12 changes: 5 additions & 7 deletions src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn ping(
return;
}
let ping = irc::Command::PING(String::new(), None);
commands.trigger_targets(Outgoing(ping), id);
commands.trigger_targets(Outgoing::new(ping), id);
pinger.last_ping.reset();
}
}
Expand All @@ -75,9 +75,9 @@ pub fn identify(
commands.entity(id).insert(Identifying);
info!(message = "Identifying", ?auth);
if let Some(pass) = &auth.pass {
commands.trigger_targets(Outgoing(irc::Command::PASS(pass.clone())), id);
commands.trigger_targets(Outgoing::new(irc::Command::PASS(pass.clone())), id);
}
commands.trigger_targets(Outgoing(irc::Command::NICK(auth.nick.clone())), id);
commands.trigger_targets(Outgoing::new(irc::Command::NICK(auth.nick.clone())), id);
}
}

Expand All @@ -92,7 +92,7 @@ pub fn join_channels(
info!(message = "Joining channels", ?channels);
for channel in &channels.0 {
let join = irc::Command::JOIN(channel.to_owned(), None, None);
commands.trigger_targets(Outgoing(join), id);
commands.trigger_targets(Outgoing::new(join), id);
}
}
}
Expand All @@ -117,7 +117,7 @@ pub fn request_capabilities(
.join(" ");
let req = irc::Command::CAP(None, irc::CapSubCommand::REQ, None, Some(caps));

commands.trigger_targets(Outgoing(req), id);
commands.trigger_targets(Outgoing::new(req), id);
}
}

Expand All @@ -140,8 +140,6 @@ pub fn poll_stream(
}
Some(Ok(msg)) => {
trace!(message = "Received message", ?msg);
let command = Incoming(msg.command.clone());
commands.trigger_targets(command, id);
commands.trigger_targets(Incoming(msg.clone()), id);
incoming.send(Incoming(msg));
}
Expand Down

0 comments on commit 0be0a0f

Please sign in to comment.