Skip to main content

romm_cli/commands/
cache.rs

1use anyhow::Result;
2use clap::{Args, Subcommand};
3
4use crate::core::cache::RomCache;
5
6#[derive(Args, Debug)]
7pub struct CacheCommand {
8    #[command(subcommand)]
9    pub action: CacheAction,
10}
11
12#[derive(Subcommand, Debug)]
13pub enum CacheAction {
14    /// Print the effective ROM cache file path.
15    Path,
16    /// Show ROM cache metadata and parse status.
17    Info,
18    /// Delete the ROM cache file if it exists.
19    Clear,
20}
21
22pub fn handle(cmd: CacheCommand) -> Result<()> {
23    match cmd.action {
24        CacheAction::Path => {
25            println!("{}", RomCache::effective_path().display());
26        }
27        CacheAction::Info => {
28            let info = RomCache::read_info();
29            println!("path: {}", info.path.display());
30            println!("exists: {}", info.exists);
31            if let Some(size) = info.size_bytes {
32                println!("size_bytes: {size}");
33            }
34            if let Some(version) = info.version {
35                println!("version: {version}");
36            }
37            if let Some(count) = info.entry_count {
38                println!("entries: {count}");
39            }
40            if let Some(err) = info.parse_error {
41                println!("parse_error: {err}");
42            }
43        }
44        CacheAction::Clear => {
45            if RomCache::clear_file()? {
46                println!("ROM cache cleared.");
47            } else {
48                println!("ROM cache file does not exist.");
49            }
50        }
51    }
52    Ok(())
53}