use serde::{ser::SerializeStruct, Serialize};
use typed_builder::TypedBuilder;
use crate::push_notify::NotifySerialize;
#[derive(Debug, Clone)]
pub enum NotifyStyle {
Default,
LongContent(String),
BigVision(String),
Banner(Vec<String>),
Custom(CustomStyle),
}
impl NotifyStyle {
pub fn new_default() -> Self {
Self::Default
}
pub fn new_long_content(content: impl Into<String>) -> Self {
Self::LongContent(content.into())
}
pub fn new_big_vision(image_url: impl Into<String>) -> Self {
Self::BigVision(image_url.into())
}
pub fn new_banner<I, T>(contents: I) -> Self
where
T: Into<String>,
I: IntoIterator<Item = T>,
{
Self::Banner(contents.into_iter().map(Into::into).collect())
}
pub fn new_custom(custom_style: CustomStyle) -> Self {
Self::Custom(custom_style)
}
}
impl NotifyStyle {
fn get_code(&self) -> i32 {
match self {
NotifyStyle::Default => 0,
NotifyStyle::LongContent(_) => 1,
NotifyStyle::BigVision(_) => 2,
NotifyStyle::Banner(_) => 3,
NotifyStyle::Custom(_) => 4,
}
}
}
#[derive(Debug, Clone)]
pub enum StyleId {
One,
Tow,
Three,
}
impl Serialize for StyleId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_i32(self.to_code())
}
}
impl StyleId {
fn to_code(&self) -> i32 {
match self {
StyleId::One => 1,
StyleId::Tow => 2,
StyleId::Three => 3,
}
}
}
#[derive(Debug, Serialize, TypedBuilder, Clone)]
#[builder(field_defaults(default, setter(strip_option)))]
pub struct CustomStyle {
#[serde(rename = "styleNo", skip_serializing_if = "Option::is_none")]
style: Option<StyleId>,
#[serde(rename = "backgroundUrl", skip_serializing_if = "Option::is_none")]
background_url: Option<String>,
#[serde(rename = "smallIcons", skip_serializing_if = "Option::is_none")]
small_icons: Option<String>,
#[serde(rename = "buttonCopy", skip_serializing_if = "Option::is_none")]
button_copy: Option<String>,
#[serde(rename = "buttonJumpUrl", skip_serializing_if = "Option::is_none")]
button_jump_url: Option<String>,
}
impl NotifySerialize for NotifyStyle {
fn serialize_field(&self) -> usize {
match self {
NotifyStyle::Default => 1,
NotifyStyle::LongContent(_)
| NotifyStyle::BigVision(_)
| NotifyStyle::Banner(_)
| NotifyStyle::Custom(_) => 2,
}
}
fn serialize<S: serde::Serializer>(
&self,
serialize_struct: &mut <S as serde::Serializer>::SerializeStruct,
) -> Result<(), <S as serde::Serializer>::Error> {
serialize_struct.serialize_field("style", &self.get_code())?;
match self {
NotifyStyle::Default => Ok(()),
NotifyStyle::LongContent(s) | NotifyStyle::BigVision(s) => {
serialize_struct.serialize_field("content", &[s])
}
NotifyStyle::Banner(info_vec) => serialize_struct.serialize_field("content", info_vec),
NotifyStyle::Custom(inner) => serialize_struct.serialize_field("customStyle", inner),
}
}
}