use std::future::Future; 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 { 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 { /// Optional tags section, prefixed with `@` pub tags: Vec, /// Optional server name, prefixed with `:`. pub sender: Option, pub body: T, } impl IrcResponseMessage { pub fn empty_tags(sender: Option, body: T) -> Self { IrcResponseMessage { tags: vec![], sender, body, } } pub fn new(tags: Vec, sender: Option, body: T) -> Self { IrcResponseMessage { tags, sender, body } } } impl SendResponseMessage> 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?; writer.write_all(sender.as_bytes()).await?; writer.write_all(b" ").await?; } self.body.write_response(writer).await?; writer.write_all(b"\r\n").await?; Ok(()) } }