subx_cli/core/matcher/
cache.rs

1//! Caching utilities for the file matching engine.
2//!
3//! Defines cache data structures and operations to store and retrieve
4//! previous matching results for faster repeated execution.
5//!
6//! # Examples
7//!
8//! ```rust
9//! use subx_cli::core::matcher::cache::{CacheData, SnapshotItem, OpItem};
10//! // Load existing cache or initialize a new one
11//! ```
12
13use serde::{Deserialize, Serialize};
14
15/// Snapshot item representing a file state for directory comparison.
16///
17/// Used to detect changes in the filesystem since the last cache update.
18/// Contains essential file metadata for comparison purposes.
19#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
20pub struct SnapshotItem {
21    /// File name (without path)
22    pub name: String,
23    /// File size in bytes
24    pub size: u64,
25    /// Last modification time as Unix timestamp
26    pub mtime: u64,
27    /// File type classification (e.g., "video", "subtitle")
28    pub file_type: String,
29}
30
31/// Single match operation cache item storing result details.
32///
33/// Represents a cached match operation between a video and subtitle file,
34/// including all the analysis results and metadata.
35#[derive(Debug, Serialize, Deserialize, Clone)]
36pub struct OpItem {
37    /// Path to the video file
38    pub video_file: String,
39    /// Path to the subtitle file
40    pub subtitle_file: String,
41    /// The new name assigned to the subtitle file
42    pub new_subtitle_name: String,
43    /// Confidence score of the match (0.0 to 1.0)
44    pub confidence: f32,
45    /// List of reasoning factors for this match
46    pub reasoning: Vec<String>,
47}
48
49/// Dry-run cache data structure containing snapshot and match history.
50///
51/// Stores the complete state of a directory scan and match operations,
52/// enabling efficient incremental processing and result caching.
53#[derive(Debug, Serialize, Deserialize, Clone)]
54pub struct CacheData {
55    /// Version of the cache format for compatibility checking
56    pub cache_version: String,
57    /// Path to the directory that was processed
58    pub directory: String,
59    /// Snapshot of all files found during scanning
60    pub file_snapshot: Vec<SnapshotItem>,
61    /// List of all match operations performed
62    pub match_operations: Vec<OpItem>,
63    /// Timestamp when the cache was created
64    pub created_at: u64,
65    /// AI model used for matching operations
66    pub ai_model_used: String,
67    /// Hash of configuration used for matching
68    pub config_hash: String,
69    /// Records the relocation mode when the cache was generated
70    #[serde(default)]
71    pub original_relocation_mode: String,
72    /// Records whether backup was enabled when the cache was generated
73    #[serde(default)]
74    pub original_backup_enabled: bool,
75}
76
77impl CacheData {
78    /// Loads cache data from the specified file path.
79    pub fn load(path: &std::path::Path) -> Result<Self, anyhow::Error> {
80        let content = std::fs::read_to_string(path)?;
81        let data = serde_json::from_str(&content)?;
82        Ok(data)
83    }
84}