use std::borrow::Cow;
use nom::{
bytes::streaming::tag_no_case,
character::complete::{space0, space1},
combinator::map,
multi::separated_list0,
sequence::{preceded, separated_pair, tuple},
IResult,
};
use crate::parser::core::astring_utf8;
use crate::parser::rfc3501::mailbox;
use crate::types::*;
pub(crate) fn acl(i: &[u8]) -> IResult<&[u8], Response> {
let (rest, (_, _, mailbox, acls)) = tuple((
tag_no_case("ACL"),
space1,
map(mailbox, Cow::Borrowed),
acl_list,
))(i)?;
Ok((rest, Response::Acl(Acl { mailbox, acls })))
}
fn acl_list(i: &[u8]) -> IResult<&[u8], Vec<AclEntry>> {
preceded(space0, separated_list0(space1, acl_entry))(i)
}
fn acl_entry(i: &[u8]) -> IResult<&[u8], AclEntry> {
let (rest, (identifier, rights)) = separated_pair(
map(astring_utf8, Cow::Borrowed),
space1,
map(astring_utf8, map_text_to_rights),
)(i)?;
Ok((rest, AclEntry { identifier, rights }))
}
pub(crate) fn list_rights(i: &[u8]) -> IResult<&[u8], Response> {
let (rest, (_, _, mailbox, _, identifier, _, required, optional)) = tuple((
tag_no_case("LISTRIGHTS"),
space1,
map(mailbox, Cow::Borrowed),
space1,
map(astring_utf8, Cow::Borrowed),
space1,
map(astring_utf8, map_text_to_rights),
list_rights_optional,
))(i)?;
Ok((
rest,
Response::ListRights(ListRights {
mailbox,
identifier,
required,
optional,
}),
))
}
fn list_rights_optional(i: &[u8]) -> IResult<&[u8], Vec<AclRight>> {
let (rest, items) = preceded(space0, separated_list0(space1, astring_utf8))(i)?;
Ok((
rest,
items
.into_iter()
.flat_map(|s| s.chars().map(|c| c.into()))
.collect(),
))
}
pub(crate) fn my_rights(i: &[u8]) -> IResult<&[u8], Response> {
let (rest, (_, _, mailbox, _, rights)) = tuple((
tag_no_case("MYRIGHTS"),
space1,
map(mailbox, Cow::Borrowed),
space1,
map(astring_utf8, map_text_to_rights),
))(i)?;
Ok((rest, Response::MyRights(MyRights { mailbox, rights })))
}
fn map_text_to_rights(i: &str) -> Vec<AclRight> {
i.chars().map(|c| c.into()).collect()
}