Skip to main content

ignite_core/
query.rs

1//! What the list view asks for: the page, the search text, the sort and the active tab's
2//! conditions.
3
4use crate::list::ListFilter;
5
6/// The sort direction of a list.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum Direction {
9    /// Smallest first.
10    Asc,
11    /// Largest first.
12    Desc,
13}
14
15/// What the list view asks a resource for. Only `search`, `sort` and the tab whose `filter`
16/// applies come from the user, through the query string.
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub struct ListQuery {
19    /// The page to return, 1-based.
20    pub page: i64,
21    /// The number of rows on a page.
22    pub per_page: i64,
23    /// The search text, if the user entered any.
24    pub search: Option<String>,
25    /// The column key to sort by and the direction, if the user chose one.
26    /// The key is one of the resource's sortable columns.
27    pub sort: Option<(String, Direction)>,
28    /// The conditions of the active [`Tab`](crate::Tab), as Filament's `modifyQueryUsing()`:
29    /// the rows and the total must meet them, beside the search. Empty when the resource has
30    /// no tabs, or the tab has no conditions. `#[derive(IgniteResource)]` applies it; a
31    /// hand-written list applies it with [`ListFilter::apply`] or [`ListFilter::matches`].
32    ///
33    /// To count each tab's rows, the panel asks for page 1 with `per_page` 0: only the total
34    /// is read.
35    pub filter: ListFilter,
36}
37
38impl ListQuery {
39    /// The number of rows to skip before the page starts. A page below 1 is
40    /// treated as 1.
41    pub fn offset(&self) -> i64 {
42        (self.page.max(1) - 1) * self.per_page
43    }
44}
45
46/// One page of rows and the total number of matching rows, for the pager.
47#[derive(Debug, Clone, PartialEq, Eq)]
48pub struct Page<T> {
49    /// The rows on this page.
50    pub rows: Vec<T>,
51    /// The number of rows across all pages.
52    pub total: i64,
53}
54
55/// Escapes `%`, `_` and `\` in search text, so a LIKE pattern matches them
56/// literally. Use it inside `%...%` and pass `ESCAPE '\\'` where the database
57/// needs the escape character named.
58pub fn escape_like(text: &str) -> String {
59    let mut escaped = String::with_capacity(text.len());
60    for c in text.chars() {
61        if matches!(c, '%' | '_' | '\\') {
62            escaped.push('\\');
63        }
64        escaped.push(c);
65    }
66    escaped
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    fn query(page: i64, per_page: i64) -> ListQuery {
74        ListQuery {
75            page,
76            per_page,
77            search: None,
78            sort: None,
79            filter: ListFilter::new(),
80        }
81    }
82
83    #[test]
84    fn offset_counts_full_pages_before_the_current_one() {
85        assert_eq!(query(1, 20).offset(), 0);
86        assert_eq!(query(3, 20).offset(), 40);
87    }
88
89    #[test]
90    fn offset_treats_a_page_below_one_as_the_first() {
91        assert_eq!(query(0, 20).offset(), 0);
92        assert_eq!(query(-4, 20).offset(), 0);
93    }
94
95    #[test]
96    fn escape_like_escapes_wildcards_and_the_escape_character() {
97        assert_eq!(escape_like("50%_off\\"), "50\\%\\_off\\\\");
98        assert_eq!(escape_like("plain"), "plain");
99    }
100}