From 37fbe989b0888f4d7600ff0e57bf36226f66b1d3 Mon Sep 17 00:00:00 2001 From: homycdev Date: Tue, 23 Apr 2024 21:13:38 +0300 Subject: [PATCH] - use Str since it is type alias to Arc - refactored code --- crates/projection-irc/src/commands/mod.rs | 8 +++--- .../src/commands/whois/error.rs | 8 +++--- .../projection-irc/src/commands/whois/mod.rs | 26 ++++++++++--------- .../src/commands/whois/response.rs | 6 ++--- crates/projection-irc/src/lib.rs | 11 ++++---- crates/proto-irc/src/response.rs | 12 +++------ 6 files changed, 34 insertions(+), 37 deletions(-) diff --git a/crates/projection-irc/src/commands/mod.rs b/crates/projection-irc/src/commands/mod.rs index bf5124f..98decf1 100644 --- a/crates/projection-irc/src/commands/mod.rs +++ b/crates/projection-irc/src/commands/mod.rs @@ -1,6 +1,6 @@ use lavina_core::prelude::Str; use lavina_core::repo::Storage; -use proto_irc::response::SendResponseBody; +use proto_irc::response::SendResponse; use std::future::Future; use tokio::io::AsyncWrite; @@ -9,9 +9,9 @@ pub mod whois; pub trait Handler { fn handle( &self, - server_name: &Str, - client: &Str, + server_name: Str, + client: Str, writer: &mut (impl AsyncWrite + Unpin), - storage: &mut Storage, + storage: Storage, ) -> impl Future>; } diff --git a/crates/projection-irc/src/commands/whois/error.rs b/crates/projection-irc/src/commands/whois/error.rs index ab2af75..e2e0d6b 100644 --- a/crates/projection-irc/src/commands/whois/error.rs +++ b/crates/projection-irc/src/commands/whois/error.rs @@ -1,7 +1,7 @@ use tokio::io::{AsyncWrite, AsyncWriteExt}; use lavina_core::prelude::Str; -use proto_irc::response::SendResponseBody; +use proto_irc::response::SendResponse; /// ERR_NOSUCHNICK (401) pub struct ErrNoSuchNick401 { @@ -33,7 +33,7 @@ impl ErrNoNicknameGiven431 { } } -impl SendResponseBody for ErrNoSuchNick401 { +impl SendResponse for ErrNoSuchNick401 { async fn write_response(self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> { writer.write_all(b"401 ").await?; writer.write_all(self.client.as_bytes()).await?; @@ -45,7 +45,7 @@ impl SendResponseBody for ErrNoSuchNick401 { } } -impl SendResponseBody for ErrNoNicknameGiven431 { +impl SendResponse for ErrNoNicknameGiven431 { async fn write_response(self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> { writer.write_all(b"431").await?; writer.write_all(self.client.as_bytes()).await?; @@ -55,7 +55,7 @@ impl SendResponseBody for ErrNoNicknameGiven431 { } } -impl SendResponseBody for ErrNoSuchServer402 { +impl SendResponse for ErrNoSuchServer402 { async fn write_response(self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> { writer.write_all(b"402 ").await?; writer.write_all(self.client.as_bytes()).await?; diff --git a/crates/projection-irc/src/commands/whois/mod.rs b/crates/projection-irc/src/commands/whois/mod.rs index 3ef741e..f2f3ca2 100644 --- a/crates/projection-irc/src/commands/whois/mod.rs +++ b/crates/projection-irc/src/commands/whois/mod.rs @@ -4,7 +4,7 @@ use tracing::instrument::WithSubscriber; use lavina_core::prelude::Str; use lavina_core::repo::Storage; use proto_irc::client::command_args::Whois; -use proto_irc::response::{IrcResponseMessage, SendResponseBody, SendResponseMessage}; +use proto_irc::response::{IrcResponseMessage, SendResponse}; use crate::commands::whois::error::{ErrNoNicknameGiven431, ErrNoSuchNick401}; use crate::commands::whois::response::{RplWhoIsUser311, RPL_ENDOFWHOIS_318}; @@ -16,15 +16,17 @@ pub mod response; impl Handler for Whois { async fn handle( &self, - server_name: &Str, - client: &Str, + server_name: Str, + client: Str, writer: &mut (impl AsyncWrite + Unpin), - storage: &mut Storage, + mut storage: Storage, ) -> anyhow::Result<()> { match self { - Whois::Nick(nick) => handle_nick_target(nick, None, server_name, client, writer, storage).await?, + Whois::Nick(nick) => { + handle_nick_target(nick.clone(), None, server_name, client, writer, storage.clone()).await? + } Whois::TargetNick(target, nick) => { - handle_nick_target(nick, Some(target), server_name, client, writer, storage).await? + handle_nick_target(nick.clone(), Some(target.clone()), server_name, client, writer, storage).await? } Whois::EmptyArgs => { IrcResponseMessage::empty_tags( @@ -40,15 +42,15 @@ impl Handler for Whois { } async fn handle_nick_target( - nick: &Str, + nick: Str, // todo: implement logic with target - _target: Option<&Str>, - server_name: &Str, - client: &Str, + _target: Option, + server_name: Str, + client: Str, writer: &mut (impl AsyncWrite + Unpin), - storage: &mut Storage, + mut storage: Storage, ) -> anyhow::Result<()> { - if let Some(user) = storage.retrieve_user_by_name(nick).await? { + if let Some(user) = storage.retrieve_user_by_name(nick.clone().as_ref()).await? { IrcResponseMessage::empty_tags( Some(server_name.clone()), RplWhoIsUser311::new( diff --git a/crates/projection-irc/src/commands/whois/response.rs b/crates/projection-irc/src/commands/whois/response.rs index c0d3ad2..3196e06 100644 --- a/crates/projection-irc/src/commands/whois/response.rs +++ b/crates/projection-irc/src/commands/whois/response.rs @@ -1,7 +1,7 @@ use tokio::io::{AsyncWrite, AsyncWriteExt}; use lavina_core::prelude::Str; -use proto_irc::response::SendResponseBody; +use proto_irc::response::SendResponse; struct RplWhoisCertfp276; struct RplWhoisRegNick307; @@ -47,7 +47,7 @@ impl RPL_ENDOFWHOIS_318 { } } -impl SendResponseBody for RplWhoIsUser311 { +impl SendResponse for RplWhoIsUser311 { async fn write_response(self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> { writer.write_all(b"311 ").await?; writer.write_all(self.client.as_bytes()).await?; @@ -70,7 +70,7 @@ impl SendResponseBody for RplWhoIsUser311 { } } -impl SendResponseBody for RPL_ENDOFWHOIS_318 { +impl SendResponse for RPL_ENDOFWHOIS_318 { async fn write_response(self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> { writer.write_all(b"318 ").await?; writer.write_all(self.client.as_bytes()).await?; diff --git a/crates/projection-irc/src/lib.rs b/crates/projection-irc/src/lib.rs index 6c58fd7..8ca15b0 100644 --- a/crates/projection-irc/src/lib.rs +++ b/crates/projection-irc/src/lib.rs @@ -469,7 +469,7 @@ async fn handle_registered_socket<'a>( len }; let incoming = std::str::from_utf8(&buffer[0..len-2])?; - if let HandleResult::Leave = handle_incoming_message(incoming, &config, &user, &rooms, &mut connection, writer, storage).await? { + if let HandleResult::Leave = handle_incoming_message(incoming, &config, &user, &rooms, &mut connection, writer, storage.clone()).await? { break; } buffer.clear(); @@ -618,7 +618,7 @@ async fn handle_incoming_message( rooms: &RoomRegistry, user_handle: &mut PlayerConnection, writer: &mut (impl AsyncWrite + Unpin), - storage: &mut Storage, + mut storage: Storage, ) -> Result { log::debug!("Incoming raw IRC message: '{buffer}'"); let parsed = client_message(buffer); @@ -730,14 +730,13 @@ async fn handle_incoming_message( }, ClientMessage::Whois { arg } => { arg.handle( - &config.server_name, - &user.nickname, + config.server_name.clone(), + user.nickname.clone(), writer, - storage, + storage.clone(), ) .await? } - ClientMessage::Mode { target } => { match target { Recipient::Nick(nickname) => { diff --git a/crates/proto-irc/src/response.rs b/crates/proto-irc/src/response.rs index 1a0051c..be4cd81 100644 --- a/crates/proto-irc/src/response.rs +++ b/crates/proto-irc/src/response.rs @@ -5,17 +5,13 @@ use tokio::io::{AsyncWrite, AsyncWriteExt}; use crate::prelude::Str; use crate::Tag; -pub trait SendResponseBody { - fn write_response(self, writer: &mut (impl AsyncWrite + Unpin)) -> impl Future>; -} - -pub trait SendResponseMessage { +pub trait SendResponse { fn write_response(self, writer: &mut (impl AsyncWrite + Unpin)) -> impl Future>; } /// Server-to-client enum agnostic message #[derive(Clone, Debug, PartialEq, Eq)] -pub struct IrcResponseMessage { +pub struct IrcResponseMessage { /// Optional tags section, prefixed with `@` pub tags: Vec, /// Optional server name, prefixed with `:`. @@ -23,7 +19,7 @@ pub struct IrcResponseMessage { pub body: T, } -impl IrcResponseMessage { +impl IrcResponseMessage { pub fn empty_tags(sender: Option, body: T) -> Self { IrcResponseMessage { tags: vec![], @@ -37,7 +33,7 @@ impl IrcResponseMessage { } } -impl SendResponseMessage> for IrcResponseMessage { +impl SendResponse for IrcResponseMessage { async fn write_response(self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> { if let Some(sender) = &self.sender { writer.write_all(b":").await?;