Skip to main content

ff_probe/
error.rs

1//! Error types for media probing.
2
3use std::path::PathBuf;
4use thiserror::Error;
5
6/// Error type for media probing operations.
7#[derive(Error, Debug)]
8pub enum ProbeError {
9    /// The specified file was not found.
10    #[error("File not found: {path}")]
11    FileNotFound {
12        /// Path to the file that was not found.
13        path: PathBuf,
14    },
15
16    /// The file could not be opened.
17    #[error("Cannot open file: {path} - {reason}")]
18    CannotOpen {
19        /// Path to the file that could not be opened.
20        path: PathBuf,
21        /// Reason why the file could not be opened.
22        reason: String,
23    },
24
25    /// The file is not a valid media file.
26    #[error("Invalid media file: {path} - {reason}")]
27    InvalidMedia {
28        /// Path to the invalid media file.
29        path: PathBuf,
30        /// Reason why the file is invalid.
31        reason: String,
32    },
33
34    /// No streams were found in the file.
35    #[error("No streams found in file: {path}")]
36    NoStreams {
37        /// Path to the file with no streams.
38        path: PathBuf,
39    },
40
41    /// An I/O error occurred.
42    #[error("IO error: {0}")]
43    Io(#[from] std::io::Error),
44
45    /// An `FFmpeg` error occurred.
46    #[error("FFmpeg error: {0}")]
47    Ffmpeg(String),
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_file_not_found_error() {
56        let err = ProbeError::FileNotFound {
57            path: PathBuf::from("/path/to/missing.mp4"),
58        };
59        let msg = err.to_string();
60        assert!(msg.contains("File not found"));
61        assert!(msg.contains("missing.mp4"));
62    }
63
64    #[test]
65    fn test_cannot_open_error() {
66        let err = ProbeError::CannotOpen {
67            path: PathBuf::from("/path/to/file.mp4"),
68            reason: "permission denied".to_string(),
69        };
70        let msg = err.to_string();
71        assert!(msg.contains("Cannot open file"));
72        assert!(msg.contains("permission denied"));
73    }
74
75    #[test]
76    fn test_invalid_media_error() {
77        let err = ProbeError::InvalidMedia {
78            path: PathBuf::from("/path/to/bad.mp4"),
79            reason: "corrupted header".to_string(),
80        };
81        let msg = err.to_string();
82        assert!(msg.contains("Invalid media file"));
83        assert!(msg.contains("corrupted header"));
84    }
85
86    #[test]
87    fn test_no_streams_error() {
88        let err = ProbeError::NoStreams {
89            path: PathBuf::from("/path/to/empty.mp4"),
90        };
91        let msg = err.to_string();
92        assert!(msg.contains("No streams found"));
93    }
94
95    #[test]
96    fn test_ffmpeg_error() {
97        let err = ProbeError::Ffmpeg("codec not found".to_string());
98        let msg = err.to_string();
99        assert!(msg.contains("FFmpeg error"));
100        assert!(msg.contains("codec not found"));
101    }
102
103    #[test]
104    fn test_io_error_conversion() {
105        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
106        let err: ProbeError = io_err.into();
107        assert!(matches!(err, ProbeError::Io(_)));
108    }
109}