-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
265 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use std::io::Write; | ||
|
||
use abnf_core::streaming::sp; | ||
use imap_types::{ | ||
command::CommandBody, | ||
core::NonEmptyVec, | ||
extensions::sort::{SortCriterion, SortKey}, | ||
}; | ||
use nom::{ | ||
branch::alt, | ||
bytes::streaming::{tag, tag_no_case}, | ||
combinator::{map, opt, value}, | ||
multi::separated_list1, | ||
sequence::{delimited, tuple}, | ||
}; | ||
|
||
use crate::{ | ||
decode::IMAPResult, | ||
encode::{EncodeContext, EncodeIntoContext}, | ||
search::search_criteria, | ||
}; | ||
|
||
/// ```abnf | ||
/// sort = ["UID" SP] "SORT" SP sort-criteria SP search-criteria | ||
/// ``` | ||
pub(crate) fn sort(input: &[u8]) -> IMAPResult<&[u8], CommandBody> { | ||
let mut parser = tuple(( | ||
map(opt(tag_no_case("UID ")), |thing| thing.is_some()), | ||
tag_no_case("SORT "), | ||
sort_criteria, | ||
sp, | ||
search_criteria, | ||
)); | ||
|
||
let (remaining, (uid, _, sort_criteria, _, (charset, search_key))) = parser(input)?; | ||
|
||
Ok(( | ||
remaining, | ||
CommandBody::Sort { | ||
sort_criteria, | ||
charset, | ||
search_criteria: search_key, | ||
uid, | ||
}, | ||
)) | ||
} | ||
|
||
/// ```abnf | ||
/// sort-criteria = "(" sort-criterion *(SP sort-criterion) ")" | ||
/// ``` | ||
pub(crate) fn sort_criteria(input: &[u8]) -> IMAPResult<&[u8], NonEmptyVec<SortCriterion>> { | ||
delimited( | ||
tag("("), | ||
map( | ||
separated_list1(sp, sort_criterion), | ||
NonEmptyVec::unvalidated, | ||
), | ||
tag(")"), | ||
)(input) | ||
} | ||
|
||
/// ```abnf | ||
/// sort-criterion = ["REVERSE" SP] sort-key | ||
/// ``` | ||
pub(crate) fn sort_criterion(input: &[u8]) -> IMAPResult<&[u8], SortCriterion> { | ||
let mut parser = tuple(( | ||
map(opt(tag_no_case(b"REVERSE ")), |thing| thing.is_some()), | ||
sort_key, | ||
)); | ||
|
||
let (remaining, (reverse, key)) = parser(input)?; | ||
|
||
Ok((remaining, SortCriterion { reverse, key })) | ||
} | ||
|
||
/// ```abnf | ||
/// sort-key = "ARRIVAL" / "CC" / "DATE" / "FROM" / "SIZE" / "SUBJECT" / "TO" | ||
/// ``` | ||
pub(crate) fn sort_key(input: &[u8]) -> IMAPResult<&[u8], SortKey> { | ||
alt(( | ||
value(SortKey::Arrival, tag_no_case("ARRIVAL")), | ||
value(SortKey::Cc, tag_no_case("CC")), | ||
value(SortKey::Date, tag_no_case("DATE")), | ||
value(SortKey::From, tag_no_case("FROM")), | ||
value(SortKey::Size, tag_no_case("SIZE")), | ||
value(SortKey::Subject, tag_no_case("SUBJECT")), | ||
value(SortKey::To, tag_no_case("TO")), | ||
))(input) | ||
} | ||
|
||
impl EncodeIntoContext for SortCriterion { | ||
fn encode_ctx(&self, ctx: &mut EncodeContext) -> std::io::Result<()> { | ||
if self.reverse { | ||
ctx.write_all(b"REVERSE ")?; | ||
} | ||
|
||
ctx.write_all(self.key.as_ref().as_bytes()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#[cfg(feature = "arbitrary")] | ||
use arbitrary::Arbitrary; | ||
#[cfg(feature = "bounded-static")] | ||
use bounded_static::ToStatic; | ||
#[cfg(feature = "serde")] | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))] | ||
#[cfg_attr(feature = "bounded-static", derive(ToStatic))] | ||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||
#[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
pub struct SortCriterion { | ||
pub reverse: bool, | ||
pub key: SortKey, | ||
} | ||
|
||
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))] | ||
#[cfg_attr(feature = "bounded-static", derive(ToStatic))] | ||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||
#[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
pub enum SortKey { | ||
Arrival, | ||
Cc, | ||
Date, | ||
From, | ||
Size, | ||
Subject, | ||
To, | ||
} | ||
|
||
impl AsRef<str> for SortKey { | ||
fn as_ref(&self) -> &str { | ||
match self { | ||
SortKey::Arrival => "ARRIVAL", | ||
SortKey::Cc => "CC", | ||
SortKey::Date => "DATE", | ||
SortKey::From => "FROM", | ||
SortKey::Size => "SIZE", | ||
SortKey::Subject => "SUBJECT", | ||
SortKey::To => "TO", | ||
} | ||
} | ||
} |
Oops, something went wrong.