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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
mod codec;
mod command;
mod commands;
pub mod extension;
mod extensions;
mod host;
mod path;
mod reply;
mod state;

pub use self::codec::*;
pub use self::command::*;
pub use self::extensions::*;
pub use self::host::*;
pub use self::path::*;
pub use self::reply::*;
pub use self::state::*;
use crate::parser::Parser;
use std::fmt;

/// Represents the instructions for the client side of the stream.
pub enum CodecControl {
    /// Write an SMTP response
    Response(Vec<u8>),
    /// Switch parser
    Parser(Box<dyn Parser + Sync + Send>),
    /// Start TLS encryption
    StartTls,
    /// Shut the stream down
    Shutdown,
}

impl fmt::Debug for CodecControl {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        #[derive(Debug)]
        enum TB<'a> {
            T(&'a str),
            B(&'a [u8]),
        }
        fn tb(inp: &[u8]) -> TB {
            if let Ok(text) = std::str::from_utf8(inp) {
                TB::T(text)
            } else {
                TB::B(inp)
            }
        }
        match self {
            CodecControl::Parser(p) => f.debug_tuple("Parser").field(&p).finish(),
            CodecControl::Response(r) => f.debug_tuple("Response").field(&tb(r)).finish(),
            CodecControl::StartTls => f.debug_tuple("StartTls").finish(),
            CodecControl::Shutdown => f.debug_tuple("Shutdown").finish(),
        }
    }
}