Skip to main content

media_seek/
error.rs

1//! Error types for the `media-seek` crate.
2
3/// A type alias for `Result<T, Error>`.
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// All errors that can be produced by `media-seek`.
7#[derive(Debug, thiserror::Error)]
8pub enum Error {
9    /// The container format is not recognised (MHTML, plaintext, or unknown magic bytes).
10    #[error("Unsupported or unrecognized container format")]
11    UnsupportedFormat,
12
13    /// The container index could not be parsed (truncated data, invalid structure, etc.).
14    #[error("Container index parse failed: {reason}")]
15    ParseFailed { reason: String },
16
17    /// The container format was detected but no seek index was found in the probe.
18    ///
19    /// This may occur when a classic MP4's `moov` box is beyond the probe window, or when
20    /// neither a SIDX box nor a `moov` box are present within the probed bytes.
21    #[error("Seek index not found in probe: {reason}")]
22    IndexNotFound { reason: String },
23
24    /// An extra `Range` fetch required by the parser failed (WebM Cues, AVI idx1, MPEG-TS PCR, OGG bisection).
25    #[error("Extra Range fetch failed: {0}")]
26    FetchFailed(Box<dyn std::error::Error + Send + Sync>),
27}
28
29impl Error {
30    /// Convenience constructor for `ParseFailed`.
31    pub(crate) fn parse(reason: impl Into<String>) -> Self {
32        let reason = reason.into();
33        tracing::warn!(reason = %reason, "Container index parse failed");
34        Self::ParseFailed { reason }
35    }
36
37    /// Convenience constructor for `IndexNotFound`.
38    pub(crate) fn index_not_found(reason: impl Into<String>) -> Self {
39        let reason = reason.into();
40        tracing::warn!(reason = %reason, "Seek index not found in probe");
41        Self::IndexNotFound { reason }
42    }
43
44    /// Convenience constructor for `FetchFailed`.
45    pub(crate) fn fetch<E: std::error::Error + Send + Sync + 'static>(source: E) -> Self {
46        tracing::warn!(error = %source, "Extra Range fetch failed");
47        Self::FetchFailed(Box::new(source))
48    }
49}