grr-cli 0.4.0

Google tools from the terminal, at maximum performance: zero-config Gmail, Calendar, Drive, Contacts, Chat and Forms over HTTP/3
//! Thread operations CLI commands

use crate::gmail::GmailClient;
use crate::output::{OutputFormat, print_output};
use anyhow::Result;
use clap::{Args, Subcommand};

#[derive(Subcommand, Debug)]
pub enum ThreadCommands {
    #[command(about = "List Gmail threads")]
    List(ListArgs),
    /// Modify labels on a thread
    Label(LabelArgs),
    /// Move thread to trash
    Trash(TrashArgs),
    /// Restore thread from trash
    Untrash(UntrashArgs),
    /// Permanently delete a thread
    Delete(DeleteArgs),
}

#[derive(Args, Debug)]
pub struct ListArgs {
    #[arg(default_value = "")]
    pub query: String,

    #[arg(short, long, default_value = "10")]
    pub max: usize,

    #[arg(short, long, value_enum, default_value = "json")]
    pub format: OutputFormat,
}

#[derive(Args, Debug)]
pub struct LabelArgs {
    /// Thread ID
    pub thread_id: String,

    /// Labels to add (comma-separated)
    #[arg(long, value_delimiter = ',')]
    pub add: Vec<String>,

    /// Labels to remove (comma-separated)
    #[arg(long, value_delimiter = ',')]
    pub remove: Vec<String>,
}

#[derive(Args, Debug)]
pub struct TrashArgs {
    /// Thread ID
    pub thread_id: String,
}

#[derive(Args, Debug)]
pub struct UntrashArgs {
    /// Thread ID
    pub thread_id: String,
}

#[derive(Args, Debug)]
pub struct DeleteArgs {
    /// Thread ID
    pub thread_id: String,
}

pub async fn handle_thread_cmd(client: &GmailClient, cmd: ThreadCommands) -> Result<()> {
    match cmd {
        ThreadCommands::List(args) => {
            let threads = client.list_threads(&args.query, args.max).await?;
            print_output(&threads, args.format)?;
        }
        ThreadCommands::Label(args) => {
            client
                .modify_thread_labels(&args.thread_id, &args.add, &args.remove)
                .await?;
            println!("Labels updated for thread {}", args.thread_id);
        }
        ThreadCommands::Trash(args) => {
            client.trash_thread(&args.thread_id).await?;
            println!("Thread {} moved to trash", args.thread_id);
        }
        ThreadCommands::Untrash(args) => {
            client.untrash_thread(&args.thread_id).await?;
            println!("Thread {} restored from trash", args.thread_id);
        }
        ThreadCommands::Delete(args) => {
            client.delete_thread(&args.thread_id).await?;
            println!("Thread {} permanently deleted", args.thread_id);
        }
    }
    Ok(())
}