zino_core/model/
order.rs

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

/// The query order.
#[derive(Debug, Clone)]
pub struct QueryOrder {
    /// The sort field.
    field: SharedString,
    /// The sort order.
    descending: bool,
    /// Whether the nulls appear first or last.
    nulls_first: Option<bool>,
}

impl QueryOrder {
    /// Creates a new instance.
    #[inline]
    pub fn new(field: impl Into<SharedString>, descending: bool) -> Self {
        Self {
            field: field.into(),
            descending,
            nulls_first: None,
        }
    }

    /// Sets the nulls first.
    #[inline]
    pub fn set_nulls_first(&mut self) {
        self.nulls_first = Some(true);
    }

    /// Sets the nulls last.
    #[inline]
    pub fn set_nulls_last(&mut self) {
        self.nulls_first = Some(false);
    }

    /// Returns the sort field.
    #[inline]
    pub fn field(&self) -> &str {
        self.field.as_ref()
    }

    /// Returns `true` if the sort order is ascending.
    #[inline]
    pub fn is_ascending(&self) -> bool {
        !self.descending
    }

    /// Returns `true` if the sort order is descending.
    #[inline]
    pub fn is_descending(&self) -> bool {
        self.descending
    }

    /// Returns `true` if the nulls appear first.
    #[inline]
    pub fn nulls_first(&self) -> bool {
        self.nulls_first.is_some_and(|b| b)
    }

    /// Returns `true` if the nulls appear last.
    #[inline]
    pub fn nulls_last(&self) -> bool {
        self.nulls_first.is_some_and(|b| !b)
    }
}