Skip to main content

io_email/search/sort/
query.rs

1//! # Search emails sort query
2//!
3//! Exposes [`SearchEmailsSortQuery`] and friends, the AST produced by
4//! [`super::parser::query`].
5
6use alloc::vec::Vec;
7
8/// The search emails sort query.
9///
10/// Just a list of [`SearchEmailsSorter`]s, applied left-to-right (the
11/// first sorter is the primary sort key).
12pub type SearchEmailsSortQuery = Vec<SearchEmailsSorter>;
13
14/// A single sorter: a kind plus an order.
15#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
16pub struct SearchEmailsSorter(
17    /// The search emails sorter kind.
18    pub SearchEmailsSorterKind,
19    /// The search emails sorter order.
20    pub SearchEmailsSorterOrder,
21);
22
23impl SearchEmailsSorter {
24    /// Build a sorter from a kind and an order.
25    pub fn new(kind: SearchEmailsSorterKind, order: SearchEmailsSorterOrder) -> Self {
26        Self(kind, order)
27    }
28}
29
30impl From<(SearchEmailsSorterKind, SearchEmailsSorterOrder)> for SearchEmailsSorter {
31    fn from((kind, order): (SearchEmailsSorterKind, SearchEmailsSorterOrder)) -> Self {
32        SearchEmailsSorter::new(kind, order)
33    }
34}
35
36impl From<(SearchEmailsSorterKind, Option<SearchEmailsSorterOrder>)> for SearchEmailsSorter {
37    fn from((kind, order): (SearchEmailsSorterKind, Option<SearchEmailsSorterOrder>)) -> Self {
38        (kind, order.unwrap_or_default()).into()
39    }
40}
41
42impl From<SearchEmailsSorterKind> for SearchEmailsSorter {
43    fn from(kind: SearchEmailsSorterKind) -> Self {
44        (kind, None).into()
45    }
46}
47
48/// The property a sorter sorts emails on.
49///
50/// `Date` resolves to the `Date:` header (sent-at); see the
51/// per-protocol converters for how that maps to each backend.
52#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
53pub enum SearchEmailsSorterKind {
54    /// Sort emails by message header `Date`.
55    Date,
56
57    /// Sort emails by envelope sender.
58    From,
59
60    /// Sort emails by envelope recipient.
61    To,
62
63    /// Sort emails by message header `Subject`.
64    Subject,
65}
66
67/// Sort direction. Defaults to ascending.
68#[derive(Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
69pub enum SearchEmailsSorterOrder {
70    /// Sort emails by ascending order.
71    #[default]
72    Ascending,
73
74    /// Sort emails by descending order.
75    Descending,
76}