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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//             DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
//                    Version 2, December 2004
//
// Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
//
// Everyone is permitted to copy and distribute verbatim or modified
// copies of this license document, and changing it is allowed as long
// as the name is changed.
//
//            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
//   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
//
//  0. You just DO WHAT THE FUCK YOU WANT TO.
//
// Author: zadig <thomas chr(0x40) bailleux.me>

use std;

/// Errors related to the process of parsing.
#[derive(Debug)]
pub enum OLEError {
  /// This happens when filesize is null, or to big to fit into an usize.
  BadFileSize,

  /// Classic std::io::Error.
  IOError(std::io::Error),

  /// Something is not implemented yet ?
  NotImplementedYet,

  /// This is not a valid OLE file.
  InvalidOLEFile,

  /// Something has a bad size.
  BadSizeValue(&'static str),

  /// MSAT is empty.
  EmptyMasterSectorAllocationTable,

  /// Malformed SAT.
  NotSectorUsedBySAT,

  /// Unknown node type.
  NodeTypeUnknown,

  /// Root storage has a bad size.
  BadRootStorageSize
}

impl std::error::Error for OLEError {
  fn description(&self) -> &str {
    match *self {
      OLEError::BadFileSize => "Filesize is null or too big.",
      OLEError::IOError(ref e) => e.description(),
      OLEError::NotImplementedYet => "Method not implemented yet",
      OLEError::InvalidOLEFile => "Invalid OLE File",
      OLEError::BadSizeValue(ref e) => e,
      OLEError::EmptyMasterSectorAllocationTable => "MSAT is empty",
      OLEError::NotSectorUsedBySAT => "Sector is not a sector used by the SAT.",
      OLEError::NodeTypeUnknown => "Unknown node type",
      OLEError::BadRootStorageSize => "Bad RootStorage size"
    }
  }

  fn cause(&self) -> Option<&std::error::Error> {
    match *self {
      OLEError::IOError(ref e) => Some(e),
      _ => None
    }
  }
}

impl std::fmt::Display for OLEError {
  fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
    use std::error::Error;
    write!(f, "{}", self.description())
  }
}