mpeg_audio_header/
error.rs

1// SPDX-FileCopyrightText: The mpeg-audio-header authors
2// SPDX-License-Identifier: MPL-2.0
3
4use thiserror::Error;
5
6use crate::ReadPosition;
7
8/// Error enriched with position information
9#[derive(Debug, Error)]
10#[error("{} at position {:.3} ms (byte offset = {} / 0x{:X})",
11        .source, .position.duration.as_secs_f64() * 1000.0, .position.byte_offset, .position.byte_offset)]
12pub struct PositionalError {
13    #[source]
14    pub(crate) source: Error,
15
16    pub(crate) position: ReadPosition,
17}
18
19impl PositionalError {
20    /// The actual error
21    #[must_use]
22    pub const fn source(&self) -> &Error {
23        &self.source
24    }
25
26    /// The last known position where this error occurred
27    #[must_use]
28    pub const fn position(&self) -> &ReadPosition {
29        &self.position
30    }
31}
32
33impl PositionalError {
34    pub(crate) fn is_unexpected_eof(&self) -> bool {
35        self.source.is_unexpected_eof()
36    }
37}
38
39/// Error type
40#[derive(Debug, Error)]
41#[non_exhaustive]
42pub enum Error {
43    /// Unexpected I/O error occurred
44    #[error(transparent)]
45    IoError(#[from] std::io::Error),
46
47    #[error("frame error: {0}")]
48    FrameError(String),
49}
50
51impl Error {
52    fn is_unexpected_eof(&self) -> bool {
53        match self {
54            Self::IoError(err) => {
55                matches!(err.kind(), std::io::ErrorKind::UnexpectedEof)
56            }
57            _ => false,
58        }
59    }
60}