use crate::CoreError;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct GenericMessage {
pub content: Option<String>,
pub username: Option<String>,
pub avatar_url: Option<String>,
pub tts: bool,
pub embeds: Vec<GenericEmbed>,
pub metadata: FxHashMap<String, serde_json::Value>,
pub attachments: Vec<Attachment>,
}
impl GenericMessage {
pub fn new(content: impl Into<String>) -> Self {
Self { content: Some(content.into()), ..Default::default() }
}
pub fn with_embed(mut self, embed: GenericEmbed) -> Self {
self.embeds.push(embed);
self
}
pub fn with_username(mut self, username: impl Into<String>) -> Self {
self.username = Some(username.into());
self
}
pub fn with_avatar(mut self, avatar_url: impl Into<String>) -> Self {
self.avatar_url = Some(avatar_url.into());
self
}
pub fn with_metadata(
mut self,
key: impl Into<String>,
value: impl Serialize,
) -> Result<Self, CoreError> {
self.metadata
.insert(key.into(), serde_json::to_value(value)?);
Ok(self)
}
pub fn with_tts(mut self, tts: bool) -> Self {
self.tts = tts;
self
}
pub fn with_attachment(mut self, attachment: Attachment) -> Self {
self.attachments.push(attachment);
self
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct GenericEmbed {
pub title: Option<String>,
pub description: Option<String>,
pub url: Option<String>,
pub color: Option<u32>,
pub timestamp: Option<String>,
pub footer: Option<GenericEmbedFooter>,
pub image: Option<GenericEmbedImage>,
pub thumbnail: Option<GenericEmbedThumbnail>,
pub author: Option<GenericEmbedAuthor>,
pub fields: Vec<GenericEmbedField>,
}
impl GenericEmbed {
pub fn new(title: impl Into<String>, description: impl Into<String>) -> Self {
Self {
title: Some(title.into()),
description: Some(description.into()),
..Default::default()
}
}
pub fn with_color(mut self, color: u32) -> Self {
self.color = Some(color);
self
}
pub fn with_field(
mut self,
name: impl Into<String>,
value: impl Into<String>,
inline: bool,
) -> Self {
self.fields
.push(GenericEmbedField { name: name.into(), value: value.into(), inline });
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenericEmbedField {
pub name: String,
pub value: String,
pub inline: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenericEmbedFooter {
pub text: String,
pub icon_url: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenericEmbedImage {
pub url: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenericEmbedThumbnail {
pub url: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenericEmbedAuthor {
pub name: String,
pub url: Option<String>,
pub icon_url: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Attachment {
pub filename: String,
pub content: Vec<u8>,
pub content_type: String,
}
pub trait MessageConverter<T> {
fn to_service(&self, generic: &GenericMessage) -> Result<T, CoreError>;
fn to_generic(&self, service_msg: &T) -> Result<GenericMessage, CoreError>;
}