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
use crate::resources::common::object::Object;

#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct List<T> {
    pub data: Vec<T>,
    pub object: Object,
    pub has_more: bool,
    pub total_count: Option<i64>,
    pub url: String,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(untagged, rename_all="lowercase")]
pub enum Expandable<T> {
    Object(Box<T>),
    Id(String)
}

macro_rules! match_object {
    ($self:ident) => (
        match $self {
            Expandable::Object(obj) => Some(obj),
            Expandable::Id(..) => None,
        }
    )
}

macro_rules! match_id {
    ($self:ident) => (
        match $self {
            Expandable::Object(..) => None,
            Expandable::Id(id) => Some(id),
        }
    )
}

impl<T> Expandable<T> {
    pub fn as_object(&self) -> Option<&T> {
        match_object!(self)
    }

    pub fn as_id(&self) -> Option<&str> {
        match_id!(self)
    }

    pub fn into_object(self) -> Option<T> {
        match_object!(self).map(|obj| *obj)
    }

    pub fn into_id(self) -> Option<String> {
        match_id!(self)
    }
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct Deleted {
    pub object: Object,
    pub deleted: bool,
    pub id: String,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct Period {
    start: i64,
    end: i64,
}

#[derive(Serialize, Debug, PartialEq)]
pub struct RangeBounds {
    pub gt: Option<i64>,
    pub gte: Option<i64>,
    pub lt: Option<i64>,
    pub lte: Option<i64>,
}

impl Default for RangeBounds {
    fn default() -> Self {
        RangeBounds {
            gt: None,
            gte: None,
            lt: None,
            lte: None,
        }
    }
}

#[derive(Serialize, Debug, PartialEq)]
#[serde(untagged)]
pub enum RangeQuery {
    Exact(i64),
    Bounds(RangeBounds),
}

impl RangeQuery {
    /// Filter results to exactly match a given value
    pub fn eq(value: i64) -> RangeQuery {
        RangeQuery::Exact(value)
    }

    /// Filter results to be after a given value
    pub fn gt(value: i64) -> RangeQuery {
        let mut bounds = RangeBounds::default();
        bounds.gt = Some(value);
        RangeQuery::Bounds(bounds)
    }

    /// Filter results to be after or equal to a given value
    pub fn gte(value: i64) -> RangeQuery {
        let mut bounds = RangeBounds::default();
        bounds.gte = Some(value);
        RangeQuery::Bounds(bounds)
    }

    /// Filter results to be before to a given value
    pub fn lt(value: i64) -> RangeQuery {
        let mut bounds = RangeBounds::default();
        bounds.gt = Some(value);
        RangeQuery::Bounds(bounds)
    }

    /// Filter results to be before or equal to a given value
    pub fn lte(value: i64) -> RangeQuery {
        let mut bounds = RangeBounds::default();
        bounds.gte = Some(value);
        RangeQuery::Bounds(bounds)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    struct Foo {
        bar: Expandable<Bar>,
    }

    struct Bar {
        foo: Expandable<Foo>,
    }

    #[test]
    fn expand_chaining() {
        let foo = Foo { bar: Expandable::Id("bar".into()) };
        let bar = Bar { foo: Expandable::Object(Box::new(foo)) };
        let foo = Foo { bar: Expandable::Object(Box::new(bar)) };

        let res = foo
            .bar.into_object().unwrap()
            .foo.into_object().unwrap()
            .bar.into_id().unwrap();
        assert_eq!(res, format!("bar"));
    }
}