Skip to main content

mail_send/smtp/
mod.rs

1/*
2 * SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
3 *
4 * SPDX-License-Identifier: Apache-2.0 OR MIT
5 */
6
7use smtp_proto::{Response, Severity};
8
9pub mod auth;
10pub mod builder;
11pub mod client;
12pub mod ehlo;
13pub mod envelope;
14pub mod message;
15pub mod tls;
16
17impl From<auth::Error> for crate::Error {
18    fn from(err: auth::Error) -> Self {
19        crate::Error::Auth(err)
20    }
21}
22
23pub trait AssertReply: Sized {
24    fn is_positive_completion(&self) -> bool;
25    fn assert_positive_completion(self) -> crate::Result<()>;
26    fn into_positive_completion(self) -> crate::Result<Self>;
27    fn assert_severity(self, severity: Severity) -> crate::Result<()>;
28    fn assert_code(self, code: u16) -> crate::Result<()>;
29}
30
31impl AssertReply for Response<String> {
32    /// Returns `true` if the reply is a positive completion.
33    #[inline(always)]
34    fn is_positive_completion(&self) -> bool {
35        (200..=299).contains(&self.code)
36    }
37
38    /// Returns Ok if the reply has the specified severity.
39    #[inline(always)]
40    fn assert_severity(self, severity: Severity) -> crate::Result<()> {
41        if self.severity() == severity {
42            Ok(())
43        } else {
44            Err(crate::Error::UnexpectedReply(self))
45        }
46    }
47
48    /// Returns Ok if the reply returned a 2xx code.
49    #[inline(always)]
50    fn assert_positive_completion(self) -> crate::Result<()> {
51        self.into_positive_completion().map(|_| ())
52    }
53
54    /// Returns the reply if it returned a 2xx code.
55    #[inline(always)]
56    fn into_positive_completion(self) -> crate::Result<Self> {
57        if self.is_positive_completion() {
58            Ok(self)
59        } else {
60            Err(crate::Error::UnexpectedReply(self))
61        }
62    }
63
64    /// Returns Ok if the reply has the specified status code.
65    #[inline(always)]
66    fn assert_code(self, code: u16) -> crate::Result<()> {
67        if self.code() == code {
68            Ok(())
69        } else {
70            Err(crate::Error::UnexpectedReply(self))
71        }
72    }
73}