From 7cac2a2903a8680ebe4f4564f1b5fd249be39530 Mon Sep 17 00:00:00 2001 From: Damian Poddebniak Date: Sat, 22 May 2021 22:56:45 +0200 Subject: [PATCH] Impl `EhloOkResp::{new, serialize}` --- src/types.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/types.rs b/src/types.rs index 9680392..ba3aecb 100644 --- a/src/types.rs +++ b/src/types.rs @@ -282,6 +282,45 @@ pub struct EhloOkResp { pub lines: Vec, } +impl EhloOkResp { + pub fn new(domain: D, greet: Option, capabilities: Vec) -> EhloOkResp + where + D: Into, + G: Into, + { + EhloOkResp { + domain: domain.into(), + greet: greet.map(|inner| inner.into()), + lines: capabilities, + } + } + + pub fn serialize(&self, writer: &mut impl Write) -> std::io::Result<()> { + let greet = match self.greet { + Some(ref greet) => format!(" {}", greet), + None => "".to_string(), + }; + + if let Some((tail, head)) = self.lines.split_last() { + writer.write_all(format!("250-{}{}\r\n", self.domain, greet).as_bytes())?; + + for capability in head { + writer.write_all(b"250-")?; + capability.serialize(writer)?; + writer.write_all(b"\r\n")?; + } + + writer.write_all(b"250 ")?; + tail.serialize(writer)?; + writer.write_all(b"\r\n") + } else { + writer.write_all(format!("250 {}{}\r\n", self.domain, greet).as_bytes()) + } + } +} + +// ------------------------------------------------------------------------------------------------- + #[derive(Debug, Clone, PartialEq, Eq)] pub enum Capability { // Send as mail [RFC821]