Skip to main content

oximedia_proxy/link/
verify.rs

1//! Proxy link verification.
2
3use super::manager::ProxyLinkManager;
4use crate::Result;
5
6/// Proxy link verifier.
7pub struct ProxyVerifier {
8    manager: ProxyLinkManager,
9}
10
11impl ProxyVerifier {
12    /// Create a new proxy verifier.
13    #[must_use]
14    pub fn new(manager: ProxyLinkManager) -> Self {
15        Self { manager }
16    }
17
18    /// Verify all links in the database.
19    pub fn verify_all(&mut self) -> Result<VerificationReport> {
20        let all_links = self.manager.all_links();
21        let total = all_links.len();
22        let mut valid = 0;
23        let mut invalid = 0;
24        let mut missing_proxy = Vec::new();
25        let mut missing_original = Vec::new();
26
27        for link in &all_links {
28            let proxy_exists = link.proxy_path.exists();
29            let original_exists = link.original_path.exists();
30
31            if proxy_exists && original_exists {
32                valid += 1;
33                let _ = self.manager.verify_link(&link.proxy_path);
34            } else {
35                invalid += 1;
36                if !proxy_exists {
37                    missing_proxy.push(link.proxy_path.clone());
38                }
39                if !original_exists {
40                    missing_original.push(link.original_path.clone());
41                }
42            }
43        }
44
45        Ok(VerificationReport {
46            total,
47            valid,
48            invalid,
49            missing_proxy,
50            missing_original,
51        })
52    }
53
54    /// Verify a specific link.
55    pub fn verify_link(&mut self, proxy_path: &std::path::Path) -> Result<bool> {
56        self.manager.verify_link(proxy_path)
57    }
58}
59
60/// Verification report.
61#[derive(Debug, Clone)]
62pub struct VerificationReport {
63    /// Total number of links checked.
64    pub total: usize,
65
66    /// Number of valid links.
67    pub valid: usize,
68
69    /// Number of invalid links.
70    pub invalid: usize,
71
72    /// Proxy files that are missing.
73    pub missing_proxy: Vec<std::path::PathBuf>,
74
75    /// Original files that are missing.
76    pub missing_original: Vec<std::path::PathBuf>,
77}
78
79impl VerificationReport {
80    /// Check if all links are valid.
81    #[must_use]
82    pub const fn all_valid(&self) -> bool {
83        self.invalid == 0
84    }
85
86    /// Get the percentage of valid links.
87    #[must_use]
88    pub fn valid_percentage(&self) -> f64 {
89        if self.total == 0 {
90            0.0
91        } else {
92            (self.valid as f64 / self.total as f64) * 100.0
93        }
94    }
95}
96
97#[cfg(test)]
98mod tests {
99    use super::*;
100
101    #[test]
102    fn test_verification_report() {
103        let report = VerificationReport {
104            total: 10,
105            valid: 8,
106            invalid: 2,
107            missing_proxy: Vec::new(),
108            missing_original: Vec::new(),
109        };
110
111        assert!(!report.all_valid());
112        assert_eq!(report.valid_percentage(), 80.0);
113    }
114
115    #[test]
116    fn test_all_valid() {
117        let report = VerificationReport {
118            total: 5,
119            valid: 5,
120            invalid: 0,
121            missing_proxy: Vec::new(),
122            missing_original: Vec::new(),
123        };
124
125        assert!(report.all_valid());
126        assert_eq!(report.valid_percentage(), 100.0);
127    }
128}