This commit is contained in:
Michael Zhang 2021-02-22 18:21:12 -06:00
parent 3d0724e342
commit 27818bd93a
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
3 changed files with 48 additions and 26 deletions

View file

@ -16,6 +16,7 @@ Goals:
Stretch goals: Stretch goals:
- Unified "feed" that any app can submit to. - Unified "feed" that any app can submit to.
- Submit notifications to gotify-shaped notification servers. - Submit notifications to gotify-shaped notification servers.
- JMAP implementation.
- RSS aggregator. - RSS aggregator.
- IRC client?? - IRC client??

View file

@ -1,3 +1,6 @@
use std::fmt::Debug;
use std::str::FromStr;
use pest::{ use pest::{
error::Error, error::Error,
iterators::{Pair, Pairs}, iterators::{Pair, Pairs},
@ -99,10 +102,10 @@ fn build_resp_cond_state(pair: Pair<Rule>) -> (Status, Option<ResponseCode>, Opt
println!("pairs: {:#?}", pairs); println!("pairs: {:#?}", pairs);
let pair = pairs.next().unwrap(); let pair = pairs.next().unwrap();
let mut pairs = pair.into_inner(); let pairs = pair.into_inner();
for pair in pairs { for pair in pairs {
match pair.as_rule() { match pair.as_rule() {
Rule::resp_text_code => code = Some(build_resp_code(pair)), Rule::resp_text_code => code = build_resp_code(pair),
Rule::text => information = Some(pair.as_str().to_owned()), Rule::text => information = Some(pair.as_str().to_owned()),
_ => unreachable!("{:#?}", pair), _ => unreachable!("{:#?}", pair),
} }
@ -111,22 +114,19 @@ fn build_resp_cond_state(pair: Pair<Rule>) -> (Status, Option<ResponseCode>, Opt
(status, code, information) (status, code, information)
} }
fn build_resp_code(pair: Pair<Rule>) -> ResponseCode { fn build_resp_code(pair: Pair<Rule>) -> Option<ResponseCode> {
if !matches!(pair.as_rule(), Rule::resp_text_code) { if !matches!(pair.as_rule(), Rule::resp_text_code) {
unreachable!("{:#?}", pair); unreachable!("{:#?}", pair);
} }
let mut pairs = pair.into_inner(); let mut pairs = pair.into_inner();
let pair = pairs.next().unwrap(); let pair = pairs.next()?;
match pair.as_rule() { Some(match pair.as_rule() {
Rule::resp_text_code_unseen => { Rule::resp_text_code_readwrite => ResponseCode::ReadWrite,
let mut pairs = pair.into_inner(); Rule::resp_text_code_uidvalidity => ResponseCode::UidValidity(build_number(pair)),
let pair = pairs.next().unwrap(); Rule::resp_text_code_unseen => ResponseCode::Unseen(build_number(pair)),
let number = pair.as_str().parse::<u32>().unwrap();
ResponseCode::Unseen(number)
}
_ => unreachable!("{:#?}", pair), _ => unreachable!("{:#?}", pair),
} })
} }
fn build_status(pair: Pair<Rule>) -> Status { fn build_status(pair: Pair<Rule>) -> Status {
@ -173,28 +173,28 @@ fn build_mailbox_data(pair: Pair<Rule>) -> MailboxData {
let mut pairs = pair.into_inner(); let mut pairs = pair.into_inner();
let pair = pairs.next().unwrap(); let pair = pairs.next().unwrap();
match pair.as_rule() { match pair.as_rule() {
Rule::mailbox_data_exists => { Rule::mailbox_data_exists => MailboxData::Exists(build_number(pair)),
let mut pairs = pair.into_inner();
let pair = pairs.next().unwrap();
let number = pair.as_str().parse::<u32>().unwrap();
MailboxData::Exists(number)
}
Rule::mailbox_data_flags => { Rule::mailbox_data_flags => {
let mut pairs = pair.into_inner(); let mut pairs = pair.into_inner();
let pair = pairs.next().unwrap(); let pair = pairs.next().unwrap();
let flags = build_flag_list(pair); let flags = build_flag_list(pair);
MailboxData::Flags(flags) MailboxData::Flags(flags)
} }
Rule::mailbox_data_recent => { Rule::mailbox_data_recent => MailboxData::Recent(build_number(pair)),
let mut pairs = pair.into_inner();
let pair = pairs.next().unwrap();
let number = pair.as_str().parse::<u32>().unwrap();
MailboxData::Recent(number)
}
_ => unreachable!("{:#?}", pair), _ => unreachable!("{:#?}", pair),
} }
} }
fn build_number<T>(pair: Pair<Rule>) -> T
where
T: FromStr,
T::Err: Debug,
{
let mut pairs = pair.into_inner();
let pair = pairs.next().unwrap();
pair.as_str().parse::<T>().unwrap()
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -273,5 +273,24 @@ mod tests {
information: Some("Message 17 is the first unseen message".to_owned()), information: Some("Message 17 is the first unseen message".to_owned()),
}) })
); );
assert_eq!(
parse_response("* OK [UIDVALIDITY 3857529045] UIDs valid\r\n"),
Ok(Response::Data {
status: Status::Ok,
code: Some(ResponseCode::UidValidity(3857529045)),
information: Some("UIDs valid".to_owned()),
})
);
assert_eq!(
parse_response("a002 OK [READ-WRITE] SELECT completed\r\n"),
Ok(Response::Done {
tag: "a002".to_owned(),
status: Status::Ok,
code: Some(ResponseCode::ReadWrite),
information: Some("SELECT completed".to_owned()),
})
);
} }
} }

View file

@ -92,9 +92,11 @@ resp_cond_state = { resp_status ~ sp ~ resp_text }
resp_specials = @{ "]" } resp_specials = @{ "]" }
resp_status = { (^"OK" | ^"NO" | ^"BAD") } resp_status = { (^"OK" | ^"NO" | ^"BAD") }
resp_text = { ("[" ~ resp_text_code ~ "]" ~ sp)? ~ text } resp_text = { ("[" ~ resp_text_code ~ "]" ~ sp)? ~ text }
resp_text_code = { ^"ALERT" | (^"BADCHARSET" ~ (sp ~ "(" ~ astring ~ (sp ~ astring)* ~ ")")?) | capability_data | ^"PARSE" | (^"PERMANENTFLAGS" ~ sp ~ "(" ~ (flag_perm ~ (sp ~ flag_perm)*)? ~ ")") | ^"READ-ONLY" | ^"READ-WRITE" | ^"TRYCREATE" | (^"UIDNEXT" ~ sp ~ nz_number) | (^"UIDVALIDITY" ~ sp ~ nz_number) | resp_text_code_unseen | (atom ~ (sp ~ resp_text_code_atom)?) } resp_text_code = { ^"ALERT" | (^"BADCHARSET" ~ (sp ~ "(" ~ astring ~ (sp ~ astring)* ~ ")")?) | capability_data | ^"PARSE" | (^"PERMANENTFLAGS" ~ sp ~ "(" ~ (flag_perm ~ (sp ~ flag_perm)*)? ~ ")") | ^"READ-ONLY" | resp_text_code_readwrite | ^"TRYCREATE" | (^"UIDNEXT" ~ sp ~ nz_number) | resp_text_code_uidvalidity | resp_text_code_unseen | (atom ~ (sp ~ resp_text_code_atom)?) }
resp_text_code_unseen = { ^"UNSEEN" ~ sp ~ nz_number }
resp_text_code_atom = @{ (!"]" ~ text_char){1,} } resp_text_code_atom = @{ (!"]" ~ text_char){1,} }
resp_text_code_readwrite = { ^"READ-WRITE" }
resp_text_code_uidvalidity = { ^"UIDVALIDITY" ~ sp ~ nz_number }
resp_text_code_unseen = { ^"UNSEEN" ~ sp ~ nz_number }
response = { continue_req | response_data | response_done } response = { continue_req | response_data | response_done }
response_data = { "*" ~ sp ~ (resp_cond_state | resp_cond_bye | mailbox_data | message_data | capability_data) ~ crlf } response_data = { "*" ~ sp ~ (resp_cond_state | resp_cond_bye | mailbox_data | message_data | capability_data) ~ crlf }
response_done = { response_tagged | response_fatal } response_done = { response_tagged | response_fatal }