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
use crate::traits::*;

/// A Collection stores a collection of items returned from the API such
/// as a collection of DriveItem's or User's.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Collection<T> {
    #[serde(rename = "@odata.nextLink")]
    #[serde(skip_serializing_if = "Option::is_none")]
    odata_next_link: Option<String>,
    #[serde(rename = "@odata.deltaLink")]
    #[serde(skip_serializing_if = "Option::is_none")]
    odata_delta_link: Option<String>,
    #[serde(rename = "@odata.context")]
    #[serde(skip_serializing_if = "Option::is_none")]
    odata_context: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    value: Option<Vec<T>>,
}

impl<T> Collection<T> {
    pub fn new(
        next_link: Option<String>,
        delta_link: Option<String>,
        context: Option<String>,
        value: Option<Vec<T>>,
    ) -> Collection<T> {
        Collection {
            odata_next_link: next_link,
            odata_delta_link: delta_link,
            odata_context: context,
            value,
        }
    }

    pub fn odata_next_link(&self) -> Option<&String> {
        self.odata_next_link.as_ref()
    }

    pub fn odata_delta_link(&self) -> Option<&String> {
        self.odata_delta_link.as_ref()
    }

    pub fn odata_context(&self) -> Option<&String> {
        self.odata_context.as_ref()
    }

    pub fn index(&self, idx: usize) -> Option<&T> {
        if let Some(vec) = self.value.as_ref() {
            vec.get(idx)
        } else {
            None
        }
    }

    pub fn add(&mut self, value: T) {
        if let Some(ref mut vec) = self.value {
            vec.push(value);
        } else {
            let mut vec: Vec<T> = Vec::new();
            vec.push(value);
            self.value = Some(vec);
        }
    }

    pub fn len(&self) -> usize {
        if let Some(v) = self.value.as_ref() {
            v.len()
        } else {
            0
        }
    }

    pub fn is_empty(&self) -> bool {
        if let Some(v) = self.value.as_ref() {
            v.is_empty()
        } else {
            true
        }
    }

    pub fn value(&self) -> Option<&Vec<T>> {
        self.value.as_ref()
    }

    pub fn value_mut(&mut self) -> &mut Option<Vec<T>> {
        &mut self.value
    }

    pub fn clone_inner(&mut self) -> Vec<T>
    where
        T: std::clone::Clone,
    {
        self.value.clone().unwrap_or_default().to_vec()
    }

    pub fn into_inner(self) -> Vec<T> {
        self.value.unwrap_or_default()
    }
}

impl<T> Eq for Collection<T> where T: std::cmp::PartialEq {}

impl<T> Into<Vec<T>> for Collection<T> {
    fn into(self) -> Vec<T> {
        self.value.unwrap_or_default()
    }
}

impl<T> IntoIterator for Collection<T>
where
    T: Clone,
{
    type Item = T;
    type IntoIter = ::std::vec::IntoIter<Self::Item>;

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

impl<T> NextLink for Collection<T> {
    fn next_link(&self) -> Option<String> {
        self.odata_next_link.clone()
    }
}

impl<T> DeltaLink for Collection<T> {
    fn delta_link(&self) -> Option<String> {
        self.odata_delta_link.clone()
    }
}

impl<T> MetadataLink for Collection<T> {
    fn metadata_link(&self) -> Option<String> {
        self.odata_context.clone()
    }
}