Skip to main content

usesend_api/types/
mod.rs

1use serde::{Deserialize, Deserializer, Serialize, Serializer};
2
3pub mod campaign;
4pub mod contact;
5pub mod contact_book;
6pub mod domain;
7pub mod email;
8
9macro_rules! define_id_type {
10    ($name:ident, String) => {
11        #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
12        #[serde(transparent)]
13        pub struct $name(pub String);
14
15        impl std::fmt::Display for $name {
16            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17                self.0.fmt(f)
18            }
19        }
20
21        impl AsRef<str> for $name {
22            fn as_ref(&self) -> &str {
23                &self.0
24            }
25        }
26
27        impl std::ops::Deref for $name {
28            type Target = str;
29            fn deref(&self) -> &str {
30                &self.0
31            }
32        }
33
34        impl From<String> for $name {
35            fn from(s: String) -> Self {
36                Self(s)
37            }
38        }
39
40        impl From<&str> for $name {
41            fn from(s: &str) -> Self {
42                Self(s.to_string())
43            }
44        }
45    };
46    ($name:ident, i64) => {
47        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
48        #[serde(transparent)]
49        pub struct $name(pub i64);
50
51        impl std::fmt::Display for $name {
52            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53                self.0.fmt(f)
54            }
55        }
56
57        impl From<i64> for $name {
58            fn from(id: i64) -> Self {
59                Self(id)
60            }
61        }
62    };
63}
64
65define_id_type!(DomainId, i64);
66define_id_type!(EmailId, String);
67define_id_type!(ContactBookId, String);
68define_id_type!(ContactId, String);
69define_id_type!(CampaignId, String);
70
71/// A value that can be either a single string or a list of strings.
72/// Used for `to`, `cc`, `bcc`, `replyTo` fields.
73#[derive(Debug, Clone, PartialEq)]
74pub enum StringOrVec {
75    Single(String),
76    Multiple(Vec<String>),
77}
78
79impl From<&str> for StringOrVec {
80    fn from(s: &str) -> Self {
81        StringOrVec::Single(s.to_string())
82    }
83}
84
85impl From<String> for StringOrVec {
86    fn from(s: String) -> Self {
87        StringOrVec::Single(s)
88    }
89}
90
91impl From<Vec<String>> for StringOrVec {
92    fn from(v: Vec<String>) -> Self {
93        StringOrVec::Multiple(v)
94    }
95}
96
97impl Serialize for StringOrVec {
98    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
99        match self {
100            StringOrVec::Single(s) => serializer.serialize_str(s),
101            StringOrVec::Multiple(v) => v.serialize(serializer),
102        }
103    }
104}
105
106impl<'de> Deserialize<'de> for StringOrVec {
107    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
108        #[derive(Deserialize)]
109        #[serde(untagged)]
110        enum Helper {
111            Single(String),
112            Multiple(Vec<String>),
113        }
114        match Helper::deserialize(deserializer)? {
115            Helper::Single(s) => Ok(StringOrVec::Single(s)),
116            Helper::Multiple(v) => Ok(StringOrVec::Multiple(v)),
117        }
118    }
119}
120
121/// Generic error response from the API.
122#[derive(Debug, Clone, Serialize, Deserialize)]
123pub struct ErrorResponse {
124    pub error: String,
125}
126
127/// Generic success response with ID and message.
128#[derive(Debug, Clone, Serialize, Deserialize)]
129pub struct DeleteResponse {
130    pub id: serde_json::Value,
131    pub success: bool,
132    pub message: String,
133}
134
135/// Simple success response.
136#[derive(Debug, Clone, Serialize, Deserialize)]
137pub struct SuccessResponse {
138    pub success: bool,
139}