Skip to main content

romm_cli/
services.rs

1//! Small service objects that wrap `RommClient` for higher-level operations.
2//!
3//! These are used by the CLI commands to keep a clear separation between
4//! \"how we talk to ROMM\" (HTTP) and \"what we want to do\" (list
5//! platforms, search ROMs, etc.).
6
7use anyhow::Result;
8
9use crate::client::RommClient;
10use crate::endpoints::{
11    platforms::{GetPlatform, ListPlatforms},
12    roms::{GetRom, GetRoms},
13};
14use crate::types::{Platform, Rom, RomList};
15
16/// Service encapsulating platform-related operations.
17pub struct PlatformService<'a> {
18    client: &'a RommClient,
19}
20
21impl<'a> PlatformService<'a> {
22    pub fn new(client: &'a RommClient) -> Self {
23        Self { client }
24    }
25
26    /// List all platforms from the ROMM API.
27    pub async fn list_platforms(&self) -> Result<Vec<Platform>> {
28        let platforms = self.client.call(&ListPlatforms).await?;
29        Ok(platforms)
30    }
31
32    /// Get a single platform by ID.
33    pub async fn get_platform(&self, id: u64) -> Result<Platform> {
34        let platform = self.client.call(&GetPlatform { id }).await?;
35        Ok(platform)
36    }
37}
38
39/// Service encapsulating ROM-related operations.
40pub struct RomService<'a> {
41    client: &'a RommClient,
42}
43
44impl<'a> RomService<'a> {
45    pub fn new(client: &'a RommClient) -> Self {
46        Self { client }
47    }
48
49    /// Search/list ROMs using a fully-configured `GetRoms` descriptor.
50    pub async fn search_roms(&self, ep: &GetRoms) -> Result<RomList> {
51        let results = self.client.call(ep).await?;
52        Ok(results)
53    }
54
55    /// Get a single ROM by ID.
56    pub async fn get_rom(&self, id: u64) -> Result<Rom> {
57        let rom = self.client.call(&GetRom { id }).await?;
58        Ok(rom)
59    }
60}