🐛 fix(gmail): display message date and subject

- Refactor log_message_subjects to log_messages for clarity
- Simplify date and subject retrieval in log_messages
- Use MessageSummary's list_date_and_subject for logging
- Elide subject in MessageSummary for consistent display
This commit is contained in:
Jeremiah Russell
2025-10-14 17:00:37 +01:00
committed by Jeremiah Russell
parent f9e86bf8d9
commit 37d02bd0bf
3 changed files with 12 additions and 27 deletions

View File

@@ -43,7 +43,7 @@ impl MessageCli {
match self.action {
MessageAction::List => {
if log::max_level() >= log::Level::Info {
client.log_message_subjects().await
client.log_messages().await
} else {
Ok(())
}

View File

@@ -1,3 +1,5 @@
use crate::utils::Elide;
#[derive(Debug, Clone)]
pub struct MessageSummary {
id: String,
@@ -18,8 +20,8 @@ impl MessageSummary {
&self.id
}
pub(crate) fn set_subject(&mut self, subject: &str) {
self.subject = Some(subject.to_string())
pub(crate) fn set_subject(&mut self, subject: Option<String>) {
self.subject = subject
}
pub(crate) fn subject(&self) -> &str {
@@ -30,8 +32,8 @@ impl MessageSummary {
}
}
pub(crate) fn set_date(&mut self, date: &str) {
self.date = Some(date.to_string())
pub(crate) fn set_date(&mut self, date: Option<String>) {
self.date = date
}
pub(crate) fn date(&self) -> &str {
@@ -51,7 +53,7 @@ impl MessageSummary {
return "***invalid date or subject***".to_string();
};
let s = date[5..16].to_string();
let s = format!("{s}: {subject}");
let s = format!("{s}: {}", subject.to_string().elide(24));
s
}
}

View File

@@ -5,12 +5,10 @@ use google_gmail1::{
hyper_util::client::legacy::connect::HttpConnector,
};
use crate::utils::Elide;
/// Methods to select lists of messages from the mailbox
pub trait MessageList {
/// Log the initial characters of the subjects of the message in the list
fn log_message_subjects(&mut self) -> impl std::future::Future<Output = Result<()>> + Send;
fn log_messages(&mut self) -> impl std::future::Future<Output = Result<()>> + Send;
/// List of messages
fn messages_list(
&mut self,
@@ -195,7 +193,7 @@ impl MessageList for GmailClient {
Ok(list)
}
async fn log_message_subjects(&mut self) -> Result<()> {
async fn log_messages(&mut self) -> Result<()> {
let hub = self.hub();
for message in &mut self.messages {
log::trace!("{}", message.id());
@@ -210,8 +208,6 @@ impl MessageList for GmailClient {
.await
.map_err(Box::new)?;
let mut subject = String::new();
let mut date = String::new();
let Some(payload) = m.payload else { continue };
let Some(headers) = payload.headers else {
continue;
@@ -220,8 +216,8 @@ impl MessageList for GmailClient {
for header in headers {
if let Some(name) = header.name {
match name.to_lowercase().as_str() {
"subject" => subject = header.value.unwrap_or_default(),
"date" => date = header.value.unwrap_or_default(),
"subject" => message.set_subject(header.value),
"date" => message.set_date(header.value),
_ => {}
}
} else {
@@ -229,19 +225,6 @@ impl MessageList for GmailClient {
}
}
if date.is_empty() {
log::info!("***No orig-date field***");
} else {
message.set_date(&date);
}
if subject.is_empty() {
log::info!("***Email with no subject***");
} else {
subject.elide(24);
message.set_subject(&subject);
}
log::info!("{}", message.list_date_and_subject());
}