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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//! This module contains the various kinds of errors used by this crate.

use std::error;
use std::fmt;
use std::io;
use std::num;

/// Represents the possible errors which can occur when manipulating
/// a `Grid`.
#[derive(Debug)]
pub enum GridErrorKind {
    OutOfBoundCoords,
}

impl fmt::Display for GridErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            GridErrorKind::OutOfBoundCoords => write!(f, "Error: out of bound index"),
        }
    }
}

impl error::Error for GridErrorKind {
    fn description(&self) -> &str {
        match *self {
            GridErrorKind::OutOfBoundCoords => "out of bound index",
        }
    }

    fn cause(&self) -> Option<&error::Error> {
        None
    }
}

/// Represents the possible errors which can occur when manipulating
/// a life file.
#[derive(Debug)]
pub enum FileParsingErrorKind {
    UnknownFormat,
    IoError,
    // EmptyFile, TODO: add this Later
    IncompleteFile,
    RuleParsingError,
    CoordParsingError,
    OutOfBoundCoords(GridErrorKind),
}

impl fmt::Display for FileParsingErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            FileParsingErrorKind::UnknownFormat => write!(f, "Unknown file format"),
            FileParsingErrorKind::IoError => write!(f, "IO error"),
            FileParsingErrorKind::IncompleteFile => write!(f, "Incomplete or empty file"), // TODO: separately handle the case where the file is empty
            FileParsingErrorKind::RuleParsingError => write!(f, "Invalid ruleset"),
            FileParsingErrorKind::CoordParsingError => write!(f, "Invalid coordinates"),
            FileParsingErrorKind::OutOfBoundCoords(ref err) => write!(f, "{}", err),
        }
    }
}

impl error::Error for FileParsingErrorKind {
    fn description(&self) -> &str {
        match *self {
            FileParsingErrorKind::UnknownFormat => "unknown file format",
            FileParsingErrorKind::IoError => "IO error",
            FileParsingErrorKind::IncompleteFile => "incomplete or empty file", // TODO: separately handle the case where the file is empty
            FileParsingErrorKind::RuleParsingError => "invalid ruleset",
            FileParsingErrorKind::CoordParsingError => "invalid coordinates",
            FileParsingErrorKind::OutOfBoundCoords(ref err) => err.description(),
        }
    }

    fn cause(&self) -> Option<&error::Error> {
        match *self {
            FileParsingErrorKind::OutOfBoundCoords(ref err) => err.cause(),
            _ => None,
        }
    }
}

impl From<io::Error> for FileParsingErrorKind {
    fn from(_: io::Error) -> FileParsingErrorKind {
        FileParsingErrorKind::IoError
    }
}

impl From<num::ParseIntError> for FileParsingErrorKind {
    fn from(_: num::ParseIntError) -> FileParsingErrorKind {
        FileParsingErrorKind::CoordParsingError
    }
}

impl From<GridErrorKind> for FileParsingErrorKind {
    fn from(err: GridErrorKind) -> FileParsingErrorKind {
        FileParsingErrorKind::OutOfBoundCoords(err)
    }
}