romm_cli/commands/
roms.rs1use 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#[derive(Args, Debug)]
12pub struct RomsCommand {
13 #[command(subcommand)]
14 pub action: Option<RomsAction>,
15
16 #[arg(long, global = true, visible_aliases = ["query", "q"])]
18 pub search_term: Option<String>,
19
20 #[arg(long, global = true, visible_alias = "id")]
22 pub platform_id: Option<u64>,
23
24 #[arg(long, global = true)]
26 pub limit: Option<u32>,
27
28 #[arg(long, global = true)]
30 pub offset: Option<u32>,
31
32 #[arg(long, global = true)]
34 pub json: bool,
35}
36
37#[derive(Subcommand, Debug)]
38pub enum RomsAction {
39 #[command(visible_alias = "ls")]
41 List,
42 #[command(visible_alias = "info")]
44 Get {
45 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 println!("{}", serde_json::to_string_pretty(&rom)?);
87 }
88 }
89 }
90 }
91
92 Ok(())
93}