♻️ refactor(trash): refactor trash module to trait implementation

- remove the `Trash` struct
- implement `Trash` as a trait for `GmailClient`
- move `batch_move_to_trash` and `batch_trash` functions to the `Trash` trait implementation
- update function calls to use the new trait implementation
This commit is contained in:
Jeremiah Russell
2025-10-13 15:59:12 +01:00
committed by Jeremiah Russell
parent 9625e64979
commit f0a268861f

View File

@@ -1,41 +1,28 @@
use google_gmail1::api::BatchModifyMessagesRequest; use google_gmail1::api::BatchModifyMessagesRequest;
use crate::{GmailClient, MessageList, Result}; use crate::{GmailClient, Result, message_list::MessageList};
/// Struct for trashing messages // /// Struct for trashing messages
#[derive(Debug)] // #[derive(Debug)]
pub struct Trash { // pub struct Trash {
message_list: MessageList, // message_list: MessageList,
} // }
impl Trash { pub(crate) trait Trash {
/// Create a new Trash struct async fn batch_move_to_trash(&self) -> Result<()>;
pub async fn new(client: &GmailClient) -> Result<Self> { async fn batch_trash(&self) -> Result<()>;
let message_list = MessageList::new(client).await?;
Ok(Trash { message_list })
}
/// return the message list struct
pub fn message_list(&mut self) -> &mut MessageList {
&mut self.message_list
}
/// Prepare the trash cli
pub async fn prepare(&mut self, pages: u32) -> Result<()> {
self.message_list.run(pages).await?;
Ok(())
} }
impl Trash for GmailClient {
/// Move the messages to trash /// Move the messages to trash
pub async fn batch_trash(&self) -> Result<()> { async fn batch_trash(&self) -> Result<()> {
self.batch_move_to_trash().await self.batch_move_to_trash().await
} }
async fn batch_move_to_trash(&self) -> Result<()> { async fn batch_move_to_trash(&self) -> Result<()> {
let add_label_ids = Some(Vec::from(["TRASH".to_string()])); let add_label_ids = Some(Vec::from(["TRASH".to_string()]));
let ids = Some(self.message_list.message_ids()); let ids = Some(self.message_ids());
let remove_label_ids = Some(self.message_list.label_ids()); let remove_label_ids = Some(self.label_ids());
let batch_request = BatchModifyMessagesRequest { let batch_request = BatchModifyMessagesRequest {
add_label_ids, add_label_ids,
@@ -46,7 +33,6 @@ impl Trash {
log::trace!("{batch_request:#?}"); log::trace!("{batch_request:#?}");
let _res = self let _res = self
.message_list
.hub() .hub()
.users() .users()
.messages_batch_modify(batch_request, "me") .messages_batch_modify(batch_request, "me")
@@ -55,7 +41,7 @@ impl Trash {
.await .await
.map_err(Box::new)?; .map_err(Box::new)?;
for m in self.message_list.messages() { for m in self.messages() {
log::info!("Message with subject `{}` moved to trash.", m.subject()); log::info!("Message with subject `{}` moved to trash.", m.subject());
} }