refactor: define tokens with macro and const fn

This commit is contained in:
Lukas Lihotzki 2023-04-03 14:35:09 +02:00
parent 94c16ce6d7
commit 20ce422db4
4 changed files with 183 additions and 834 deletions

View file

@ -63,6 +63,7 @@ use std::fmt::Display;
pub mod request; pub mod request;
pub mod response; pub mod response;
mod tokens;
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum Request<T> { pub enum Request<T> {

View file

@ -21,657 +21,120 @@
* for more details. * for more details.
*/ */
use crate::tokens::{define_tokens_64, define_tokens_128};
pub mod parser; pub mod parser;
pub mod receiver; pub mod receiver;
// SMTP commands pub const AUTH: u64 = crate::tokens::token64("AUTH"); // special, because it is `pub` instead of `pub(crate)`
pub(crate) const EHLO: u64 =
(b'E' as u64) | (b'H' as u64) << 8 | (b'L' as u64) << 16 | (b'O' as u64) << 24;
pub(crate) const HELO: u64 =
(b'H' as u64) | (b'E' as u64) << 8 | (b'L' as u64) << 16 | (b'O' as u64) << 24;
pub(crate) const LHLO: u64 =
(b'L' as u64) | (b'H' as u64) << 8 | (b'L' as u64) << 16 | (b'O' as u64) << 24;
pub(crate) const MAIL: u64 =
(b'M' as u64) | (b'A' as u64) << 8 | (b'I' as u64) << 16 | (b'L' as u64) << 24;
pub(crate) const RCPT: u64 =
(b'R' as u64) | (b'C' as u64) << 8 | (b'P' as u64) << 16 | (b'T' as u64) << 24;
pub(crate) const DATA: u64 =
(b'D' as u64) | (b'A' as u64) << 8 | (b'T' as u64) << 16 | (b'A' as u64) << 24;
pub(crate) const BDAT: u64 =
(b'B' as u64) | (b'D' as u64) << 8 | (b'A' as u64) << 16 | (b'T' as u64) << 24;
pub(crate) const RSET: u64 =
(b'R' as u64) | (b'S' as u64) << 8 | (b'E' as u64) << 16 | (b'T' as u64) << 24;
pub(crate) const VRFY: u64 =
(b'V' as u64) | (b'R' as u64) << 8 | (b'F' as u64) << 16 | (b'Y' as u64) << 24;
pub(crate) const EXPN: u64 =
(b'E' as u64) | (b'X' as u64) << 8 | (b'P' as u64) << 16 | (b'N' as u64) << 24;
pub(crate) const HELP: u64 =
(b'H' as u64) | (b'E' as u64) << 8 | (b'L' as u64) << 16 | (b'P' as u64) << 24;
pub(crate) const NOOP: u64 =
(b'N' as u64) | (b'O' as u64) << 8 | (b'O' as u64) << 16 | (b'P' as u64) << 24;
pub(crate) const QUIT: u64 =
(b'Q' as u64) | (b'U' as u64) << 8 | (b'I' as u64) << 16 | (b'T' as u64) << 24;
pub(crate) const ETRN: u64 =
(b'E' as u64) | (b'T' as u64) << 8 | (b'R' as u64) << 16 | (b'N' as u64) << 24;
pub(crate) const ATRN: u64 =
(b'A' as u64) | (b'T' as u64) << 8 | (b'R' as u64) << 16 | (b'N' as u64) << 24;
pub const AUTH: u64 =
(b'A' as u64) | (b'U' as u64) << 8 | (b'T' as u64) << 16 | (b'H' as u64) << 24;
pub(crate) const BURL: u64 =
(b'B' as u64) | (b'U' as u64) << 8 | (b'R' as u64) << 16 | (b'L' as u64) << 24;
pub(crate) const STARTTLS: u64 = (b'S' as u64)
| (b'T' as u64) << 8
| (b'A' as u64) << 16
| (b'R' as u64) << 24
| (b'T' as u64) << 32
| (b'T' as u64) << 40
| (b'L' as u64) << 48
| (b'S' as u64) << 56;
// Arguments define_tokens_64! {
pub(crate) const FROM: u64 = // SMTP commands
(b'F' as u64) | (b'R' as u64) << 8 | (b'O' as u64) << 16 | (b'M' as u64) << 24; EHLO,
pub(crate) const TO: u64 = (b'T' as u64) | (b'O' as u64) << 8; HELO,
pub(crate) const LAST: u64 = LHLO,
(b'L' as u64) | (b'A' as u64) << 8 | (b'S' as u64) << 16 | (b'T' as u64) << 24; MAIL,
RCPT,
DATA,
BDAT,
RSET,
VRFY,
EXPN,
HELP,
NOOP,
QUIT,
ETRN,
ATRN,
BURL,
STARTTLS,
// Parameters // Arguments
pub(crate) const BODY: u128 = FROM,
(b'B' as u128) | (b'O' as u128) << 8 | (b'D' as u128) << 16 | (b'Y' as u128) << 24; TO,
pub(crate) const SEVENBIT: u128 = LAST,
(b'7' as u128) | (b'B' as u128) << 8 | (b'I' as u128) << 16 | (b'T' as u128) << 24;
pub(crate) const EIGHBITMIME: u128 = (b'8' as u128)
| (b'B' as u128) << 8
| (b'I' as u128) << 16
| (b'T' as u128) << 24
| (b'M' as u128) << 32
| (b'I' as u128) << 40
| (b'M' as u128) << 48
| (b'E' as u128) << 56;
pub(crate) const BINARYMIME: u128 = (b'B' as u128)
| (b'I' as u128) << 8
| (b'N' as u128) << 16
| (b'A' as u128) << 24
| (b'R' as u128) << 32
| (b'Y' as u128) << 40
| (b'M' as u128) << 48
| (b'I' as u128) << 56
| (b'M' as u128) << 64
| (b'E' as u128) << 72;
pub(crate) const SIZE: u128 =
(b'S' as u128) | (b'I' as u128) << 8 | (b'Z' as u128) << 16 | (b'E' as u128) << 24;
pub(crate) const TRANSID: u128 = (b'T' as u128)
| (b'R' as u128) << 8
| (b'A' as u128) << 16
| (b'N' as u128) << 24
| (b'S' as u128) << 32
| (b'I' as u128) << 40
| (b'D' as u128) << 48;
pub(crate) const BY: u128 = (b'B' as u128) | (b'Y' as u128) << 8;
pub(crate) const N: u64 = b'N' as u64; // Parameters
pub(crate) const NT: u64 = (b'N' as u64) | (b'T' as u64) << 8; N,
pub(crate) const C: u64 = b'C' as u64; NT,
pub(crate) const R: u64 = b'R' as u64; C,
pub(crate) const RT: u64 = (b'R' as u64) | (b'T' as u64) << 8; R,
RT,
FULL,
HDRS,
RFC822,
}
pub(crate) const NOTIFY: u128 = (b'N' as u128) define_tokens_128! {
| (b'O' as u128) << 8 // Parameters
| (b'T' as u128) << 16 BODY,
| (b'I' as u128) << 24 SEVENBIT = "7BIT",
| (b'F' as u128) << 32 EIGHBITMIME = "8BITMIME",
| (b'Y' as u128) << 40; BINARYMIME,
pub(crate) const ORCPT: u128 = (b'O' as u128) SIZE,
| (b'R' as u128) << 8 TRANSID,
| (b'C' as u128) << 16 BY,
| (b'P' as u128) << 24 NOTIFY,
| (b'T' as u128) << 32; ORCPT,
pub(crate) const RFC822: u64 = (b'R' as u64) RET,
| (b'F' as u64) << 8 ENVID,
| (b'C' as u64) << 16 NEVER,
| (b'8' as u64) << 24 SUCCESS,
| (b'2' as u64) << 32 FAILURE,
| (b'2' as u64) << 40; DELAY,
pub(crate) const RET: u128 = (b'R' as u128) | (b'E' as u128) << 8 | (b'T' as u128) << 16; SOLICIT,
pub(crate) const ENVID: u128 = (b'E' as u128) MTRK,
| (b'N' as u128) << 8 AUTH_ = "AUTH",
| (b'V' as u128) << 16 HOLDFOR,
| (b'I' as u128) << 24 HOLDUNTIL,
| (b'D' as u128) << 32; SMTPUTF8,
pub(crate) const NEVER: u128 = (b'N' as u128) CONPERM,
| (b'E' as u128) << 8 CONNEG,
| (b'V' as u128) << 16 MT_PRIORITY = "MT-PRIORITY",
| (b'E' as u128) << 24 RRVS,
| (b'R' as u128) << 32; REQUIRETLS,
pub(crate) const SUCCESS: u128 = (b'S' as u128) _9798_M_DSA_SHA1 = "9798-M-DSA-SHA1",
| (b'U' as u128) << 8 _9798_M_ECDSA_SHA = "9798-M-ECDSA-SHA",
| (b'C' as u128) << 16 _9798_M_RSA_SHA1_ = "9798-M-RSA-SHA1-",
| (b'C' as u128) << 24 _9798_U_DSA_SHA1 = "9798-U-DSA-SHA1",
| (b'E' as u128) << 32 _9798_U_ECDSA_SHA = "9798-U-ECDSA-SHA",
| (b'S' as u128) << 40 _9798_U_RSA_SHA1_ = "9798-U-RSA-SHA1-",
| (b'S' as u128) << 48; ANONYMOUS,
pub(crate) const FAILURE: u128 = (b'F' as u128) CRAM_MD5 = "CRAM-MD5",
| (b'A' as u128) << 8 DIGEST_MD5 = "DIGEST-MD5",
| (b'I' as u128) << 16 EAP_AES128 = "EAP-AES128",
| (b'L' as u128) << 24 EAP_AES128_PLUS = "EAP-AES128-PLUS",
| (b'U' as u128) << 32 ECDH_X25519_CHAL = "ECDH-X25519-CHAL",
| (b'R' as u128) << 40 ECDSA_NIST256P_C = "ECDSA-NIST256P-C",
| (b'E' as u128) << 48; EXTERNAL,
pub(crate) const DELAY: u128 = (b'D' as u128) GS2_KRB5 = "GS2-KRB5",
| (b'E' as u128) << 8 GS2_KRB5_PLUS = "GS2-KRB5-PLUS",
| (b'L' as u128) << 16 GSS_SPNEGO = "GSS-SPNEGO",
| (b'A' as u128) << 24 GSSAPI,
| (b'Y' as u128) << 32; KERBEROS_V4 = "KERBEROS-V4",
pub(crate) const FULL: u64 = KERBEROS_V5 = "KERBEROS-V5",
(b'F' as u64) | (b'U' as u64) << 8 | (b'L' as u64) << 16 | (b'L' as u64) << 24; LOGIN,
pub(crate) const HDRS: u64 = NMAS_SAMBA_AUTH = "NMAS-SAMBA-AUTH",
(b'H' as u64) | (b'D' as u64) << 8 | (b'R' as u64) << 16 | (b'S' as u64) << 24; NMAS_AUTHEN = "NMAS-AUTHEN",
pub(crate) const SOLICIT: u128 = (b'S' as u128) NMAS_LOGIN = "NMAS-LOGIN",
| (b'O' as u128) << 8 NTLM,
| (b'L' as u128) << 16 OAUTH10A,
| (b'I' as u128) << 24 OAUTHBEARER,
| (b'C' as u128) << 32 OPENID20,
| (b'I' as u128) << 40 OTP,
| (b'T' as u128) << 48; PLAIN,
pub(crate) const MTRK: u128 = SAML20,
(b'M' as u128) | (b'T' as u128) << 8 | (b'R' as u128) << 16 | (b'K' as u128) << 24; SCRAM_SHA_1 = "SCRAM-SHA-1",
pub(crate) const AUTH_: u128 = SCRAM_SHA_1_PLUS = "SCRAM-SHA-1-PLUS",
(b'A' as u128) | (b'U' as u128) << 8 | (b'T' as u128) << 16 | (b'H' as u128) << 24; SCRAM_SHA_256 = "SCRAM-SHA-256",
pub(crate) const HOLDFOR: u128 = (b'H' as u128) SCRAM_SHA_256_PL = "SCRAM-SHA-256-PL",
| (b'O' as u128) << 8 SECURID,
| (b'L' as u128) << 16 SKEY,
| (b'D' as u128) << 24 SPNEGO,
| (b'F' as u128) << 32 SPNEGO_PLUS = "SPNEGO-PLUS",
| (b'O' as u128) << 40 SXOVER_PLUS = "SXOVER-PLUS",
| (b'R' as u128) << 48; XOAUTH,
pub(crate) const HOLDUNTIL: u128 = (b'H' as u128) XOAUTH2,
| (b'O' as u128) << 8 }
| (b'L' as u128) << 16
| (b'D' as u128) << 24
| (b'U' as u128) << 32
| (b'N' as u128) << 40
| (b'T' as u128) << 48
| (b'I' as u128) << 56
| (b'L' as u128) << 64;
pub(crate) const SMTPUTF8: u128 = (b'S' as u128)
| (b'M' as u128) << 8
| (b'T' as u128) << 16
| (b'P' as u128) << 24
| (b'U' as u128) << 32
| (b'T' as u128) << 40
| (b'F' as u128) << 48
| (b'8' as u128) << 56;
pub(crate) const CONPERM: u128 = (b'C' as u128)
| (b'O' as u128) << 8
| (b'N' as u128) << 16
| (b'P' as u128) << 24
| (b'E' as u128) << 32
| (b'R' as u128) << 40
| (b'M' as u128) << 48;
pub(crate) const CONNEG: u128 = (b'C' as u128)
| (b'O' as u128) << 8
| (b'N' as u128) << 16
| (b'N' as u128) << 24
| (b'E' as u128) << 32
| (b'G' as u128) << 40;
pub(crate) const MT_PRIORITY: u128 = (b'M' as u128)
| (b'T' as u128) << 8
| (b'-' as u128) << 16
| (b'P' as u128) << 24
| (b'R' as u128) << 32
| (b'I' as u128) << 40
| (b'O' as u128) << 48
| (b'R' as u128) << 56
| (b'I' as u128) << 64
| (b'T' as u128) << 72
| (b'Y' as u128) << 80;
pub(crate) const RRVS: u128 =
(b'R' as u128) | (b'R' as u128) << 8 | (b'V' as u128) << 16 | (b'S' as u128) << 24;
pub(crate) const REQUIRETLS: u128 = (b'R' as u128)
| (b'E' as u128) << 8
| (b'Q' as u128) << 16
| (b'U' as u128) << 24
| (b'I' as u128) << 32
| (b'R' as u128) << 40
| (b'E' as u128) << 48
| (b'T' as u128) << 56
| (b'L' as u128) << 64
| (b'S' as u128) << 72;
// SASL Mechanisms
pub(crate) const _9798_M_DSA_SHA1: u128 = (b'9' as u128)
| (b'7' as u128) << 8
| (b'9' as u128) << 16
| (b'8' as u128) << 24
| (b'-' as u128) << 32
| (b'M' as u128) << 40
| (b'-' as u128) << 48
| (b'D' as u128) << 56
| (b'S' as u128) << 64
| (b'A' as u128) << 72
| (b'-' as u128) << 80
| (b'S' as u128) << 88
| (b'H' as u128) << 96
| (b'A' as u128) << 104
| (b'1' as u128) << 112;
pub(crate) const _9798_M_ECDSA_SHA: u128 = (b'9' as u128)
| (b'7' as u128) << 8
| (b'9' as u128) << 16
| (b'8' as u128) << 24
| (b'-' as u128) << 32
| (b'M' as u128) << 40
| (b'-' as u128) << 48
| (b'E' as u128) << 56
| (b'C' as u128) << 64
| (b'D' as u128) << 72
| (b'S' as u128) << 80
| (b'A' as u128) << 88
| (b'-' as u128) << 96
| (b'S' as u128) << 104
| (b'H' as u128) << 112
| (b'A' as u128) << 120;
pub(crate) const _9798_M_RSA_SHA1_: u128 = (b'9' as u128)
| (b'7' as u128) << 8
| (b'9' as u128) << 16
| (b'8' as u128) << 24
| (b'-' as u128) << 32
| (b'M' as u128) << 40
| (b'-' as u128) << 48
| (b'R' as u128) << 56
| (b'S' as u128) << 64
| (b'A' as u128) << 72
| (b'-' as u128) << 80
| (b'S' as u128) << 88
| (b'H' as u128) << 96
| (b'A' as u128) << 104
| (b'1' as u128) << 112
| (b'-' as u128) << 120;
pub(crate) const _9798_U_DSA_SHA1: u128 = (b'9' as u128)
| (b'7' as u128) << 8
| (b'9' as u128) << 16
| (b'8' as u128) << 24
| (b'-' as u128) << 32
| (b'U' as u128) << 40
| (b'-' as u128) << 48
| (b'D' as u128) << 56
| (b'S' as u128) << 64
| (b'A' as u128) << 72
| (b'-' as u128) << 80
| (b'S' as u128) << 88
| (b'H' as u128) << 96
| (b'A' as u128) << 104
| (b'1' as u128) << 112;
pub(crate) const _9798_U_ECDSA_SHA: u128 = (b'9' as u128)
| (b'7' as u128) << 8
| (b'9' as u128) << 16
| (b'8' as u128) << 24
| (b'-' as u128) << 32
| (b'U' as u128) << 40
| (b'-' as u128) << 48
| (b'E' as u128) << 56
| (b'C' as u128) << 64
| (b'D' as u128) << 72
| (b'S' as u128) << 80
| (b'A' as u128) << 88
| (b'-' as u128) << 96
| (b'S' as u128) << 104
| (b'H' as u128) << 112
| (b'A' as u128) << 120;
pub(crate) const _9798_U_RSA_SHA1_: u128 = (b'9' as u128)
| (b'7' as u128) << 8
| (b'9' as u128) << 16
| (b'8' as u128) << 24
| (b'-' as u128) << 32
| (b'U' as u128) << 40
| (b'-' as u128) << 48
| (b'R' as u128) << 56
| (b'S' as u128) << 64
| (b'A' as u128) << 72
| (b'-' as u128) << 80
| (b'S' as u128) << 88
| (b'H' as u128) << 96
| (b'A' as u128) << 104
| (b'1' as u128) << 112
| (b'-' as u128) << 120;
pub(crate) const ANONYMOUS: u128 = (b'A' as u128)
| (b'N' as u128) << 8
| (b'O' as u128) << 16
| (b'N' as u128) << 24
| (b'Y' as u128) << 32
| (b'M' as u128) << 40
| (b'O' as u128) << 48
| (b'U' as u128) << 56
| (b'S' as u128) << 64;
pub(crate) const CRAM_MD5: u128 = (b'C' as u128)
| (b'R' as u128) << 8
| (b'A' as u128) << 16
| (b'M' as u128) << 24
| (b'-' as u128) << 32
| (b'M' as u128) << 40
| (b'D' as u128) << 48
| (b'5' as u128) << 56;
pub(crate) const DIGEST_MD5: u128 = (b'D' as u128)
| (b'I' as u128) << 8
| (b'G' as u128) << 16
| (b'E' as u128) << 24
| (b'S' as u128) << 32
| (b'T' as u128) << 40
| (b'-' as u128) << 48
| (b'M' as u128) << 56
| (b'D' as u128) << 64
| (b'5' as u128) << 72;
pub(crate) const EAP_AES128: u128 = (b'E' as u128)
| (b'A' as u128) << 8
| (b'P' as u128) << 16
| (b'-' as u128) << 24
| (b'A' as u128) << 32
| (b'E' as u128) << 40
| (b'S' as u128) << 48
| (b'1' as u128) << 56
| (b'2' as u128) << 64
| (b'8' as u128) << 72;
pub(crate) const EAP_AES128_PLUS: u128 = (b'E' as u128)
| (b'A' as u128) << 8
| (b'P' as u128) << 16
| (b'-' as u128) << 24
| (b'A' as u128) << 32
| (b'E' as u128) << 40
| (b'S' as u128) << 48
| (b'1' as u128) << 56
| (b'2' as u128) << 64
| (b'8' as u128) << 72
| (b'-' as u128) << 80
| (b'P' as u128) << 88
| (b'L' as u128) << 96
| (b'U' as u128) << 104
| (b'S' as u128) << 112;
pub(crate) const ECDH_X25519_CHAL: u128 = (b'E' as u128)
| (b'C' as u128) << 8
| (b'D' as u128) << 16
| (b'H' as u128) << 24
| (b'-' as u128) << 32
| (b'X' as u128) << 40
| (b'2' as u128) << 48
| (b'5' as u128) << 56
| (b'5' as u128) << 64
| (b'1' as u128) << 72
| (b'9' as u128) << 80
| (b'-' as u128) << 88
| (b'C' as u128) << 96
| (b'H' as u128) << 104
| (b'A' as u128) << 112
| (b'L' as u128) << 120;
pub(crate) const ECDSA_NIST256P_C: u128 = (b'E' as u128)
| (b'C' as u128) << 8
| (b'D' as u128) << 16
| (b'S' as u128) << 24
| (b'A' as u128) << 32
| (b'-' as u128) << 40
| (b'N' as u128) << 48
| (b'I' as u128) << 56
| (b'S' as u128) << 64
| (b'T' as u128) << 72
| (b'2' as u128) << 80
| (b'5' as u128) << 88
| (b'6' as u128) << 96
| (b'P' as u128) << 104
| (b'-' as u128) << 112
| (b'C' as u128) << 120;
pub(crate) const EXTERNAL: u128 = (b'E' as u128)
| (b'X' as u128) << 8
| (b'T' as u128) << 16
| (b'E' as u128) << 24
| (b'R' as u128) << 32
| (b'N' as u128) << 40
| (b'A' as u128) << 48
| (b'L' as u128) << 56;
pub(crate) const GS2_KRB5: u128 = (b'G' as u128)
| (b'S' as u128) << 8
| (b'2' as u128) << 16
| (b'-' as u128) << 24
| (b'K' as u128) << 32
| (b'R' as u128) << 40
| (b'B' as u128) << 48
| (b'5' as u128) << 56;
pub(crate) const GS2_KRB5_PLUS: u128 = (b'G' as u128)
| (b'S' as u128) << 8
| (b'2' as u128) << 16
| (b'-' as u128) << 24
| (b'K' as u128) << 32
| (b'R' as u128) << 40
| (b'B' as u128) << 48
| (b'5' as u128) << 56
| (b'-' as u128) << 64
| (b'P' as u128) << 72
| (b'L' as u128) << 80
| (b'U' as u128) << 88
| (b'S' as u128) << 96;
pub(crate) const GSS_SPNEGO: u128 = (b'G' as u128)
| (b'S' as u128) << 8
| (b'S' as u128) << 16
| (b'-' as u128) << 24
| (b'S' as u128) << 32
| (b'P' as u128) << 40
| (b'N' as u128) << 48
| (b'E' as u128) << 56
| (b'G' as u128) << 64
| (b'O' as u128) << 72;
pub(crate) const GSSAPI: u128 = (b'G' as u128)
| (b'S' as u128) << 8
| (b'S' as u128) << 16
| (b'A' as u128) << 24
| (b'P' as u128) << 32
| (b'I' as u128) << 40;
pub(crate) const KERBEROS_V4: u128 = (b'K' as u128)
| (b'E' as u128) << 8
| (b'R' as u128) << 16
| (b'B' as u128) << 24
| (b'E' as u128) << 32
| (b'R' as u128) << 40
| (b'O' as u128) << 48
| (b'S' as u128) << 56
| (b'_' as u128) << 64
| (b'V' as u128) << 72
| (b'4' as u128) << 80;
pub(crate) const KERBEROS_V5: u128 = (b'K' as u128)
| (b'E' as u128) << 8
| (b'R' as u128) << 16
| (b'B' as u128) << 24
| (b'E' as u128) << 32
| (b'R' as u128) << 40
| (b'O' as u128) << 48
| (b'S' as u128) << 56
| (b'_' as u128) << 64
| (b'V' as u128) << 72
| (b'5' as u128) << 80;
pub(crate) const LOGIN: u128 = (b'L' as u128)
| (b'O' as u128) << 8
| (b'G' as u128) << 16
| (b'I' as u128) << 24
| (b'N' as u128) << 32;
pub(crate) const NMAS_SAMBA_AUTH: u128 = (b'N' as u128)
| (b'M' as u128) << 8
| (b'A' as u128) << 16
| (b'S' as u128) << 24
| (b'-' as u128) << 32
| (b'S' as u128) << 40
| (b'A' as u128) << 48
| (b'M' as u128) << 56
| (b'B' as u128) << 64
| (b'A' as u128) << 72
| (b'-' as u128) << 80
| (b'A' as u128) << 88
| (b'U' as u128) << 96
| (b'T' as u128) << 104
| (b'H' as u128) << 112;
pub(crate) const NMAS_AUTHEN: u128 = (b'N' as u128)
| (b'M' as u128) << 8
| (b'A' as u128) << 16
| (b'S' as u128) << 24
| (b'_' as u128) << 32
| (b'A' as u128) << 40
| (b'U' as u128) << 48
| (b'T' as u128) << 56
| (b'H' as u128) << 64
| (b'E' as u128) << 72
| (b'N' as u128) << 80;
pub(crate) const NMAS_LOGIN: u128 = (b'N' as u128)
| (b'M' as u128) << 8
| (b'A' as u128) << 16
| (b'S' as u128) << 24
| (b'_' as u128) << 32
| (b'L' as u128) << 40
| (b'O' as u128) << 48
| (b'G' as u128) << 56
| (b'I' as u128) << 64
| (b'N' as u128) << 72;
pub(crate) const NTLM: u128 =
(b'N' as u128) | (b'T' as u128) << 8 | (b'L' as u128) << 16 | (b'M' as u128) << 24;
pub(crate) const OAUTH10A: u128 = (b'O' as u128)
| (b'A' as u128) << 8
| (b'U' as u128) << 16
| (b'T' as u128) << 24
| (b'H' as u128) << 32
| (b'1' as u128) << 40
| (b'0' as u128) << 48
| (b'A' as u128) << 56;
pub(crate) const OAUTHBEARER: u128 = (b'O' as u128)
| (b'A' as u128) << 8
| (b'U' as u128) << 16
| (b'T' as u128) << 24
| (b'H' as u128) << 32
| (b'B' as u128) << 40
| (b'E' as u128) << 48
| (b'A' as u128) << 56
| (b'R' as u128) << 64
| (b'E' as u128) << 72
| (b'R' as u128) << 80;
pub(crate) const OPENID20: u128 = (b'O' as u128)
| (b'P' as u128) << 8
| (b'E' as u128) << 16
| (b'N' as u128) << 24
| (b'I' as u128) << 32
| (b'D' as u128) << 40
| (b'2' as u128) << 48
| (b'0' as u128) << 56;
pub(crate) const OTP: u128 = (b'O' as u128) | (b'T' as u128) << 8 | (b'P' as u128) << 16;
pub(crate) const PLAIN: u128 = (b'P' as u128)
| (b'L' as u128) << 8
| (b'A' as u128) << 16
| (b'I' as u128) << 24
| (b'N' as u128) << 32;
pub(crate) const SAML20: u128 = (b'S' as u128)
| (b'A' as u128) << 8
| (b'M' as u128) << 16
| (b'L' as u128) << 24
| (b'2' as u128) << 32
| (b'0' as u128) << 40;
pub(crate) const SCRAM_SHA_1: u128 = (b'S' as u128)
| (b'C' as u128) << 8
| (b'R' as u128) << 16
| (b'A' as u128) << 24
| (b'M' as u128) << 32
| (b'-' as u128) << 40
| (b'S' as u128) << 48
| (b'H' as u128) << 56
| (b'A' as u128) << 64
| (b'-' as u128) << 72
| (b'1' as u128) << 80;
pub(crate) const SCRAM_SHA_1_PLUS: u128 = (b'S' as u128)
| (b'C' as u128) << 8
| (b'R' as u128) << 16
| (b'A' as u128) << 24
| (b'M' as u128) << 32
| (b'-' as u128) << 40
| (b'S' as u128) << 48
| (b'H' as u128) << 56
| (b'A' as u128) << 64
| (b'-' as u128) << 72
| (b'1' as u128) << 80
| (b'-' as u128) << 88
| (b'P' as u128) << 96
| (b'L' as u128) << 104
| (b'U' as u128) << 112
| (b'S' as u128) << 120;
pub(crate) const SCRAM_SHA_256: u128 = (b'S' as u128)
| (b'C' as u128) << 8
| (b'R' as u128) << 16
| (b'A' as u128) << 24
| (b'M' as u128) << 32
| (b'-' as u128) << 40
| (b'S' as u128) << 48
| (b'H' as u128) << 56
| (b'A' as u128) << 64
| (b'-' as u128) << 72
| (b'2' as u128) << 80
| (b'5' as u128) << 88
| (b'6' as u128) << 96;
pub(crate) const SCRAM_SHA_256_PL: u128 = (b'S' as u128)
| (b'C' as u128) << 8
| (b'R' as u128) << 16
| (b'A' as u128) << 24
| (b'M' as u128) << 32
| (b'-' as u128) << 40
| (b'S' as u128) << 48
| (b'H' as u128) << 56
| (b'A' as u128) << 64
| (b'-' as u128) << 72
| (b'2' as u128) << 80
| (b'5' as u128) << 88
| (b'6' as u128) << 96
| (b'-' as u128) << 104
| (b'P' as u128) << 112
| (b'L' as u128) << 120;
pub(crate) const SECURID: u128 = (b'S' as u128)
| (b'E' as u128) << 8
| (b'C' as u128) << 16
| (b'U' as u128) << 24
| (b'R' as u128) << 32
| (b'I' as u128) << 40
| (b'D' as u128) << 48;
pub(crate) const SKEY: u128 =
(b'S' as u128) | (b'K' as u128) << 8 | (b'E' as u128) << 16 | (b'Y' as u128) << 24;
pub(crate) const SPNEGO: u128 = (b'S' as u128)
| (b'P' as u128) << 8
| (b'N' as u128) << 16
| (b'E' as u128) << 24
| (b'G' as u128) << 32
| (b'O' as u128) << 40;
pub(crate) const SPNEGO_PLUS: u128 = (b'S' as u128)
| (b'P' as u128) << 8
| (b'N' as u128) << 16
| (b'E' as u128) << 24
| (b'G' as u128) << 32
| (b'O' as u128) << 40
| (b'-' as u128) << 48
| (b'P' as u128) << 56
| (b'L' as u128) << 64
| (b'U' as u128) << 72
| (b'S' as u128) << 80;
pub(crate) const SXOVER_PLUS: u128 = (b'S' as u128)
| (b'X' as u128) << 8
| (b'O' as u128) << 16
| (b'V' as u128) << 24
| (b'E' as u128) << 32
| (b'R' as u128) << 40
| (b'-' as u128) << 48
| (b'P' as u128) << 56
| (b'L' as u128) << 64
| (b'U' as u128) << 72
| (b'S' as u128) << 80;
pub(crate) const XOAUTH: u128 = (b'X' as u128)
| (b'O' as u128) << 8
| (b'A' as u128) << 16
| (b'U' as u128) << 24
| (b'T' as u128) << 32
| (b'H' as u128) << 40;
pub(crate) const XOAUTH2: u128 = (b'X' as u128)
| (b'O' as u128) << 8
| (b'A' as u128) << 16
| (b'U' as u128) << 24
| (b'T' as u128) << 32
| (b'H' as u128) << 40
| (b'2' as u128) << 48;
/* /*
* Adapted from Daniel Lemire's source: * Adapted from Daniel Lemire's source:

View file

@ -24,201 +24,46 @@
use std::fmt::Display; use std::fmt::Display;
use crate::{EhloResponse, Response}; use crate::{EhloResponse, Response};
use crate::tokens::define_tokens_128;
pub mod generate; pub mod generate;
pub mod parser; pub mod parser;
pub(crate) const _8BITMIME: u128 = (b'8' as u128) define_tokens_128! {
| (b'B' as u128) << 8 _8BITMIME = "8BITMIME",
| (b'I' as u128) << 16 ATRN,
| (b'T' as u128) << 24 AUTH,
| (b'M' as u128) << 32 BINARYMIME,
| (b'I' as u128) << 40 BURL,
| (b'M' as u128) << 48 CHECKPOINT,
| (b'E' as u128) << 56; CHUNKING,
pub(crate) const ATRN: u128 = CONNEG,
(b'A' as u128) | (b'T' as u128) << 8 | (b'R' as u128) << 16 | (b'N' as u128) << 24; CONPERM,
pub(crate) const AUTH: u128 = DELIVERBY,
(b'A' as u128) | (b'U' as u128) << 8 | (b'T' as u128) << 16 | (b'H' as u128) << 24; DSN,
pub(crate) const BINARYMIME: u128 = (b'B' as u128) ENHANCEDSTATUSCO,
| (b'I' as u128) << 8 ETRN,
| (b'N' as u128) << 16 EXPN,
| (b'A' as u128) << 24 VRFY,
| (b'R' as u128) << 32 FUTURERELEASE,
| (b'Y' as u128) << 40 HELP,
| (b'M' as u128) << 48 MT_PRIORITY = "MT-PRIORITY",
| (b'I' as u128) << 56 MTRK,
| (b'M' as u128) << 64 NO_SOLICITING = "NO-SOLICITING",
| (b'E' as u128) << 72; ONEX,
pub(crate) const BURL: u128 = PIPELINING,
(b'B' as u128) | (b'U' as u128) << 8 | (b'R' as u128) << 16 | (b'L' as u128) << 24; REQUIRETLS,
pub(crate) const CHECKPOINT: u128 = (b'C' as u128) RRVS,
| (b'H' as u128) << 8 SIZE,
| (b'E' as u128) << 16 SMTPUTF8,
| (b'C' as u128) << 24 STARTTLS,
| (b'K' as u128) << 32 VERB,
| (b'P' as u128) << 40
| (b'O' as u128) << 48
| (b'I' as u128) << 56
| (b'N' as u128) << 64
| (b'T' as u128) << 72;
pub(crate) const CHUNKING: u128 = (b'C' as u128)
| (b'H' as u128) << 8
| (b'U' as u128) << 16
| (b'N' as u128) << 24
| (b'K' as u128) << 32
| (b'I' as u128) << 40
| (b'N' as u128) << 48
| (b'G' as u128) << 56;
pub(crate) const CONNEG: u128 = (b'C' as u128)
| (b'O' as u128) << 8
| (b'N' as u128) << 16
| (b'N' as u128) << 24
| (b'E' as u128) << 32
| (b'G' as u128) << 40;
pub(crate) const CONPERM: u128 = (b'C' as u128)
| (b'O' as u128) << 8
| (b'N' as u128) << 16
| (b'P' as u128) << 24
| (b'E' as u128) << 32
| (b'R' as u128) << 40
| (b'M' as u128) << 48;
pub(crate) const DELIVERBY: u128 = (b'D' as u128)
| (b'E' as u128) << 8
| (b'L' as u128) << 16
| (b'I' as u128) << 24
| (b'V' as u128) << 32
| (b'E' as u128) << 40
| (b'R' as u128) << 48
| (b'B' as u128) << 56
| (b'Y' as u128) << 64;
pub(crate) const DSN: u128 = (b'D' as u128) | (b'S' as u128) << 8 | (b'N' as u128) << 16;
pub(crate) const ENHANCEDSTATUSCO: u128 = (b'E' as u128)
| (b'N' as u128) << 8
| (b'H' as u128) << 16
| (b'A' as u128) << 24
| (b'N' as u128) << 32
| (b'C' as u128) << 40
| (b'E' as u128) << 48
| (b'D' as u128) << 56
| (b'S' as u128) << 64
| (b'T' as u128) << 72
| (b'A' as u128) << 80
| (b'T' as u128) << 88
| (b'U' as u128) << 96
| (b'S' as u128) << 104
| (b'C' as u128) << 112
| (b'O' as u128) << 120;
pub(crate) const ETRN: u128 =
(b'E' as u128) | (b'T' as u128) << 8 | (b'R' as u128) << 16 | (b'N' as u128) << 24;
pub(crate) const EXPN: u128 =
(b'E' as u128) | (b'X' as u128) << 8 | (b'P' as u128) << 16 | (b'N' as u128) << 24;
pub(crate) const VRFY: u128 =
(b'V' as u128) | (b'R' as u128) << 8 | (b'F' as u128) << 16 | (b'Y' as u128) << 24;
pub(crate) const FUTURERELEASE: u128 = (b'F' as u128)
| (b'U' as u128) << 8
| (b'T' as u128) << 16
| (b'U' as u128) << 24
| (b'R' as u128) << 32
| (b'E' as u128) << 40
| (b'R' as u128) << 48
| (b'E' as u128) << 56
| (b'L' as u128) << 64
| (b'E' as u128) << 72
| (b'A' as u128) << 80
| (b'S' as u128) << 88
| (b'E' as u128) << 96;
pub(crate) const HELP: u128 =
(b'H' as u128) | (b'E' as u128) << 8 | (b'L' as u128) << 16 | (b'P' as u128) << 24;
pub(crate) const MT_PRIORITY: u128 = (b'M' as u128)
| (b'T' as u128) << 8
| (b'-' as u128) << 16
| (b'P' as u128) << 24
| (b'R' as u128) << 32
| (b'I' as u128) << 40
| (b'O' as u128) << 48
| (b'R' as u128) << 56
| (b'I' as u128) << 64
| (b'T' as u128) << 72
| (b'Y' as u128) << 80;
pub(crate) const MTRK: u128 =
(b'M' as u128) | (b'T' as u128) << 8 | (b'R' as u128) << 16 | (b'K' as u128) << 24;
pub(crate) const NO_SOLICITING: u128 = (b'N' as u128)
| (b'O' as u128) << 8
| (b'-' as u128) << 16
| (b'S' as u128) << 24
| (b'O' as u128) << 32
| (b'L' as u128) << 40
| (b'I' as u128) << 48
| (b'C' as u128) << 56
| (b'I' as u128) << 64
| (b'T' as u128) << 72
| (b'I' as u128) << 80
| (b'N' as u128) << 88
| (b'G' as u128) << 96;
pub(crate) const ONEX: u128 =
(b'O' as u128) | (b'N' as u128) << 8 | (b'E' as u128) << 16 | (b'X' as u128) << 24;
pub(crate) const PIPELINING: u128 = (b'P' as u128)
| (b'I' as u128) << 8
| (b'P' as u128) << 16
| (b'E' as u128) << 24
| (b'L' as u128) << 32
| (b'I' as u128) << 40
| (b'N' as u128) << 48
| (b'I' as u128) << 56
| (b'N' as u128) << 64
| (b'G' as u128) << 72;
pub(crate) const REQUIRETLS: u128 = (b'R' as u128)
| (b'E' as u128) << 8
| (b'Q' as u128) << 16
| (b'U' as u128) << 24
| (b'I' as u128) << 32
| (b'R' as u128) << 40
| (b'E' as u128) << 48
| (b'T' as u128) << 56
| (b'L' as u128) << 64
| (b'S' as u128) << 72;
pub(crate) const RRVS: u128 =
(b'R' as u128) | (b'R' as u128) << 8 | (b'V' as u128) << 16 | (b'S' as u128) << 24;
pub(crate) const SIZE: u128 =
(b'S' as u128) | (b'I' as u128) << 8 | (b'Z' as u128) << 16 | (b'E' as u128) << 24;
pub(crate) const SMTPUTF8: u128 = (b'S' as u128)
| (b'M' as u128) << 8
| (b'T' as u128) << 16
| (b'P' as u128) << 24
| (b'U' as u128) << 32
| (b'T' as u128) << 40
| (b'F' as u128) << 48
| (b'8' as u128) << 56;
pub(crate) const STARTTLS: u128 = (b'S' as u128)
| (b'T' as u128) << 8
| (b'A' as u128) << 16
| (b'R' as u128) << 24
| (b'T' as u128) << 32
| (b'T' as u128) << 40
| (b'L' as u128) << 48
| (b'S' as u128) << 56;
pub(crate) const VERB: u128 =
(b'V' as u128) | (b'E' as u128) << 8 | (b'R' as u128) << 16 | (b'B' as u128) << 24;
// Priorities // Priorities
pub(crate) const MIXER: u128 = (b'M' as u128) MIXER,
| (b'I' as u128) << 8 STANAG4406,
| (b'X' as u128) << 16 NSEP,
| (b'E' as u128) << 24 }
| (b'R' as u128) << 32;
pub(crate) const STANAG4406: u128 = (b'S' as u128)
| (b'T' as u128) << 8
| (b'A' as u128) << 16
| (b'N' as u128) << 24
| (b'A' as u128) << 32
| (b'G' as u128) << 40
| (b'4' as u128) << 48
| (b'4' as u128) << 56
| (b'0' as u128) << 64
| (b'6' as u128) << 72;
pub(crate) const NSEP: u128 =
(b'N' as u128) | (b'S' as u128) << 8 | (b'E' as u128) << 16 | (b'P' as u128) << 24;
impl<T: Display> EhloResponse<T> { impl<T: Display> EhloResponse<T> {
/// Returns the hostname of the SMTP server. /// Returns the hostname of the SMTP server.

40
src/tokens.rs Normal file
View file

@ -0,0 +1,40 @@
const fn str_to_array<const N: usize>(s: &str) -> [u8; N] {
let s = s.as_bytes();
let mut arr = [0; N];
let mut i = 0;
while i < s.len() {
arr[i] = s[i];
i += 1;
}
arr
}
pub(crate) const fn token64(s: &str) -> u64 {
u64::from_le_bytes(str_to_array(s))
}
pub(crate) const fn token128(s: &str) -> u128 {
u128::from_le_bytes(str_to_array(s))
}
macro_rules! first {
($head:expr$(, $tail:expr)*) => {
$head
};
}
macro_rules! define_tokens_64 {
($($a:ident $(= $b:expr)?,)*) => {
$(pub(crate) const $a: u64 = $crate::tokens::token64($crate::tokens::first!($($b,)* stringify!($a)));)*
};
}
macro_rules! define_tokens_128 {
($($a:ident $(= $b:expr)?,)*) => {
$(pub(crate) const $a: u128 = $crate::tokens::token128($crate::tokens::first!($($b,)* stringify!($a)));)*
};
}
pub(crate) use first;
pub(crate) use define_tokens_64;
pub(crate) use define_tokens_128;