use crate::utils::Elide;
#[derive(Debug, Clone)]
pub struct MessageSummary {
id: String,
date: Option<String>,
subject: Option<String>,
}
impl MessageSummary {
pub(crate) fn new(id: &str) -> Self {
MessageSummary {
id: id.to_string(),
date: None,
subject: None,
}
}
pub(crate) fn id(&self) -> &str {
&self.id
}
pub(crate) fn set_subject(&mut self, subject: Option<String>) {
self.subject = subject
}
pub(crate) fn subject(&self) -> &str {
if let Some(s) = &self.subject {
s
} else {
"*** No Subject for Message ***"
}
}
pub(crate) fn set_date(&mut self, date: Option<String>) {
self.date = date
}
pub(crate) fn date(&self) -> &str {
if let Some(d) = &self.date {
d
} else {
"*** No Date for Message ***"
}
}
pub(crate) fn list_date_and_subject(&self) -> String {
let Some(date) = self.date.as_ref() else {
return "***invalid date or subject***".to_string();
};
let Some(subject) = self.subject.as_ref() else {
return "***invalid date or subject***".to_string();
};
let s = date[5..16].to_string();
let s = format!("{s}: {}", subject.clone().elide(24));
s
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_message_summary_new() {
let summary = MessageSummary::new("test_message_id");
assert_eq!(summary.id(), "test_message_id");
assert_eq!(summary.subject(), "*** No Subject for Message ***");
assert_eq!(summary.date(), "*** No Date for Message ***");
}
#[test]
fn test_message_summary_set_subject() {
let mut summary = MessageSummary::new("test_id");
summary.set_subject(Some("Test Subject".to_string()));
assert_eq!(summary.subject(), "Test Subject");
summary.set_subject(None);
assert_eq!(summary.subject(), "*** No Subject for Message ***");
summary.set_subject(Some("".to_string()));
assert_eq!(summary.subject(), "");
}
#[test]
fn test_message_summary_set_date() {
let mut summary = MessageSummary::new("test_id");
summary.set_date(Some("2023-12-25 10:30:00".to_string()));
assert_eq!(summary.date(), "2023-12-25 10:30:00");
summary.set_date(None);
assert_eq!(summary.date(), "*** No Date for Message ***");
summary.set_date(Some("".to_string()));
assert_eq!(summary.date(), "");
}
#[test]
fn test_message_summary_list_date_and_subject_valid() {
let mut summary = MessageSummary::new("test_id");
summary.set_date(Some("2023-12-25 10:30:00 GMT".to_string()));
summary.set_subject(Some(
"This is a very long subject that should be elided".to_string(),
));
let display = summary.list_date_and_subject();
assert!(display.contains("2-25 10:30"));
assert!(display.contains(":"));
assert!(display.len() <= 40); }
#[test]
fn test_message_summary_list_date_and_subject_missing_fields() {
let mut summary = MessageSummary::new("test_id");
summary.set_subject(Some("Test Subject".to_string()));
let result = summary.list_date_and_subject();
assert_eq!(result, "***invalid date or subject***");
let mut summary2 = MessageSummary::new("test_id");
summary2.set_date(Some("2023-12-25 10:30:00".to_string()));
let result2 = summary2.list_date_and_subject();
assert_eq!(result2, "***invalid date or subject***");
let summary3 = MessageSummary::new("test_id");
let result3 = summary3.list_date_and_subject();
assert_eq!(result3, "***invalid date or subject***");
}
#[test]
fn test_message_summary_clone() {
let mut original = MessageSummary::new("original_id");
original.set_subject(Some("Original Subject".to_string()));
original.set_date(Some("2023-12-25 10:30:00".to_string()));
let cloned = original.clone();
assert_eq!(original.id(), cloned.id());
assert_eq!(original.subject(), cloned.subject());
assert_eq!(original.date(), cloned.date());
}
#[test]
fn test_message_summary_debug() {
let mut summary = MessageSummary::new("debug_test_id");
summary.set_subject(Some("Debug Subject".to_string()));
summary.set_date(Some("2023-12-25".to_string()));
let debug_str = format!("{summary:?}");
assert!(debug_str.contains("MessageSummary"));
assert!(debug_str.contains("debug_test_id"));
assert!(debug_str.contains("Debug Subject"));
assert!(debug_str.contains("2023-12-25"));
}
#[test]
fn test_message_summary_unicode_handling() {
let mut summary = MessageSummary::new("unicode_test");
summary.set_subject(Some("📧 Important émails with 䏿–‡å—符".to_string()));
summary.set_date(Some("2023-12-25 10:30:00 UTC+8 🕒".to_string()));
assert_eq!(summary.subject(), "📧 Important émails with 䏿–‡å—符");
assert_eq!(summary.date(), "2023-12-25 10:30:00 UTC+8 🕒");
let display = summary.list_date_and_subject();
assert!(!display.is_empty());
}
#[test]
fn test_message_summary_edge_cases() {
let test_cases = vec![
("", "Empty ID"),
("a", "Single char ID"),
(
"very_long_message_id_that_exceeds_normal_length_expectations_123456789",
"Very long ID",
),
("msg-with-dashes", "ID with dashes"),
("msg_with_underscores", "ID with underscores"),
("123456789", "Numeric ID"),
];
for (id, description) in test_cases {
let summary = MessageSummary::new(id);
assert_eq!(summary.id(), id, "Failed for case: {description}");
}
}
}