1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (c) 2016 Brandon Thomas <bt@brand.io, echelon@gmail.com>

use std::error::Error;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result;
use std::io;

/// Ilda library errors.
#[derive(Debug)]
pub enum IldaError {
  /// The ILDA file is too small to read.
  FileTooSmall,

  /// Problems were encountered while reading the ILDA data.
  InvalidData,

  /// Problems were encountered while reading the ILDA data, specifically with
  /// an invalid ILDA header section.
  InvalidHeader,

  /// Wraps standard library IO errors.
  IoError { cause: io::Error },

  /// No data in the file, or nothing could be parsed.
  NoData,

  /// Not yet supported.
  Unsupported
}

impl Error for IldaError {
  fn description(&self) -> &str {
    match *self {
      IldaError::FileTooSmall => "FileTooSmall",
      IldaError::InvalidData => "InvalidData",
      IldaError::InvalidHeader => "InvalidHeader",
      IldaError::IoError { .. } => "IoError",
      IldaError::NoData => "NoData",
      IldaError::Unsupported => "Unsupported",
    }
  }
}

impl Display for IldaError {
  fn fmt(&self, f: &mut Formatter) -> Result {
    write!(f, "{}", self.description())
  }
}

impl From<io::Error> for IldaError {
  fn from(error: io::Error) -> IldaError {
    IldaError::IoError { cause: error }
  }
}