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 assert_severity(self, severity: Severity) -> crate::Result<()>;
27    fn assert_code(self, code: u16) -> crate::Result<()>;
28}
29
30impl AssertReply for Response<String> {
31    /// Returns `true` if the reply is a positive completion.
32    #[inline(always)]
33    fn is_positive_completion(&self) -> bool {
34        (200..=299).contains(&self.code)
35    }
36
37    /// Returns Ok if the reply has the specified severity.
38    #[inline(always)]
39    fn assert_severity(self, severity: Severity) -> crate::Result<()> {
40        if self.severity() == severity {
41            Ok(())
42        } else {
43            Err(crate::Error::UnexpectedReply(self))
44        }
45    }
46
47    /// Returns Ok if the reply returned a 2xx code.
48    #[inline(always)]
49    fn assert_positive_completion(self) -> crate::Result<()> {
50        if (200..=299).contains(&self.code) {
51            Ok(())
52        } else {
53            Err(crate::Error::UnexpectedReply(self))
54        }
55    }
56
57    /// Returns Ok if the reply has the specified status code.
58    #[inline(always)]
59    fn assert_code(self, code: u16) -> crate::Result<()> {
60        if self.code() == code {
61            Ok(())
62        } else {
63            Err(crate::Error::UnexpectedReply(self))
64        }
65    }
66}