Skip to main content

romm_cli/commands/
roms.rs

1use anyhow::Result;
2use clap::{Args, Subcommand};
3
4use crate::client::RommClient;
5use crate::commands::print::print_roms_table;
6use crate::commands::OutputFormat;
7use crate::endpoints::roms::GetRoms;
8use crate::services::RomService;
9
10/// CLI entrypoint for listing/searching ROMs via `/api/roms`.
11#[derive(Args, Debug)]
12pub struct RomsCommand {
13    #[command(subcommand)]
14    pub action: Option<RomsAction>,
15
16    /// Search term to filter roms
17    #[arg(long, global = true, visible_aliases = ["query", "q"])]
18    pub search_term: Option<String>,
19
20    /// Filter by platform ID
21    #[arg(long, global = true, visible_alias = "id")]
22    pub platform_id: Option<u64>,
23
24    /// Page size limit
25    #[arg(long, global = true)]
26    pub limit: Option<u32>,
27
28    /// Page offset
29    #[arg(long, global = true)]
30    pub offset: Option<u32>,
31
32    /// Output as JSON (overrides global --json when set).
33    #[arg(long, global = true)]
34    pub json: bool,
35}
36
37#[derive(Subcommand, Debug)]
38pub enum RomsAction {
39    /// List available ROMs (default)
40    #[command(visible_alias = "ls")]
41    List,
42    /// Get detailed information for a single ROM
43    #[command(visible_alias = "info")]
44    Get {
45        /// The ID of the ROM
46        id: u64,
47    },
48}
49
50pub async fn handle(cmd: RomsCommand, client: &RommClient, format: OutputFormat) -> Result<()> {
51    let action = cmd.action.unwrap_or(RomsAction::List);
52
53    match action {
54        RomsAction::List => {
55            let ep = GetRoms {
56                search_term: cmd.search_term.clone(),
57                platform_id: cmd.platform_id,
58                collection_id: None,
59                limit: cmd.limit,
60                offset: cmd.offset,
61            };
62
63            let service = RomService::new(client);
64            let results = service.search_roms(&ep).await?;
65
66            match format {
67                OutputFormat::Json => {
68                    println!("{}", serde_json::to_string_pretty(&results)?);
69                }
70                OutputFormat::Text => {
71                    print_roms_table(&results);
72                }
73            }
74        }
75        RomsAction::Get { id } => {
76            let service = RomService::new(client);
77            let rom = service.get_rom(id).await?;
78
79            match format {
80                OutputFormat::Json => {
81                    println!("{}", serde_json::to_string_pretty(&rom)?);
82                }
83                OutputFormat::Text => {
84                    // For now, reuse the table printer for a single item
85                    // or just pretty-print the JSON.
86                    println!("{}", serde_json::to_string_pretty(&rom)?);
87                }
88            }
89        }
90    }
91
92    Ok(())
93}