| 1 | use std::error::Error; |
| 2 | use std::fmt; |
| 3 | |
| 4 | /// Alias for `Result<T, PageError>` |
| 5 | pub type Result<T> = std::result::Result<T, PageError>; |
| 6 | |
| 7 | /// Errors that can occur while performing `Page` operations |
| 8 | #[derive(Debug)] |
| 9 | pub enum PageError { |
| 10 | /// The reader contains a page with a nonzero version |
| 11 | InvalidVersion, |
| 12 | /// The reader contains a page with a segment count < 1 |
| 13 | BadSegmentCount, |
| 14 | /// The reader contains a page without a magic signature (OggS) |
| 15 | MissingMagic, |
| 16 | /// The reader contains too much data for a single page |
| 17 | TooMuchData, |
| 18 | /// The reader contains too little data to extract the expected information |
| 19 | NotEnoughData, |
| 20 | /// Any std::io::Error |
| 21 | Io(std::io::Error), |
| 22 | } |
| 23 | |
| 24 | impl fmt::Display for PageError { |
| 25 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 26 | match self { |
| 27 | PageError::InvalidVersion => { |
| 28 | write!(f, "Invalid stream structure version (Should always be 0)") |
| 29 | }, |
| 30 | PageError::BadSegmentCount => write!(f, "Page has a segment count < 1"), |
| 31 | PageError::MissingMagic => write!(f, "Page is missing a magic signature"), |
| 32 | PageError::TooMuchData => write!(f, "Too much data was provided"), |
| 33 | PageError::NotEnoughData => { |
| 34 | write!(f, "Too little data is available for the expected read") |
| 35 | }, |
| 36 | PageError::Io(err) => write!(f, "{}", err), |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | impl Error for PageError { |
| 42 | fn source(&self) -> Option<&(dyn Error + 'static)> { |
| 43 | match *self { |
| 44 | PageError::Io(ref e) => Some(e), |
| 45 | _ => None, |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | impl From<std::io::Error> for PageError { |
| 51 | fn from(err: std::io::Error) -> PageError { |
| 52 | PageError::Io(err) |
| 53 | } |
| 54 | } |