Skip to main content

oximedia_proxy/conform/
relink.rs

1//! Proxy relink utilities.
2
3use crate::{ProxyLinkManager, Result};
4use std::path::{Path, PathBuf};
5
6/// Relink proxies to originals.
7pub struct Relinker<'a> {
8    link_manager: &'a ProxyLinkManager,
9}
10
11impl<'a> Relinker<'a> {
12    /// Create a new relinker.
13    #[must_use]
14    pub const fn new(link_manager: &'a ProxyLinkManager) -> Self {
15        Self { link_manager }
16    }
17
18    /// Relink a single proxy to its original.
19    ///
20    /// # Errors
21    ///
22    /// Returns an error if no link exists.
23    pub fn relink(&self, proxy_path: &Path) -> Result<PathBuf> {
24        self.link_manager
25            .get_original(proxy_path)
26            .map(|p| p.to_path_buf())
27    }
28
29    /// Relink multiple proxies to their originals.
30    pub fn relink_batch(&self, proxy_paths: &[PathBuf]) -> Vec<RelinkResult> {
31        proxy_paths
32            .iter()
33            .map(|proxy| match self.relink(proxy) {
34                Ok(original) => RelinkResult::Success {
35                    proxy: proxy.clone(),
36                    original,
37                },
38                Err(e) => RelinkResult::Failed {
39                    proxy: proxy.clone(),
40                    error: e.to_string(),
41                },
42            })
43            .collect()
44    }
45}
46
47/// Result of a relink operation.
48#[derive(Debug, Clone)]
49pub enum RelinkResult {
50    /// Successful relink.
51    Success {
52        /// Proxy path.
53        proxy: PathBuf,
54        /// Original path.
55        original: PathBuf,
56    },
57    /// Failed relink.
58    Failed {
59        /// Proxy path.
60        proxy: PathBuf,
61        /// Error message.
62        error: String,
63    },
64}
65
66impl RelinkResult {
67    /// Check if this result is successful.
68    #[must_use]
69    pub const fn is_success(&self) -> bool {
70        matches!(self, Self::Success { .. })
71    }
72}
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77
78    #[test]
79    fn test_relink_result() {
80        let result = RelinkResult::Success {
81            proxy: PathBuf::from("proxy.mp4"),
82            original: PathBuf::from("original.mov"),
83        };
84
85        assert!(result.is_success());
86    }
87}