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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
use std::fmt::{self, Display, Formatter};
use std::ops::Neg;
use std::str::FromStr;

use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer};

use error::Error;
use query::Path;
use sealed::Sealed;
use value::Stringify;

/// A single sort instruction containing a direction and field path.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Sort {
    /// The direction to sort by.
    pub direction: Direction,

    /// The name of the field to sort by.
    pub field: Path,

    /// Private field for backwards compatibility.
    _ext: (),
}

impl Sort {
    /// Returns a new `Sort`.
    pub fn new(field: Path, direction: Direction) -> Self {
        Sort {
            direction,
            field,
            _ext: (),
        }
    }

    /// Returns a cloned inverse of `self`.
    ///
    /// # Example
    ///
    /// ```
    /// # extern crate json_api;
    /// #
    /// # use json_api::Error;
    /// #
    /// # fn example() -> Result<(), Error> {
    /// use json_api::query::{Direction, Sort};
    ///
    /// let chrono = Sort::new("created-at".parse()?, Direction::Asc);
    /// let latest = chrono.reverse();
    ///
    /// assert_eq!(chrono.field, latest.field);
    /// assert_eq!(chrono.direction, Direction::Asc);
    /// assert_eq!(latest.direction, Direction::Desc);
    /// #
    /// # Ok(())
    /// # }
    /// #
    /// # fn main() {
    /// #     example().unwrap();
    /// # }
    /// ```
    pub fn reverse(&self) -> Self {
        -self.clone()
    }
}

impl Display for Sort {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.write_str(&self.stringify())
    }
}

impl FromStr for Sort {
    type Err = Error;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        if value.starts_with('-') {
            let field = (&value[1..]).parse()?;
            Ok(Sort::new(field, Direction::Desc))
        } else {
            let field = value.parse()?;
            Ok(Sort::new(field, Direction::Asc))
        }
    }
}

impl Neg for Sort {
    type Output = Self;

    fn neg(self) -> Self::Output {
        Sort::new(self.field, -self.direction)
    }
}

impl<'de> Deserialize<'de> for Sort {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        use serde::de::{Error, Visitor};

        struct SortVisitor;

        impl<'de> Visitor<'de> for SortVisitor {
            type Value = Sort;

            fn expecting(&self, f: &mut Formatter) -> fmt::Result {
                write!(f, "a valid json api member name, optionally ")?;
                write!(f, r#"prefixed with "-" to denote descending order"#)
            }

            fn visit_str<E: Error>(self, value: &str) -> Result<Self::Value, E> {
                value.parse().map_err(Error::custom)
            }
        }

        deserializer.deserialize_str(SortVisitor)
    }
}

impl Serialize for Sort {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        self.stringify().serialize(serializer)
    }
}

impl Sealed for Sort {}

impl Stringify for Sort {
    fn to_bytes(&self) -> Vec<u8> {
        let mut field = self.field.to_bytes();
        let mut bytes = Vec::with_capacity(field.len() + 1);

        if self.direction.is_desc() {
            bytes.push(b'-');
        }

        bytes.append(&mut field);
        bytes
    }
}

/// The direction of a sort instruction.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Direction {
    /// Ascending
    Asc,

    /// Descending
    Desc,
}

impl Direction {
    /// Returns `true` if the direction is [`Asc`].
    ///
    /// # Example
    ///
    /// ```
    /// # extern crate json_api;
    /// #
    /// # fn main() {
    /// use json_api::query::Direction;
    ///
    /// let direction = Direction::Desc;
    /// assert_eq!(direction.is_asc(), false);
    ///
    /// let direction = Direction::Asc;
    /// assert_eq!(direction.is_asc(), true);
    /// # }
    /// ```
    ///
    /// [`Asc`]: #variant.Asc
    pub fn is_asc(&self) -> bool {
        *self == Direction::Asc
    }

    /// Returns `true` if the direction is [`Desc`].
    ///
    /// # Example
    ///
    /// ```
    /// # extern crate json_api;
    /// #
    /// # fn main() {
    /// use json_api::query::Direction;
    ///
    /// let direction = Direction::Asc;
    /// assert_eq!(direction.is_desc(), false);
    ///
    /// let direction = Direction::Desc;
    /// assert_eq!(direction.is_desc(), true);
    /// # }
    /// ```
    ///
    /// [`Desc`]: #variant.Desc
    pub fn is_desc(&self) -> bool {
        *self == Direction::Desc
    }

    /// Returns a cloned inverse of `self`.
    ///
    /// # Example
    ///
    /// ```
    /// # extern crate json_api;
    /// #
    /// # fn main() {
    /// use json_api::query::Direction;
    ///
    /// let asc = Direction::Asc;
    /// let desc = Direction::Desc;
    ///
    /// assert_eq!(asc.reverse(), desc);
    /// assert_eq!(desc.reverse(), asc);
    /// # }
    /// ```
    pub fn reverse(&self) -> Self {
        -*self
    }
}

impl Neg for Direction {
    type Output = Self;

    fn neg(self) -> Self::Output {
        match self {
            Direction::Asc => Direction::Desc,
            Direction::Desc => Direction::Asc,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{Direction, Sort};
    use value::Path;

    #[test]
    fn direction_is_asc() {
        assert_eq!(Direction::Asc.is_asc(), true);
        assert_eq!(Direction::Desc.is_asc(), false);
    }

    #[test]
    fn direction_is_desc() {
        assert_eq!(Direction::Desc.is_desc(), true);
        assert_eq!(Direction::Asc.is_desc(), false);
    }

    #[test]
    fn direction_reverse() {
        let asc = Direction::Asc;
        let desc = Direction::Desc;

        assert_eq!(asc.reverse(), desc);
        assert_eq!(desc.reverse(), asc);
    }

    #[test]
    fn sort_from_str() {
        let field = "created-at".parse::<Path>().unwrap();
        let mut sort = "created-at".parse::<Sort>().unwrap();

        assert_eq!(sort.field, field);
        assert_eq!(sort.direction, Direction::Asc);

        sort = "-created-at".parse().unwrap();

        assert_eq!(sort.field, field);
        assert_eq!(sort.direction, Direction::Desc);
    }

    #[test]
    fn sort_reverse() {
        let field = "created-at".parse().unwrap();
        let chrono = Sort::new(field, Direction::Asc);
        let latest = chrono.reverse();

        assert_eq!(chrono.field, latest.field);
        assert_eq!(chrono.direction, Direction::Asc);
        assert_eq!(latest.direction, Direction::Desc);
    }

    #[test]
    fn sort_to_string() {
        let sort = Sort::new("created-at".parse().unwrap(), Direction::Asc);

        assert_eq!(sort.to_string(), "created-at");
        assert_eq!(sort.reverse().to_string(), "-created-at");
    }
}