♻️ refactor(gmail): rename labels.rs to gmail_client.rs

- Renamed `labels.rs` to `gmail_client.rs` for better clarity
- Refactor `Labels` struct to `GmailClient` for better naming
- Update `get_label_map` to use BTreeMap for label storage
- Remove the show flag functionality
This commit is contained in:
Jeremiah Russell
2025-10-13 12:09:08 +01:00
committed by Jeremiah Russell
parent c76cd42c1d
commit 946398380b

View File

@@ -1,8 +1,7 @@
use std::collections::HashMap; use std::collections::BTreeMap;
use google_gmail1::{ use google_gmail1::{
Gmail, Gmail,
api::Label,
hyper_rustls::{HttpsConnector, HttpsConnectorBuilder}, hyper_rustls::{HttpsConnector, HttpsConnectorBuilder},
hyper_util::{ hyper_util::{
client::legacy::{Client, connect::HttpConnector}, client::legacy::{Client, connect::HttpConnector},
@@ -11,30 +10,28 @@ use google_gmail1::{
yup_oauth2::{ApplicationSecret, InstalledFlowAuthenticator, InstalledFlowReturnMethod}, yup_oauth2::{ApplicationSecret, InstalledFlowAuthenticator, InstalledFlowReturnMethod},
}; };
use crate::{Credential, Result}; use crate::{Credential, Error, Result};
/// Default for the maximum number of results to return on a page /// Default for the maximum number of results to return on a page
pub const DEFAULT_MAX_RESULTS: &str = "10"; pub const DEFAULT_MAX_RESULTS: &str = "200";
/// Struct to capture configuration for List API call. /// Struct to capture configuration for List API call.
pub struct Labels { pub struct GmailClient {
hub: Gmail<HttpsConnector<HttpConnector>>, hub: Gmail<HttpsConnector<HttpConnector>>,
label_list: Vec<Label>, label_map: BTreeMap<String, String>,
label_map: HashMap<String, String>,
} }
impl std::fmt::Debug for Labels { impl std::fmt::Debug for GmailClient {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Labels") f.debug_struct("Labels")
.field("label_list", &self.label_list)
.field("label_map", &self.label_map) .field("label_map", &self.label_map)
.finish() .finish()
} }
} }
impl Labels { impl GmailClient {
/// Create a new List struct and add the Gmail api connection. /// Create a new List struct and add the Gmail api connection.
pub async fn new(credential: &str, show: bool) -> Result<Self> { pub async fn new(credential: &str) -> Result<Self> {
let (config_dir, secret) = { let (config_dir, secret) = {
let config_dir = crate::utils::assure_config_dir_exists("~/.cull-gmail")?; let config_dir = crate::utils::assure_config_dir_exists("~/.cull-gmail")?;
@@ -63,35 +60,36 @@ impl Labels {
.unwrap(); .unwrap();
let hub = Gmail::new(client, auth); let hub = Gmail::new(client, auth);
let label_map = GmailClient::get_label_map(&hub).await?;
Ok(GmailClient { hub, label_map })
}
/// Create a new List struct and add the Gmail api connection.
async fn get_label_map(
hub: &Gmail<HttpsConnector<HttpConnector>>,
) -> Result<BTreeMap<String, String>> {
let call = hub.users().labels_list("me"); let call = hub.users().labels_list("me");
let (_response, list) = call.doit().await.map_err(Box::new)?; let (_response, list) = call
.add_scope("https://mail.google.com/")
.doit()
.await
.map_err(Box::new)?;
let Some(label_list) = list.labels else { let Some(label_list) = list.labels else {
return Ok(Labels { return Err(Error::NoLabelsFound);
hub,
label_list: Vec::new(),
label_map: HashMap::new(),
});
}; };
let mut label_map = HashMap::new(); let mut label_map = BTreeMap::new();
for label in &label_list { for label in &label_list {
if label.id.is_some() && label.name.is_some() { if label.id.is_some() && label.name.is_some() {
let name = label.name.clone().unwrap(); let name = label.name.clone().unwrap();
let id = label.id.clone().unwrap(); let id = label.id.clone().unwrap();
if show {
log::info!("{name}: {id}")
}
label_map.insert(name, id); label_map.insert(name, id);
} }
} }
Ok(Labels { Ok(label_map)
hub,
label_list,
label_map,
})
} }
/// Return the id for the name from the labels map. /// Return the id for the name from the labels map.
@@ -99,4 +97,11 @@ impl Labels {
pub fn get_label_id(&self, name: &str) -> Option<String> { pub fn get_label_id(&self, name: &str) -> Option<String> {
self.label_map.get(name).cloned() self.label_map.get(name).cloned()
} }
/// Show the label names and related id.
pub fn show_label(&self) {
for (name, id) in self.label_map.iter() {
log::info!("{name}: {id}")
}
}
} }