use crate::block::Block;
use af_core::prelude::*;
use serde::ser::SerializeStruct;
#[derive(Debug)]
pub struct Attachment<'a> {
pub blocks: Vec<Block<'a>>,
pub color: Cow<'a, str>,
}
impl<'a> Attachment<'a> {
pub const fn new() -> Self {
Self::info()
}
pub const fn debug() -> Self {
Self { blocks: Vec::new(), color: Cow::Borrowed("debug") }
}
pub const fn info() -> Self {
Self { blocks: Vec::new(), color: Cow::Borrowed("info") }
}
pub const fn warn() -> Self {
Self { blocks: Vec::new(), color: Cow::Borrowed("warn") }
}
pub const fn error() -> Self {
Self { blocks: Vec::new(), color: Cow::Borrowed("error") }
}
pub const fn success() -> Self {
Self { blocks: Vec::new(), color: Cow::Borrowed("success") }
}
pub fn add_block(&mut self, block: impl Into<Block<'a>>) -> &mut Self {
self.blocks.push(block.into());
self
}
pub fn set_color(&mut self, color: impl Into<Cow<'a, str>>) -> &mut Self {
self.color = color.into();
self
}
pub fn with_block(mut self, block: impl Into<Block<'a>>) -> Self {
self.add_block(block);
self
}
pub fn with_color(mut self, color: impl Into<Cow<'a, str>>) -> Self {
self.set_color(color);
self
}
}
impl<'a> Default for Attachment<'a> {
fn default() -> Self {
Self::new()
}
}
impl<'a> Serialize for Attachment<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let color = match self.color.as_ref() {
"good" | "success" | "ok" => "#73d216",
"debug" => "#75507b",
"info" | "default" | "" => "#3465a4",
"warn" | "warning" => "#edd400",
"error" | "err" => "#cc0000",
other => other,
};
let mut s = serializer.serialize_struct("Attachment", 2)?;
s.serialize_field("color", &color)?;
s.serialize_field("blocks", &self.blocks)?;
s.end()
}
}