1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use super::*;

/// List of Recipients
///
/// As returned by a query to the MessageBird API.
///
// requires manual Serialize/Deserialize implementation
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct Recipients {
    #[serde(rename = "totalCount")]
    total_count: u32,
    #[serde(rename = "totalSentCount")]
    total_sent: u32,
    #[serde(rename = "totalDeliveredCount")]
    total_delivered: u32,
    #[serde(rename = "totalDeliveryFailedCount")]
    total_delivery_failed: u32,
    #[serde(rename = "items")]
    items: Vec<Recipient>,
}

impl Default for Recipients {
    fn default() -> Self {
        Self {
            total_count: 0,
            total_sent: 0,
            total_delivered: 0,
            total_delivery_failed: 0,
            items: Vec::new(),
        }
    }
}

impl Recipients {
    pub fn count(&self) -> (u32, u32, u32) {
        (
            self.total_sent,
            self.total_delivered,
            self.total_delivery_failed,
        )
    }
    pub fn iter(&mut self) -> Iter<Recipient> {
        self.items.iter()
    }
    pub fn add(&mut self, recipient: Recipient) {
        self.items.push(recipient)
    }
}

#[cfg(test)]
mod test {
    use super::*;
    static RAW: &str = r#"{
    "totalCount":1,
    "totalSentCount":1,
    "totalDeliveredCount":0,
    "totalDeliveryFailedCount":0,
    "items":[
      {
        "recipient": 31612345678,
        "status":"sent",
        "statusDatetime":"2016-05-03T14:26:57+00:00"
      }
    ]
}"#;

    deser_roundtrip!(recipients_deser, Recipients, RAW);
    serde_roundtrip!(recipients_serde, Recipients, Recipients::default());
}