more proto stuff

This commit is contained in:
Michael Zhang 2021-08-07 21:17:36 -05:00
parent 04a6d45802
commit 043feadd71
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
5 changed files with 57 additions and 15 deletions

View file

@ -1,5 +1,9 @@
#![allow(non_snake_case, dead_code)] #![allow(non_snake_case, dead_code)]
mod prelude; // data types
mod rfc2234; pub mod response;
mod rfc3501;
// parsers
pub mod parsers;
pub mod rfc2234;
pub mod rfc3501;

View file

@ -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<ResponseCode<'a>>,
pub info: Option<Cow<'a, str>>,
}
pub enum Status {
Ok,
No,
Bad,
PreAuth,
Bye,
}
pub enum ResponseCode<'a> {
Capabilities(Vec<Capability<'a>>),
}
pub enum Capability<'a> {
Imap4rev1,
Auth(Cow<'a, str>),
Atom(Cow<'a, str>),
}

View file

@ -1,14 +1,8 @@
//! Grammar from https://tools.ietf.org/html/rfc2234#section-6.1 //! Grammar from https://tools.ietf.org/html/rfc2234#section-6.1
use nom::{ use nom::{branch::alt, character::streaming::anychar, multi::many0, sequence::pair, IResult};
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> { pub fn ALPHA(i: &[u8]) -> IResult<&[u8], u8> {
satisfy(|c| (c >= b'a' && c <= b'z') || (c >= b'A' && c <= b'Z'))(i) satisfy(|c| (c >= b'a' && c <= b'z') || (c >= b'A' && c <= b'Z'))(i)

View file

@ -6,20 +6,29 @@ use nom::{
character::streaming::char, character::streaming::char,
combinator::{map, map_res}, combinator::{map, map_res},
multi::{many0, many1}, multi::{many0, many1},
sequence::delimited, sequence::{delimited, terminated},
IResult, IResult,
}; };
use super::rfc2234::{DIGIT, DQUOTE}; use super::rfc2234::{CRLF, DIGIT, DQUOTE};
/// literal = "{" number "}" CRLF *CHAR8 /// literal = "{" number "}" CRLF *CHAR8
/// ; Number represents the number of CHAR8s /// ; Number represents the number of CHAR8s
pub fn literal(i: &[u8]) -> IResult<&[u8], Vec<u8>> { pub fn literal(i: &[u8]) -> IResult<&[u8], Vec<u8>> {
let mut length_of = delimited(char('{'), number, char('}')); let mut length_of = terminated(delimited(char('{'), number, char('}')), CRLF);
let (i, length) = length_of(i)?; let (i, length) = length_of(i)?;
println!("length is: {:?}", (i, length));
map(take(length), |s: &[u8]| s.to_vec())(i) 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]> { pub fn nil(i: &[u8]) -> IResult<&[u8], &[u8]> {
tag_no_case("NIL")(i) tag_no_case("NIL")(i)
} }
@ -29,7 +38,9 @@ pub fn nstring(i: &[u8]) -> IResult<&[u8], Option<Vec<u8>>> {
} }
pub fn number(i: &[u8]) -> IResult<&[u8], u32> { pub fn number(i: &[u8]) -> IResult<&[u8], u32> {
map_res(map_res(many1(DIGIT), String::from_utf8), |s| s.parse::<u32>())(i) map_res(map_res(many1(DIGIT), String::from_utf8), |s| {
s.parse::<u32>()
})(i)
} }
pub fn quoted(i: &[u8]) -> IResult<&[u8], Vec<u8>> { pub fn quoted(i: &[u8]) -> IResult<&[u8], Vec<u8>> {