1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use rustyknife::nom::branch::alt;
use rustyknife::nom::combinator::map;

use rustyknife::rfc5321::{
    bdat_command, command as base_command, starttls_command, Command as BaseCommand, UTF8Policy,
};
use rustyknife::xforward::{command as xforward_command, Param as XforwardParam};
use rustyknife::NomResult;

#[derive(Debug)]
pub enum Command {
    Base(BaseCommand),
    Ext(Ext),
}

#[derive(Debug)]
pub enum Ext {
    STARTTLS,
    BDAT(u64, bool),
    XFORWARD(Vec<XforwardParam>),
}

pub fn command<P: UTF8Policy>(input: &[u8]) -> NomResult<'_, Command> {
    alt((
        map(base_command::<P>, Command::Base),
        map(starttls_command, |_| Command::Ext(Ext::STARTTLS)),
        map(bdat_command, |(size, last)| {
            Command::Ext(Ext::BDAT(size, last))
        }),
        map(xforward_command, |params| {
            Command::Ext(Ext::XFORWARD(params))
        }),
    ))(input)
}