From 5ffcd1af5eaeecdd04975cb2cbbf4c8edb0b1615 Mon Sep 17 00:00:00 2001 From: Nathalie Cai Date: Mon, 25 Mar 2024 22:32:41 +0100 Subject: [PATCH] add serializer for response content field (#157) * add serializer for response content field * update formatting --------- Co-authored-by: ncai --- crates/rfc5321/src/client_types.rs | 39 ++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/crates/rfc5321/src/client_types.rs b/crates/rfc5321/src/client_types.rs index 8cef2c7b..e5e03f45 100644 --- a/crates/rfc5321/src/client_types.rs +++ b/crates/rfc5321/src/client_types.rs @@ -145,6 +145,7 @@ impl SmtpClientTimeouts { pub struct Response { pub code: u16, pub enhanced_code: Option, + #[serde(serialize_with = "as_single_line")] pub content: String, pub command: Option, } @@ -157,13 +158,7 @@ impl Response { line.push_str(&format!("{}.{}.{} ", enh.class, enh.subject, enh.detail)); } - for c in self.content.chars() { - match c { - '\r' => line.push_str("\\r"), - '\n' => line.push_str("\\n"), - c => line.push(c), - } - } + line.push_str(&remove_line_break(&self.content)); line } @@ -183,3 +178,33 @@ pub struct EnhancedStatusCode { pub subject: u16, pub detail: u16, } + +fn remove_line_break(line: &String) -> String { + let mut new_line = String::new(); + let mut cr_to_space = false; + + for c in line.chars() { + match c { + '\r' => { + new_line.push_str(" "); + cr_to_space = true; + } + '\n' => { + if !cr_to_space { + new_line.push_str(" "); + } else { + cr_to_space = false; + } + } + c => new_line.push(c), + } + } + new_line +} + +fn as_single_line(content: &String, serializer: S) -> Result +where + S: serde::Serializer, +{ + serializer.serialize_str(&remove_line_break(content)) +}