imap_types/
search.rs

1//! Search-related types.
2
3#[cfg(feature = "bounded-static")]
4use bounded_static::ToStatic;
5#[cfg(feature = "serde")]
6use serde::{Deserialize, Serialize};
7
8use crate::{
9    core::{AString, Atom, NonEmptyVec},
10    datetime::NaiveDate,
11    sequence::SequenceSet,
12};
13
14/// The defined search keys.
15#[cfg_attr(feature = "bounded-static", derive(ToStatic))]
16#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
17#[derive(Debug, Clone, PartialEq, Eq, Hash)]
18pub enum SearchKey<'a> {
19    // <Not in RFC.>
20    //
21    // IMAP doesn't have a dedicated AND operator in its search syntax.
22    // ANDing multiple search keys works by concatenating them with an ascii space.
23    // Introducing this variant makes sense, because
24    //   * it may help in understanding the RFC
25    //   * and it can be used to distinguish between a single search key
26    //     and multiple search keys.
27    //
28    // See also the corresponding `search` parser.
29    And(NonEmptyVec<SearchKey<'a>>),
30
31    /// Messages with message sequence numbers corresponding to the
32    /// specified message sequence number set.
33    SequenceSet(SequenceSet),
34
35    /// All messages in the mailbox; the default initial key for ANDing.
36    All,
37
38    /// Messages with the \Answered flag set.
39    Answered,
40
41    /// Messages that contain the specified string in the envelope
42    /// structure's BCC field.
43    Bcc(AString<'a>),
44
45    /// Messages whose internal date (disregarding time and timezone)
46    /// is earlier than the specified date.
47    Before(NaiveDate),
48
49    /// Messages that contain the specified string in the body of the
50    /// message.
51    Body(AString<'a>),
52
53    /// Messages that contain the specified string in the envelope
54    /// structure's CC field.
55    Cc(AString<'a>),
56
57    /// Messages with the \Deleted flag set.
58    Deleted,
59
60    /// Messages with the \Draft flag set.
61    Draft,
62
63    /// Messages with the \Flagged flag set.
64    Flagged,
65
66    /// Messages that contain the specified string in the envelope
67    /// structure's FROM field.
68    From(AString<'a>),
69
70    /// Messages that have a header with the specified field-name (as
71    /// defined in [RFC-2822]) and that contains the specified string
72    /// in the text of the header (what comes after the colon).  If the
73    /// string to search is zero-length, this matches all messages that
74    /// have a header line with the specified field-name regardless of
75    /// the contents.
76    Header(AString<'a>, AString<'a>),
77
78    /// Messages with the specified keyword flag set.
79    Keyword(Atom<'a>),
80
81    /// Messages with an [RFC-2822] size larger than the specified
82    /// number of octets.
83    Larger(u32),
84
85    /// Messages that have the \Recent flag set but not the \Seen flag.
86    /// This is functionally equivalent to "(RECENT UNSEEN)".
87    New,
88
89    /// Messages that do not match the specified search key.
90    Not(Box<SearchKey<'a>>),
91
92    /// Messages that do not have the \Recent flag set.  This is
93    /// functionally equivalent to "NOT RECENT" (as opposed to "NOT
94    /// NEW").
95    Old,
96
97    /// Messages whose internal date (disregarding time and timezone)
98    /// is within the specified date.
99    On(NaiveDate),
100
101    /// Messages that match either search key.
102    Or(Box<SearchKey<'a>>, Box<SearchKey<'a>>),
103
104    /// Messages that have the \Recent flag set.
105    Recent,
106
107    /// Messages that have the \Seen flag set.
108    Seen,
109
110    /// Messages whose [RFC-2822] Date: header (disregarding time and
111    /// timezone) is earlier than the specified date.
112    SentBefore(NaiveDate),
113
114    /// Messages whose [RFC-2822] Date: header (disregarding time and
115    /// timezone) is within the specified date.
116    SentOn(NaiveDate),
117
118    /// Messages whose [RFC-2822] Date: header (disregarding time and
119    /// timezone) is within or later than the specified date.
120    SentSince(NaiveDate),
121
122    /// Messages whose internal date (disregarding time and timezone)
123    /// is within or later than the specified date.
124    Since(NaiveDate),
125
126    /// Messages with an [RFC-2822] size smaller than the specified
127    /// number of octets.
128    Smaller(u32),
129
130    /// Messages that contain the specified string in the envelope
131    /// structure's SUBJECT field.
132    Subject(AString<'a>),
133
134    /// Messages that contain the specified string in the header or
135    /// body of the message.
136    Text(AString<'a>),
137
138    /// Messages that contain the specified string in the envelope
139    /// structure's TO field.
140    To(AString<'a>),
141
142    /// Messages with unique identifiers corresponding to the specified
143    /// unique identifier set.  Sequence set ranges are permitted.
144    Uid(SequenceSet),
145
146    /// Messages that do not have the \Answered flag set.
147    Unanswered,
148
149    /// Messages that do not have the \Deleted flag set.
150    Undeleted,
151
152    /// Messages that do not have the \Draft flag set.
153    Undraft,
154
155    /// Messages that do not have the \Flagged flag set.
156    Unflagged,
157
158    /// Messages that do not have the specified keyword flag set.
159    Unkeyword(Atom<'a>),
160
161    /// Messages that do not have the \Seen flag set.
162    Unseen,
163}
164
165impl<'a> SearchKey<'a> {
166    pub fn uid<S>(sequence_set: S) -> Self
167    where
168        S: Into<SequenceSet>,
169    {
170        Self::Uid(sequence_set.into())
171    }
172}