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
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//! Per refugee camp data

use std::ops::Deref;

use serde::{Deserialize, Serialize};

use crate::build_request;

// HTTP GET /regugee-camps
/// Recorded cases in Refugee-camps.
#[derive(Debug, Deserialize, Serialize)]
pub struct RefCamps {
    #[serde(rename = "refugee-camps")]
    pub refugee_camps: Vec<RefCamp>,
}

impl Deref for RefCamps {
    type Target = Vec<RefCamp>;
    fn deref(&self) -> &Self::Target {
        &self.refugee_camps
    }
}

impl IntoIterator for RefCamps {
    type Item = RefCamp;
    type IntoIter = std::vec::IntoIter<RefCamp>;

    fn into_iter(self) -> Self::IntoIter {
        self.refugee_camps.into_iter()
    }
}

/// Slice Iterator for RefCamp.
pub type IterRefCamp<'iter> = core::slice::Iter<'iter, RefCamp>;
/// Mutable slice Iterator for RefCamp.
pub type IterMutRefCamp<'iter_mut> = core::slice::IterMut<'iter_mut, RefCamp>;

impl RefCamps {
    pub fn iter(&self) -> IterRefCamp {
        self.refugee_camps.iter()
    }

    pub fn iter_mut(&mut self) -> IterMutRefCamp {
        self.refugee_camps.iter_mut()
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub struct RefCamp {
    pub area_type_en: String,
    pub area_type_gr: String,
    pub capacity: Option<u32>,
    pub current_hosts: Option<u32>,
    pub description: String,
    pub last_update: Option<String>,
    pub latitude: f64,
    pub longtitude: f64,
    pub name_en: String,
    pub name_gr: String,
    pub recorded_events: Vec<RecordedEvent>,
    pub region_en: String,
    pub region_gr: String,
    pub total_confirmed_cases: u32,
    pub total_confirmed_samples: Option<u32>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct RecordedEvent {
    pub case_detection_week: Option<String>,
    pub confirmed_cases: Option<u32>,
    pub quarantine_duration_days: Option<u32>,
    pub samples: Option<u32>,
}

pub fn get_refugee_camp_data() -> RefCamps {
    let json_resp = build_request("refugee-camps");
    let ref_camps = serde_json::from_str(&json_resp).unwrap();
    ref_camps
}

#[cfg(test)]
mod tests {

    use super::*;

    const STR_JSON: &str = r#"{
              "refugee-camps":
              [
                               {
                                "area_type_en": "string",
                                "area_type_gr": "string",
                                "capacity": 0,
                                "current_hosts": 0,
                                "description": "string",
                                "last_update": "2021-04-28",
                                "latitude": 0,
                                "longtitude": 0,
                                "name_en": "string",
                                "name_gr": "string",
                                "recorded_events": [
                                    {
                                    "case_detection_week": "string",
                                    "confirmed_cases": 0,
                                    "quarantine_duration_days": 0,
                                    "samples": 0
                                    }
                                ],
                                "region_en": "string",
                                "region_gr": "string",
                                "total_confirmed_cases": 0,
                                "total_confirmed_samples": 0
                                }
                 ]
                            }"#;

    #[test]
    fn test_deserilize_refugee() {
        let ref_camps: Result<RefCamps, _> = serde_json::from_str(STR_JSON);
        assert!(ref_camps.is_ok());
    }

    #[test]
    fn test_get_refugee_camp_data() {
        let ref_camp = get_refugee_camp_data();
    }

    #[test]
    fn test_into_iterator() {
        let ref_camps: RefCamps = serde_json::from_str(STR_JSON).unwrap();
        ref_camps
            .into_iter()
            .for_each(|ref_camp| println!("{:?}", ref_camp));
    }

    #[test]
    fn test_iter() {
        let ref_camps: RefCamps = serde_json::from_str(STR_JSON).unwrap();
        ref_camps.iter().for_each(|r| println!("{:?}", r));
    }

    #[test]
    fn test_iter_mut() {
        let mut ref_camps: RefCamps = serde_json::from_str(STR_JSON).unwrap();
        let mut iter_mut = ref_camps.iter_mut();
        let mut first = iter_mut.next();
        let mut second = iter_mut.next();
        println!("{:?} {:?}", first, second);
    }

    #[test]
    fn test_derer() {
        let ref_camps: RefCamps = serde_json::from_str(STR_JSON).unwrap();
        let vec_ref = ref_camps.deref();
        println!("{:?}", (*ref_camps).iter());
    }
}