Skip to main content

musefs_format/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Error, PartialEq, Eq)]
4pub enum FormatError {
5    #[error("not a FLAC stream (missing fLaC marker)")]
6    NotFlac,
7    #[error("not an MP3 stream (no MPEG frame sync at the audio offset)")]
8    NotMp3,
9    #[error("truncated or malformed metadata")]
10    Malformed,
11    #[error("synthesized metadata exceeds the format's size limit")]
12    TooLarge,
13    #[error("not a supported MP4/M4A file")]
14    NotMp4,
15    #[error("not a supported WAV/RIFF file")]
16    NotWav,
17    #[error("synthesized region layout violates producer invariants: {0}")]
18    InvalidLayout(#[from] crate::layout::LayoutError),
19    #[error("producer invariant violated: {0}")]
20    ProducerBug(&'static str),
21    #[error("failed to read art {art_id} bytes for synthesis")]
22    ArtRead { art_id: i64 },
23}
24
25pub type Result<T> = std::result::Result<T, FormatError>;
26
27#[cfg(test)]
28mod tests {
29    use super::FormatError;
30    use crate::layout::LayoutError;
31
32    #[test]
33    fn invalid_layout_carries_inner_layout_error() {
34        let e: FormatError = LayoutError::EmptySegment.into();
35        assert!(matches!(
36            e,
37            FormatError::InvalidLayout(LayoutError::EmptySegment)
38        ));
39        // Display includes the inner reason, not just a generic string.
40        assert!(e.to_string().contains("zero length"));
41    }
42
43    #[test]
44    fn producer_bug_carries_reason() {
45        let e = FormatError::ProducerBug("no leading Inline");
46        assert!(e.to_string().contains("no leading Inline"));
47    }
48}