whatsapp_cloud_api/models/
component.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Clone, Debug, Serialize, Deserialize)]
4pub struct Component {
5 #[serde(rename = "type")]
6 pub component_type: ComponentType,
7 pub sub_type: Option<ComponentSubType>,
8 pub parameters: Option<Vec<Parameter>>,
9 pub index: Option<i32>,
10}
11
12impl Component {
13 pub fn new(component_type: ComponentType) -> Self {
14 Self {
15 component_type,
16 sub_type: None,
17 parameters: None,
18 index: None,
19 }
20 }
21
22 pub fn with_parameters(component_type: ComponentType, parameters: Vec<Parameter>) -> Self {
23 Self {
24 component_type,
25 sub_type: None,
26 parameters: Some(parameters),
27 index: None,
28 }
29 }
30
31 pub fn for_button(
32 component_type: ComponentType,
33 sub_type: ComponentSubType,
34 parameters: Vec<Parameter>,
35 index: i32,
36 ) -> Self {
37 Self {
38 component_type,
39 sub_type: Some(sub_type),
40 parameters: Some(parameters),
41 index: Some(index),
42 }
43 }
44}
45
46#[derive(Deserialize, Serialize, Debug, Clone)]
47#[serde(rename_all = "snake_case")]
48pub enum ComponentType {
49 Header,
50 Body,
51 Button,
52}
53
54#[derive(Deserialize, Serialize, Debug, Clone)]
55#[serde(rename_all = "snake_case")]
56pub enum ComponentSubType {
57 QuickReply,
58 Url,
59 Catalog,
60}
61
62#[derive(Clone, Debug, Serialize, Deserialize)]
63pub struct Parameter {
64 #[serde(rename = "type")]
65 pub parameter_type: ParameterType,
66 pub text: Option<String>,
67 pub currency: Option<Currency>,
68 pub date_time: Option<String>,
69 pub image: Option<Media>,
70 pub document: Option<Media>,
71 pub video: Option<Media>,
72}
73
74impl Parameter {
75 pub fn from_text(text: &str) -> Self {
76 Self {
77 parameter_type: ParameterType::Text,
78 text: Some(text.into()),
79 currency: None,
80 date_time: None,
81 image: None,
82 document: None,
83 video: None,
84 }
85 }
86}
87
88#[derive(Deserialize, Serialize, Debug, Clone)]
89#[serde(rename_all = "snake_case")]
90pub enum ParameterType {
91 Currency,
92 DateTime,
93 Document,
94 Image,
95 Text,
96 Video,
97}
98
99#[derive(Clone, Debug, Serialize, Deserialize)]
100pub struct Currency {
101 pub fallback_value: String,
102 pub code: String,
103 pub amount_1000: i32,
104}
105
106#[derive(Clone, Debug, Serialize, Deserialize)]
107pub struct DateTime {
108 pub fallback_value: String,
109}
110
111#[derive(Clone, Debug, Serialize, Deserialize)]
112pub struct Media {
113 pub id: Option<String>,
114 pub link: Option<String>,
115 pub caption: Option<String>,
116 pub filename: Option<String>,
117}