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
use crate::postgres::error::Error;

#[derive(Clone, Debug)]
pub struct QueryOrders(Vec<QueryOrder>);

impl Default for QueryOrders {
    fn default() -> Self {
        QueryOrders(Vec::new())
    }
}

impl QueryOrders {
    pub fn build(&self) -> Result<String, Error> {
        Ok(self.0.clone().into_iter().map(|s| s.build()).collect::<Vec<String>>().join(", "))
    }
    pub fn push(&mut self, field: QueryOrder) {
        self.0.push(field);
    }
    pub fn len(&self) -> usize {
        self.0.len()
    }
}

#[derive(Clone, Debug)]
pub struct QueryOrder {
    name: String, // TBD on DESC ASC NULL FIRST CASE WHEN
}

impl QueryOrder {
    fn build(&self) -> String {
        self.name.clone()
    }
}

impl From<&str> for QueryOrder {
    fn from(field: &str) -> Self {
        QueryOrder {
            name: field.to_owned(),
        }
    }
}

impl From<(&str, &str)> for QueryOrder {
    fn from(tup: (&str, &str)) -> Self {
        QueryOrder {
            name: format!("{} {}", tup.0, tup.1),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_order_1() {
        let field: QueryOrder = QueryOrder {
            name: "u.id ASC".to_owned(),
        };

        assert_eq!(field.build(), "u.id ASC");
    }

    #[test]
    fn test_order_2() {
        let field: QueryOrder = "u.created_at".into();
        assert_eq!(field.build(), "u.created_at");
    }
}