creek_core/read/
error.rs

1use std::error::Error;
2
3/// A fatal error occurred and the stream cannot continue.
4#[derive(Debug)]
5pub enum FatalReadError<FatalDecoderError: Error> {
6    /// The stream is closed and thus cannot continue.
7    StreamClosed,
8    /// A fatal decoder error occurred. The stream cannot continue.
9    DecoderError(FatalDecoderError),
10}
11
12/// An error reading the file.
13#[derive(Debug)]
14pub enum ReadError<FatalDecoderError: Error> {
15    /// A fatal error occurred. The stream cannot continue.
16    FatalError(FatalReadError<FatalDecoderError>),
17    /// The end of the file was reached. The stream must be seeked to
18    /// an earlier position to continue reading data. Until then, output
19    /// silence.
20    ///
21    /// If this is returned, then the playhead of the stream did not
22    /// advance.
23    EndOfFile,
24    /// The given cache with index `index` is out of range of the number
25    /// of caches assigned to this stream. Please try a
26    /// different index.
27    ///
28    /// If this is returned, then the playhead of the stream did not
29    /// advance.
30    CacheIndexOutOfRange { index: usize, num_caches: usize },
31    /// The message channel to the IO server was full.
32    ///
33    /// In theory this should not happen, but if it does, then output silence
34    /// until the channel has more slots open later.
35    ///
36    /// If this is returned, then the playhead of the stream did not
37    /// advance.
38    IOServerChannelFull,
39    /// The given buffer does not match the internal layout of the stream. Check
40    /// that the number of channels in both are the same.
41    ///
42    /// If this is returned, then the playhead of the stream did not
43    /// advance.
44    InvalidBuffer,
45}
46
47impl<FatalDecoderError: Error> std::error::Error for ReadError<FatalDecoderError> {}
48
49impl<FatalDecoderError: Error> std::fmt::Display for ReadError<FatalDecoderError> {
50    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51        match self {
52            ReadError::FatalError(e) => match e {
53                FatalReadError::StreamClosed => {
54                    write!(f, "Fatal error: stream is closed")
55                }
56                FatalReadError::DecoderError(de) => {
57                    write!(f, "Fatal decoder error: {:?}", de)
58                }
59            },
60            ReadError::EndOfFile => write!(f, "End of file"),
61            ReadError::CacheIndexOutOfRange { index, num_caches } => {
62                write!(
63                    f,
64                    "Cache index {} is out of range of the number of allocated caches {}",
65                    index, num_caches
66                )
67            }
68            ReadError::IOServerChannelFull => {
69                write!(f, "The message channel to the IO server is full.")
70            }
71            ReadError::InvalidBuffer => {
72                write!(f, "Fill buffer does not match internal buffer layout")
73            }
74        }
75    }
76}