Skip to main content

alpha2_container/
error.rs

1//! Demux and mux failures.
2
3use alpha2_media::{DecodeError, EncodeError};
4
5/// Why a file could not be read.
6#[derive(Debug)]
7pub enum DemuxError {
8    /// A format, codec layout or feature this layer does not read.
9    Unsupported(String),
10    /// Data that breaks its format's rules or a size limit.
11    Invalid(String),
12    /// A track id the reader does not have.
13    NoSuchTrack(u32),
14    /// The file could not be read.
15    Io(std::io::Error),
16}
17
18impl std::fmt::Display for DemuxError {
19    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        match self {
21            Self::Unsupported(reason) => write!(formatter, "{reason}"),
22            Self::Invalid(reason) => write!(formatter, "invalid media: {reason}"),
23            Self::NoSuchTrack(track) => write!(formatter, "no track {track}"),
24            Self::Io(error) => write!(formatter, "cannot read the file: {error}"),
25        }
26    }
27}
28
29impl std::error::Error for DemuxError {
30    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
31        match self {
32            Self::Io(error) => Some(error),
33            _ => None,
34        }
35    }
36}
37
38impl From<std::io::Error> for DemuxError {
39    fn from(error: std::io::Error) -> Self {
40        Self::Io(error)
41    }
42}
43
44impl From<DemuxError> for DecodeError {
45    fn from(error: DemuxError) -> Self {
46        match error {
47            DemuxError::Unsupported(reason) => Self::Unsupported(reason),
48            error => Self::Adapter(error.to_string()),
49        }
50    }
51}
52
53/// Why a file could not be written.
54#[derive(Debug)]
55pub enum MuxError {
56    /// A codec or track layout the container cannot carry.
57    Unsupported(String),
58    /// Packets out of order, or a track added too late.
59    Invalid(String),
60    /// The file could not be written.
61    Io(std::io::Error),
62}
63
64impl std::fmt::Display for MuxError {
65    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        match self {
67            Self::Unsupported(reason) | Self::Invalid(reason) => write!(formatter, "{reason}"),
68            Self::Io(error) => write!(formatter, "cannot write the file: {error}"),
69        }
70    }
71}
72
73impl std::error::Error for MuxError {
74    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
75        match self {
76            Self::Io(error) => Some(error),
77            _ => None,
78        }
79    }
80}
81
82impl From<std::io::Error> for MuxError {
83    fn from(error: std::io::Error) -> Self {
84        Self::Io(error)
85    }
86}
87
88impl From<MuxError> for EncodeError {
89    fn from(error: MuxError) -> Self {
90        match error {
91            MuxError::Io(error) => Self::Output(error.to_string()),
92            error => Self::Adapter(error.to_string()),
93        }
94    }
95}