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
use serde::{Deserialize, Serialize};
use std::{
    convert::Infallible,
    fmt::{Display, Formatter, Result},
    str::FromStr,
};

/// Fields by which to sort results.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct Sortby {
    /// The field to sort by.
    pub field: String,

    /// The direction to sort by.
    pub direction: Direction,
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub enum Direction {
    #[serde(rename = "asc")]
    Ascending,
    #[serde(rename = "desc")]
    Descending,
}

impl Sortby {
    /// Creates a new ascending sortby for the field.
    ///
    /// # Examples
    ///
    /// ```
    /// # use stac_api::Sortby;
    /// let sortby = Sortby::asc("id");
    /// ```
    pub fn asc(field: impl ToString) -> Sortby {
        Sortby {
            field: field.to_string(),
            direction: Direction::Ascending,
        }
    }

    /// Creates a new descending sortby for the field.
    ///
    /// # Examples
    ///
    /// ```
    /// # use stac_api::Sortby;
    /// let sortby = Sortby::desc("id");
    /// ```
    pub fn desc(field: impl ToString) -> Sortby {
        Sortby {
            field: field.to_string(),
            direction: Direction::Descending,
        }
    }
}

impl FromStr for Sortby {
    type Err = Infallible;

    fn from_str(s: &str) -> std::result::Result<Self, Infallible> {
        if s.starts_with('+') {
            Ok(Sortby::asc(&s[1..]))
        } else if s.starts_with('-') {
            Ok(Sortby::desc(&s[1..]))
        } else {
            Ok(Sortby::asc(s))
        }
    }
}

impl Display for Sortby {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        match self.direction {
            Direction::Ascending => write!(f, "{}", self.field),
            Direction::Descending => write!(f, "-{}", self.field),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::Sortby;
    use serde_json::json;

    #[test]
    fn optional_plus() {
        assert_eq!(
            "properties.created".parse::<Sortby>().unwrap(),
            "+properties.created".parse().unwrap()
        );
    }

    #[test]
    fn descending() {
        assert_eq!(Sortby::desc("id"), "-id".parse().unwrap());
    }

    #[test]
    fn names() {
        assert_eq!(
            json!({"field": "foo", "direction": "asc"}),
            serde_json::to_value(Sortby::asc("foo")).unwrap()
        );
        assert_eq!(
            json!({"field": "foo", "direction": "desc"}),
            serde_json::to_value(Sortby::desc("foo")).unwrap()
        );
    }
}