add serializer for response content field (#157)

* add serializer for response content field

* update formatting

---------

Co-authored-by: ncai <ncai@chapsvision.com>
This commit is contained in:
Nathalie Cai
2024-03-25 14:32:41 -07:00
committed by GitHub
co-authored by ncai
parent f54922b9f0
commit 5ffcd1af5e
+32 -7
View File
@@ -145,6 +145,7 @@ impl SmtpClientTimeouts {
pub struct Response {
pub code: u16,
pub enhanced_code: Option<EnhancedStatusCode>,
#[serde(serialize_with = "as_single_line")]
pub content: String,
pub command: Option<String>,
}
@@ -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<S>(content: &String, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&remove_line_break(content))
}