Skip to content

Commit

Permalink
Added rkyv support
Browse files Browse the repository at this point in the history
  • Loading branch information
mdecimus committed Mar 1, 2025
1 parent f4d7270 commit d767ccc
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 111 deletions.
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ license = "Apache-2.0 OR MIT"
keywords = ["smtp", "lmtp", "protocol", "parser"]
categories = ["email", "parser-implementations"]
readme = "README.md"
version = "0.1.5"
version = "0.1.6"
edition = "2021"

[dependencies]
rkyv = { version = "0.8.10", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }

[dev-dependencies]

[features]
default = []
serde_support = ["serde"]
rkyv = ["dep:rkyv"]
serde = ["dep:serde"]
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ at your option.

## Copyright

Copyright (C) 2020-2024, Stalwart Labs Ltd.
Copyright (C) 2020, Stalwart Labs LLC

10 changes: 3 additions & 7 deletions fuzz/fuzz_targets/smtp_proto.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
/*
* Copyright (c) 2020-2024, Stalwart Labs Ltd.
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
* option. This file may not be copied, modified, or distributed
* except according to those terms.
*/
* SPDX-License-Identifier: Apache-2.0 OR MIT
*/

#![no_main]
use libfuzzer_sys::fuzz_target;
Expand Down
99 changes: 50 additions & 49 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,61 +1,23 @@
/*
* Copyright (c) 2020-2024, Stalwart Labs Ltd.
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
* option. This file may not be copied, modified, or distributed
* except according to those terms.
*/

//! # smtp-proto
//!
//! [![crates.io](https://img.shields.io/crates/v/smtp-proto)](https://crates.io/crates/smtp-proto)
//! [![build](https://github.com/stalwartlabs/sieve/actions/workflows/rust.yml/badge.svg)](https://github.com/stalwartlabs/sieve/actions/workflows/rust.yml)
//! [![docs.rs](https://img.shields.io/docsrs/smtp-proto)](https://docs.rs/smtp-proto)
//! [![License: AGPL v3](https://img.shields.io/badge/License-AGPL_v3-blue.svg)](https://www.gnu.org/licenses/agpl-3.0)
//!
//! _smtp-proto_ is a fast SMTP/LMTP parser for Rust that supports all [registered SMTP service extensions](https://www.iana.org/assignments/mail-parameters/mail-parameters.xhtml).
//! The library is part of Stalwart SMTP and LMTP servers. It is not yet documented so if you need help using the library please start a discussion.
//!
//!
//! ## Testing & Fuzzing
//!
//! To run the testsuite:
//!
//! ```bash
//! $ cargo test
//! ```
//!
//! To fuzz the library with `cargo-fuzz`:
//!
//! ```bash
//! $ cargo +nightly fuzz run smtp_proto
//! ```
//!
//! ## License
//!
//! Licensed under the terms of the [GNU Affero General Public License](https://www.gnu.org/licenses/agpl-3.0.en.html) as published by
//! the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//! See [LICENSE](LICENSE) for more details.
//!
//! You can be released from the requirements of the AGPLv3 license by purchasing
//! a commercial license. Please contact licensing@stalw.art for more details.
//!
//! ## Copyright
//!
//! Copyright (C) 2020-2023, Stalwart Labs Ltd.
* SPDX-License-Identifier: Apache-2.0 OR MIT
*/

#![doc = include_str!("../README.md")]
#![deny(rust_2018_idioms)]
use std::fmt::Display;

pub mod request;
pub mod response;
mod tokens;

#[cfg(feature = "serde_support")]
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)
)]
pub enum Request<T> {
Ehlo { host: T },
Lhlo { host: T },
Expand All @@ -78,6 +40,11 @@ pub enum Request<T> {
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)
)]
pub struct MailFrom<T> {
pub address: T,
pub flags: u64,
Expand All @@ -94,6 +61,11 @@ pub struct MailFrom<T> {
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)
)]
pub struct RcptTo<T> {
pub address: T,
pub orcpt: Option<T>,
Expand Down Expand Up @@ -122,6 +94,11 @@ pub const RCPT_RRVS_REJECT: u64 = 1 << 5;
pub const RCPT_RRVS_CONTINUE: u64 = 1 << 6;

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)
)]
pub struct Mtrk<T> {
pub certifier: T,
pub timeout: u64,
Expand Down Expand Up @@ -200,6 +177,11 @@ pub const EXT_EXPN: u32 = 1 << 26;
pub const EXT_VRFY: u32 = 1 << 27;

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)
)]
pub enum MtPriority {
#[default]
Mixer,
Expand All @@ -208,6 +190,11 @@ pub enum MtPriority {
}

#[derive(Debug, Default, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)
)]
pub struct EhloResponse<T: Display> {
pub hostname: T,
pub capabilities: u32,
Expand All @@ -222,14 +209,23 @@ pub struct EhloResponse<T: Display> {
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)
)]
pub struct Response<T: Display> {
pub code: u16,
pub esc: [u8; 3],
pub message: T,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)
)]
pub enum Severity {
PositiveCompletion = 2,
PositiveIntermediate = 3,
Expand All @@ -239,6 +235,11 @@ pub enum Severity {
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)
)]
pub enum Category {
Syntax = 0,
Information = 1,
Expand Down
10 changes: 3 additions & 7 deletions src/request/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
/*
* Copyright (c) 2020-2024, Stalwart Labs Ltd.
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
* option. This file may not be copied, modified, or distributed
* except according to those terms.
*/
* SPDX-License-Identifier: Apache-2.0 OR MIT
*/

use crate::tokens::{define_tokens_128, define_tokens_64};

Expand Down
10 changes: 3 additions & 7 deletions src/request/parser.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
/*
* Copyright (c) 2020-2024, Stalwart Labs Ltd.
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
* option. This file may not be copied, modified, or distributed
* except according to those terms.
*/
* SPDX-License-Identifier: Apache-2.0 OR MIT
*/

use std::slice::Iter;

Expand Down
10 changes: 3 additions & 7 deletions src/request/receiver.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
/*
* Copyright (c) 2020-2024, Stalwart Labs Ltd.
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
* option. This file may not be copied, modified, or distributed
* except according to those terms.
*/
* SPDX-License-Identifier: Apache-2.0 OR MIT
*/

use std::slice::Iter;

Expand Down
10 changes: 3 additions & 7 deletions src/response/generate.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
/*
* Copyright (c) 2020-2024, Stalwart Labs Ltd.
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
* option. This file may not be copied, modified, or distributed
* except according to those terms.
*/
* SPDX-License-Identifier: Apache-2.0 OR MIT
*/

use std::{
fmt::Display,
Expand Down
10 changes: 3 additions & 7 deletions src/response/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
/*
* Copyright (c) 2020-2024, Stalwart Labs Ltd.
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
* option. This file may not be copied, modified, or distributed
* except according to those terms.
*/
* SPDX-License-Identifier: Apache-2.0 OR MIT
*/

use std::fmt::Display;

Expand Down
16 changes: 6 additions & 10 deletions src/response/parser.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
/*
* Copyright (c) 2020-2024, Stalwart Labs Ltd.
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
* option. This file may not be copied, modified, or distributed
* except according to those terms.
*/
* SPDX-License-Identifier: Apache-2.0 OR MIT
*/

use std::slice::Iter;

Expand Down Expand Up @@ -244,9 +240,9 @@ impl EhloResponse<String> {
}
DSN => EXT_DSN,
ENHANCEDSTATUSCO
if parser.stop_char.to_ascii_uppercase() == b'D'
&& parser.read_char()?.to_ascii_uppercase() == b'E'
&& parser.read_char()?.to_ascii_uppercase() == b'S' =>
if parser.stop_char.eq_ignore_ascii_case(&b'D')
&& parser.read_char()?.eq_ignore_ascii_case(&b'E')
&& parser.read_char()?.eq_ignore_ascii_case(&b'S') =>
{
EXT_ENHANCED_STATUS_CODES
}
Expand Down
10 changes: 3 additions & 7 deletions src/tokens.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
/*
* Copyright (c) 2020-2024, Stalwart Labs Ltd.
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
* option. This file may not be copied, modified, or distributed
* except according to those terms.
*/
* SPDX-License-Identifier: Apache-2.0 OR MIT
*/

const fn str_to_array<const N: usize>(s: &str) -> [u8; N] {
let s = s.as_bytes();
Expand Down

0 comments on commit d767ccc

Please sign in to comment.