From eb987322364f7701de3077a1127168c506017de6 Mon Sep 17 00:00:00 2001 From: Sebastian Speitel Date: Thu, 11 Jul 2024 20:52:15 +0200 Subject: [PATCH] refactor: simplify Incoming and Outgoing --- src/components.rs | 26 +++++++++++++------------- src/lib.rs | 3 +-- src/observers.rs | 21 ++++++++++----------- src/systems.rs | 12 +++++------- 4 files changed, 29 insertions(+), 33 deletions(-) diff --git a/src/components.rs b/src/components.rs index a65d62d..6c766f9 100644 --- a/src/components.rs +++ b/src/components.rs @@ -174,45 +174,45 @@ pub(crate) struct Pinger { /// Bevy Event for incoming IRC messages and commands #[derive(Event, Debug, Clone)] -pub struct Incoming(pub(crate) T); +pub struct Incoming(pub(crate) irc::Message); -impl Deref for Incoming { - type Target = T; +impl Deref for Incoming { + type Target = irc::Message; fn deref(&self) -> &Self::Target { &self.0 } } -impl AsRef for Incoming { - fn as_ref(&self) -> &T { +impl AsRef for Incoming { + fn as_ref(&self) -> &irc::Message { &self.0 } } /// Bevy Event for outgoing IRC messages and commands #[derive(Event, Debug, Clone)] -pub struct Outgoing(pub(crate) T); +pub struct Outgoing(pub(crate) irc::Message); -impl Deref for Outgoing { - type Target = T; +impl Deref for Outgoing { + type Target = irc::Message; fn deref(&self) -> &Self::Target { &self.0 } } -impl AsRef for Outgoing { - fn as_ref(&self) -> &T { +impl AsRef for Outgoing { + fn as_ref(&self) -> &irc::Message { &self.0 } } -impl Outgoing { +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) -> Self { + Self(msg.into()) } } diff --git a/src/lib.rs b/src/lib.rs index e45687b..98fb0eb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,8 +58,7 @@ impl bevy_app::Plugin for IRCPlugin { app.add_event::(); - app.observe(observers::send::) - .observe(observers::send::) + app.observe(observers::send) .observe(observers::on_ping) .observe(observers::on_welcome); diff --git a/src/observers.rs b/src/observers.rs index 39c7eef..c85e158 100644 --- a/src/observers.rs +++ b/src/observers.rs @@ -5,11 +5,7 @@ use bevy_utils::tracing::{debug, error, info, trace}; use crate::irc_prelude as irc; -pub fn send + std::fmt::Debug + Clone>( - trigger: Trigger>, - sender: Query<&Sender>, - mut commands: Commands, -) { +pub fn send(trigger: Trigger, sender: Query<&Sender>, mut commands: Commands) { let msg = &trigger.event().0; let id = trigger.entity(); let sender = match sender.get(id) { @@ -26,20 +22,23 @@ pub fn send + std::fmt::Debug + Clone>( } } -pub fn on_ping(trigger: Trigger>, mut commands: Commands) { +pub fn on_ping(trigger: Trigger, 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>, mut commands: Commands) { +pub fn on_welcome(trigger: Trigger, 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::(); entity.insert(Registered); diff --git a/src/systems.rs b/src/systems.rs index ef412ce..0024dbe 100644 --- a/src/systems.rs +++ b/src/systems.rs @@ -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(); } } @@ -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); } } @@ -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); } } } @@ -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); } } @@ -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)); }