From 043feadd71a8c67733a90f565f75026436b86b6a Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Sat, 7 Aug 2021 21:17:36 -0500 Subject: [PATCH] more proto stuff --- imap/src/proto/mod.rs | 10 ++++--- imap/src/proto/{prelude.rs => parsers.rs} | 0 imap/src/proto/response.rs | 33 +++++++++++++++++++++++ imap/src/proto/rfc2234.rs | 10 ++----- imap/src/proto/rfc3501.rs | 19 ++++++++++--- 5 files changed, 57 insertions(+), 15 deletions(-) rename imap/src/proto/{prelude.rs => parsers.rs} (100%) create mode 100644 imap/src/proto/response.rs diff --git a/imap/src/proto/mod.rs b/imap/src/proto/mod.rs index 01a147d..7f172d0 100644 --- a/imap/src/proto/mod.rs +++ b/imap/src/proto/mod.rs @@ -1,5 +1,9 @@ #![allow(non_snake_case, dead_code)] -mod prelude; -mod rfc2234; -mod rfc3501; +// data types +pub mod response; + +// parsers +pub mod parsers; +pub mod rfc2234; +pub mod rfc3501; diff --git a/imap/src/proto/prelude.rs b/imap/src/proto/parsers.rs similarity index 100% rename from imap/src/proto/prelude.rs rename to imap/src/proto/parsers.rs diff --git a/imap/src/proto/response.rs b/imap/src/proto/response.rs new file mode 100644 index 0000000..d3f6e91 --- /dev/null +++ b/imap/src/proto/response.rs @@ -0,0 +1,33 @@ +use std::borrow::Cow; + +#[derive(Clone)] +pub struct Tag(pub String); + +pub enum Response<'a> { + Done(ResponseDone<'a>), +} + +pub struct ResponseDone<'a> { + pub tag: Tag, + pub status: Status, + pub code: Option>, + pub info: Option>, +} + +pub enum Status { + Ok, + No, + Bad, + PreAuth, + Bye, +} + +pub enum ResponseCode<'a> { + Capabilities(Vec>), +} + +pub enum Capability<'a> { + Imap4rev1, + Auth(Cow<'a, str>), + Atom(Cow<'a, str>), +} diff --git a/imap/src/proto/rfc2234.rs b/imap/src/proto/rfc2234.rs index 069fdc3..eb91c3d 100644 --- a/imap/src/proto/rfc2234.rs +++ b/imap/src/proto/rfc2234.rs @@ -1,14 +1,8 @@ //! Grammar from https://tools.ietf.org/html/rfc2234#section-6.1 -use nom::{ - branch::alt, - character::streaming::{anychar}, - multi::many0, - sequence::pair, - IResult, -}; +use nom::{branch::alt, character::streaming::anychar, multi::many0, sequence::pair, IResult}; -use super::prelude::{satisfy, skip, byte}; +use super::parsers::{byte, satisfy, skip}; pub fn ALPHA(i: &[u8]) -> IResult<&[u8], u8> { satisfy(|c| (c >= b'a' && c <= b'z') || (c >= b'A' && c <= b'Z'))(i) diff --git a/imap/src/proto/rfc3501.rs b/imap/src/proto/rfc3501.rs index 76d2af7..5237f42 100644 --- a/imap/src/proto/rfc3501.rs +++ b/imap/src/proto/rfc3501.rs @@ -6,20 +6,29 @@ use nom::{ character::streaming::char, combinator::{map, map_res}, multi::{many0, many1}, - sequence::delimited, + sequence::{delimited, terminated}, IResult, }; -use super::rfc2234::{DIGIT, DQUOTE}; +use super::rfc2234::{CRLF, DIGIT, DQUOTE}; /// literal = "{" number "}" CRLF *CHAR8 /// ; Number represents the number of CHAR8s pub fn literal(i: &[u8]) -> IResult<&[u8], Vec> { - let mut length_of = delimited(char('{'), number, char('}')); + let mut length_of = terminated(delimited(char('{'), number, char('}')), CRLF); let (i, length) = length_of(i)?; + println!("length is: {:?}", (i, length)); map(take(length), |s: &[u8]| s.to_vec())(i) } +#[test] +fn test_literal() { + assert_eq!( + literal(b"{13}\r\nHello, world!").unwrap().1, + b"Hello, world!" + ); +} + pub fn nil(i: &[u8]) -> IResult<&[u8], &[u8]> { tag_no_case("NIL")(i) } @@ -29,7 +38,9 @@ pub fn nstring(i: &[u8]) -> IResult<&[u8], Option>> { } pub fn number(i: &[u8]) -> IResult<&[u8], u32> { - map_res(map_res(many1(DIGIT), String::from_utf8), |s| s.parse::())(i) + map_res(map_res(many1(DIGIT), String::from_utf8), |s| { + s.parse::() + })(i) } pub fn quoted(i: &[u8]) -> IResult<&[u8], Vec> {