1use crate::wire::{WireContact, WireDeliveredEmail, WireSendResponseData};
2
3#[derive(Clone, Debug, PartialEq, Eq)]
5pub struct SendResponse {
6 deliveries: Vec<Delivery>,
7 timestamp: String,
8}
9
10impl SendResponse {
11 pub fn deliveries(&self) -> &[Delivery] {
12 &self.deliveries
13 }
14
15 pub fn timestamp(&self) -> &str {
16 &self.timestamp
17 }
18}
19
20impl From<WireSendResponseData> for SendResponse {
21 fn from(value: WireSendResponseData) -> Self {
22 Self {
23 deliveries: value.emails.into_iter().map(Delivery::from).collect(),
24 timestamp: value.timestamp,
25 }
26 }
27}
28
29#[derive(Clone, Debug, PartialEq, Eq)]
30pub struct Delivery {
31 contact: Contact,
32 id: String,
33}
34
35impl Delivery {
36 pub fn contact(&self) -> &Contact {
37 &self.contact
38 }
39
40 pub fn id(&self) -> &str {
41 &self.id
42 }
43}
44
45impl From<WireDeliveredEmail> for Delivery {
46 fn from(value: WireDeliveredEmail) -> Self {
47 Self {
48 contact: Contact::from(value.contact),
49 id: value.email,
50 }
51 }
52}
53
54#[derive(Clone, Debug, PartialEq, Eq)]
55pub struct Contact {
56 id: String,
57 email: String,
58}
59
60impl Contact {
61 pub fn id(&self) -> &str {
62 &self.id
63 }
64
65 pub fn email(&self) -> &str {
66 &self.email
67 }
68}
69
70impl From<WireContact> for Contact {
71 fn from(value: WireContact) -> Self {
72 Self {
73 id: value.id,
74 email: value.email,
75 }
76 }
77}
78
79#[cfg(test)]
80mod tests {
81 use super::*;
82 use crate::wire::WireSendResponse;
83 use serde_json::json;
84
85 #[test]
86 fn deserializes_success_response_into_domain_types() {
87 let response = serde_json::from_value::<WireSendResponse>(json!({
88 "success": true,
89 "data": {
90 "emails": [
91 {
92 "contact": {
93 "id": "cnt_abc123",
94 "email": "user@example.com"
95 },
96 "email": "ac32f08e-c6b9-45d3-9824-a73dff1e3bbf"
97 }
98 ],
99 "timestamp": "2025-01-15T10:30:00.000Z"
100 }
101 }))
102 .unwrap();
103
104 let response = SendResponse::from(response.data);
105
106 assert_eq!(response.deliveries().len(), 1);
107 assert_eq!(
108 response.deliveries()[0].contact().email(),
109 "user@example.com"
110 );
111 assert_eq!(
112 response.deliveries()[0].id(),
113 "ac32f08e-c6b9-45d3-9824-a73dff1e3bbf"
114 );
115 }
116}