use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MsgType {
Text,
Post,
Image,
File,
#[cfg(feature = "card")]
Interactive,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TextContent {
pub text: String,
}
impl TextContent {
pub fn new(text: String) -> Self {
Self { text }
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PostContent {
pub post: String,
}
impl PostContent {
pub fn new(post: String) -> Self {
Self { post }
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImageContent {
pub image_key: String,
}
impl ImageContent {
pub fn new(image_key: String) -> Self {
Self { image_key }
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileContent {
pub file_key: String,
}
impl FileContent {
pub fn new(file_key: String) -> Self {
Self { file_key }
}
}
#[cfg(feature = "card")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InteractiveContent {
pub card: serde_json::Value,
}
#[cfg(feature = "card")]
impl InteractiveContent {
pub fn new(card: serde_json::Value) -> Self {
Self { card }
}
}
#[cfg(test)]
#[allow(unused_imports)]
mod tests {
use super::*;
#[test]
fn test_text_content_serialization() {
let content = TextContent::new("Hello, World!".to_string());
let json = serde_json::to_value(&content).unwrap();
assert_eq!(json["text"], "Hello, World!");
}
#[test]
fn test_post_content_serialization() {
let post_json = r#"{"title":"Test","content":"Content"}"#.to_string();
let content = PostContent::new(post_json.clone());
let json = serde_json::to_value(&content).unwrap();
assert_eq!(json["post"], post_json);
}
#[test]
fn test_image_content_serialization() {
let content = ImageContent::new("img_abc123".to_string());
let json = serde_json::to_value(&content).unwrap();
assert_eq!(json["image_key"], "img_abc123");
}
#[test]
fn test_file_content_serialization() {
let content = FileContent::new("file_xyz789".to_string());
let json = serde_json::to_value(&content).unwrap();
assert_eq!(json["file_key"], "file_xyz789");
}
#[test]
fn test_msg_type_serialization() {
let text_type = MsgType::Text;
let json = serde_json::to_value(text_type).unwrap();
assert_eq!(json, "text");
let image_type = MsgType::Image;
let json = serde_json::to_value(image_type).unwrap();
assert_eq!(json, "image");
}
#[cfg(feature = "card")]
#[test]
fn test_interactive_content_serialization() {
let card_json = serde_json::json!({
"type": "template",
"data": {
"template_id": "test_template"
}
});
let content = InteractiveContent::new(card_json.clone());
let json = serde_json::to_value(&content).unwrap();
assert_eq!(json["card"], card_json);
}
#[cfg(feature = "card")]
#[test]
fn test_msg_type_interactive_serialization() {
let interactive_type = MsgType::Interactive;
let json = serde_json::to_value(interactive_type).unwrap();
assert_eq!(json, "interactive");
}
}