2024-04-21 18:50:45 +00:00
|
|
|
use std::future::Future;
|
|
|
|
|
|
|
|
use tokio::io::{AsyncWrite, AsyncWriteExt};
|
|
|
|
|
|
|
|
use crate::prelude::Str;
|
|
|
|
use crate::Tag;
|
|
|
|
|
2024-04-23 18:13:38 +00:00
|
|
|
pub trait SendResponse {
|
2024-04-21 18:50:45 +00:00
|
|
|
fn write_response(self, writer: &mut (impl AsyncWrite + Unpin)) -> impl Future<Output = std::io::Result<()>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Server-to-client enum agnostic message
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
2024-04-23 18:13:38 +00:00
|
|
|
pub struct IrcResponseMessage<T> {
|
2024-04-21 18:50:45 +00:00
|
|
|
/// Optional tags section, prefixed with `@`
|
|
|
|
pub tags: Vec<Tag>,
|
|
|
|
/// Optional server name, prefixed with `:`.
|
|
|
|
pub sender: Option<Str>,
|
|
|
|
pub body: T,
|
|
|
|
}
|
|
|
|
|
2024-04-23 18:13:38 +00:00
|
|
|
impl<T> IrcResponseMessage<T> {
|
2024-04-21 18:50:45 +00:00
|
|
|
pub fn empty_tags(sender: Option<Str>, body: T) -> Self {
|
|
|
|
IrcResponseMessage {
|
|
|
|
tags: vec![],
|
|
|
|
sender,
|
|
|
|
body,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new(tags: Vec<Tag>, sender: Option<Str>, body: T) -> Self {
|
|
|
|
IrcResponseMessage { tags, sender, body }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 18:13:38 +00:00
|
|
|
impl<T: SendResponse> SendResponse for IrcResponseMessage<T> {
|
2024-04-21 18:50:45 +00:00
|
|
|
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(())
|
|
|
|
}
|
|
|
|
}
|