Skip to main content

ffmpeg_pipeline/
result.rs

1use ffmpeg_next::codec::Id;
2use thiserror::Error;
3
4#[derive(Error, Debug)]
5pub enum FFmpegError {
6    #[error("IO error: {0}")]
7    Io(#[from] std::io::Error),
8    #[error("FFmpeg error: {0}")]
9    FFmpeg(#[from] ffmpeg_next::Error),
10    #[error("Attachment not found: {0}")]
11    AttachmentNotFound(usize),
12    #[error("Stream not found: {0}")]
13    StreamNotFound(usize),
14    #[error("Codec not found: {0:?}")]
15    CodecNotFound(Id),
16    #[error("Invalid format: {0}")]
17    InvalidFormat(String),
18    #[error("Invalid frame type: {0}")]
19    InvalidFrameType(String),
20    #[error("Invalid stream type: {0}")]
21    InvalidStreamType(String),
22    #[error(
23        "Decoder {operation} failed on stream {stream} at packet {packet_position:?}: {source}"
24    )]
25    Decoder {
26        operation: &'static str,
27        stream: usize,
28        packet_position: Option<isize>,
29        #[source]
30        source: ffmpeg_next::Error,
31    },
32}
33
34impl FFmpegError {
35    pub(crate) fn decoder(
36        operation: &'static str,
37        stream: usize,
38        packet_position: Option<isize>,
39        source: ffmpeg_next::Error,
40    ) -> Self {
41        Self::Decoder {
42            operation,
43            stream,
44            packet_position,
45            source,
46        }
47    }
48}
49
50impl PartialEq<FFmpegError> for FFmpegError {
51    fn eq(&self, other: &FFmpegError) -> bool {
52        match (self, other) {
53            (FFmpegError::FFmpeg(e1), FFmpegError::FFmpeg(e2)) => e1 == e2,
54            (FFmpegError::AttachmentNotFound(e1), FFmpegError::AttachmentNotFound(e2)) => e1 == e2,
55            (FFmpegError::StreamNotFound(e1), FFmpegError::StreamNotFound(e2)) => e1 == e2,
56            (FFmpegError::CodecNotFound(e1), FFmpegError::CodecNotFound(e2)) => e1 == e2,
57            (FFmpegError::InvalidFrameType(e1), FFmpegError::InvalidFrameType(e2)) => e1 == e2,
58            (
59                FFmpegError::Decoder {
60                    operation: o1,
61                    stream: s1,
62                    packet_position: p1,
63                    source: e1,
64                },
65                FFmpegError::Decoder {
66                    operation: o2,
67                    stream: s2,
68                    packet_position: p2,
69                    source: e2,
70                },
71            ) => o1 == o2 && s1 == s2 && p1 == p2 && e1 == e2,
72            _ => false,
73        }
74    }
75}
76
77pub type FFmpegResult<T = ()> = Result<T, FFmpegError>;