-
-
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
10 changed files
with
224 additions
and
4 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,135 @@ | ||
//! IMAP4 ID extension | ||
// Additional changes: | ||
// | ||
// command_any ::= "CAPABILITY" / "LOGOUT" / "NOOP" / x_command / id | ||
// response_data ::= "*" SPACE (resp_cond_state / resp_cond_bye / mailbox_data / message_data / capability_data / id_response) | ||
|
||
use abnf_core::streaming::sp; | ||
use imap_types::core::{IString, NString}; | ||
use nom::{ | ||
branch::alt, | ||
bytes::streaming::{tag, tag_no_case}, | ||
combinator::value, | ||
multi::separated_list0, | ||
sequence::{delimited, preceded, separated_pair}, | ||
}; | ||
|
||
use crate::{ | ||
core::{nil, nstring, string}, | ||
decode::IMAPResult, | ||
}; | ||
|
||
/// ```abnf_old | ||
/// id ::= "ID" SPACE id_params_list | ||
/// ``` | ||
pub(crate) fn id(input: &[u8]) -> IMAPResult<&[u8], Vec<(IString, NString)>> { | ||
preceded(tag_no_case("ID "), id_params_list)(input) | ||
} | ||
|
||
/// ```abnf_olf | ||
/// id_response ::= "ID" SPACE id_params_list | ||
/// ``` | ||
#[inline] | ||
pub(crate) fn id_response(input: &[u8]) -> IMAPResult<&[u8], Vec<(IString, NString)>> { | ||
id(input) | ||
} | ||
|
||
/// ```abnf_old | ||
/// id_params_list = "(" #(string SPACE nstring) ")" / nil | ||
/// ``` | ||
/// | ||
/// Note: The ABNF above is non-standard. According to the examples, `#` likely means "separated by whitespace". | ||
/// I'm not sure if `#` means `*` or `1*`, though. However, we can bypass this uncertainty by accepting both. | ||
pub(crate) fn id_params_list(input: &[u8]) -> IMAPResult<&[u8], Vec<(IString, NString)>> { | ||
alt(( | ||
delimited( | ||
tag("("), | ||
separated_list0(sp, separated_pair(string, sp, nstring)), | ||
tag(")"), | ||
), | ||
value(vec![], nil), | ||
))(input) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use imap_types::{ | ||
command::{Command, CommandBody}, | ||
core::{IString, NString}, | ||
response::{Data, Response}, | ||
}; | ||
|
||
use super::*; | ||
use crate::testing::{kat_inverse_command, kat_inverse_response}; | ||
|
||
#[test] | ||
fn test_parse_id() { | ||
let got = id(b"id (\"name\" \"imap-codec\")\r\n").unwrap().1; | ||
assert_eq!( | ||
vec![( | ||
IString::try_from("name").unwrap(), | ||
NString::try_from("imap-codec").unwrap() | ||
)], | ||
got | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_kat_inverse_command_id() { | ||
kat_inverse_command(&[ | ||
( | ||
b"A ID nil\r\n".as_ref(), | ||
b"".as_ref(), | ||
Command::new("A", CommandBody::Id { parameters: vec![] }).unwrap(), | ||
), | ||
( | ||
b"A ID NIL\r\n".as_ref(), | ||
b"".as_ref(), | ||
Command::new("A", CommandBody::Id { parameters: vec![] }).unwrap(), | ||
), | ||
( | ||
b"A ID ()\r\n".as_ref(), | ||
b"".as_ref(), | ||
Command::new("A", CommandBody::Id { parameters: vec![] }).unwrap(), | ||
), | ||
( | ||
b"A ID (\"\" \"\")\r\n".as_ref(), | ||
b"".as_ref(), | ||
Command::new( | ||
"A", | ||
CommandBody::Id { | ||
parameters: vec![( | ||
IString::try_from("").unwrap(), | ||
NString::try_from("").unwrap(), | ||
)], | ||
}, | ||
) | ||
.unwrap(), | ||
), | ||
( | ||
b"A ID (\"name\" \"imap-codec\")\r\n".as_ref(), | ||
b"".as_ref(), | ||
Command::new( | ||
"A", | ||
CommandBody::Id { | ||
parameters: vec![( | ||
IString::try_from("name").unwrap(), | ||
NString::try_from("imap-codec").unwrap(), | ||
)], | ||
}, | ||
) | ||
.unwrap(), | ||
), | ||
]); | ||
} | ||
|
||
#[test] | ||
fn test_kat_inverse_response_id() { | ||
kat_inverse_response(&[( | ||
b"* ID nil\r\n".as_ref(), | ||
b"".as_ref(), | ||
Response::Data(Data::Id { parameters: vec![] }), | ||
)]); | ||
} | ||
} |
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