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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
pub mod validation;
use std::io::{self, Write};
use std::error::Error;
use std::fmt;
#[derive(Debug)]
pub enum NogoErrorKind {
IncorrectNumberOfArgs,
IncorrectTypes,
InvalidBoardDimensions,
CantOpenFileForSaving,
CantOpenFileForReading,
ErrorReadingGameFile,
EOFWaitingForUserInput,
SystemIOError,
ParsingError,
}
#[derive(Debug)]
pub struct NogoError<'a> {
kind: NogoErrorKind,
status: i32,
general: &'a str,
specific: &'a str,
}
impl<'a> fmt::Display for NogoError<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.specific)
}
}
impl<'a> Error for NogoError<'a> {
fn description(&self) -> &str {
self.specific
}
}
impl<'a> NogoError<'a> {
pub fn new(kind: NogoErrorKind) -> Self {
let mut error = NogoError {
kind: kind,
status: 0,
general: "",
specific: "",
};
match error.kind {
NogoErrorKind::IncorrectNumberOfArgs => {
error.status = 1;
error.general = "Program started with incorrect number of arguments";
}
NogoErrorKind::IncorrectTypes => {
error.status = 2;
error.general = "Invalid type(s)";
}
NogoErrorKind::InvalidBoardDimensions => {
error.status = 3;
error.general = "Invalid board dimension(s)";
}
NogoErrorKind::CantOpenFileForReading => {
error.status = 4;
error.general = "Unable to open save file";
}
NogoErrorKind::ErrorReadingGameFile => {
error.status = 5;
error.general = "Incorrect save file contents";
}
NogoErrorKind::EOFWaitingForUserInput => {
error.status = 6;
error.general = "End of input from user";
}
NogoErrorKind::CantOpenFileForSaving => {
error.status = 7;
error.general = "Unable to open new save file";
}
NogoErrorKind::SystemIOError => {
error.status = 8;
error.general = "System IO error";
}
NogoErrorKind::ParsingError => {
error.status = 9;
error.general = "Error while parsing value";
}
}
error
}
pub fn kind(&self) -> &NogoErrorKind {
&self.kind
}
pub fn status(&self) -> i32 {
self.status
}
pub fn general(&self) -> &'a str {
&self.general
}
pub fn specific(&self) -> &'a str {
&self.specific
}
pub fn set_specific(&mut self, msg: &'a str) {
self.specific = msg;
}
}
impl<'a> ::std::convert::From<io::Error> for NogoError<'a> {
fn from(_: io::Error) -> Self {
NogoError::new(NogoErrorKind::SystemIOError)
}
}
impl<'a> ::std::convert::From<::std::num::ParseIntError> for NogoError<'a> {
fn from(_: ::std::num::ParseIntError) -> Self {
NogoError::new(NogoErrorKind::ParsingError)
}
}
pub type Result<'a, T> = ::std::result::Result<T, NogoError<'a>>;
pub fn clean_exit() {
println!("\nThank you for playing nogo!\n");
::std::process::exit(0);
}
pub fn exit_with_error(error: NogoError) {
writeln!(io::stderr(), "Error: {}", error.description()).unwrap();
::std::process::exit(error.status());
}
pub fn construct_error(specific: &str, kind: NogoErrorKind) -> NogoError {
let mut err = NogoError::new(kind);
err.set_specific(specific);
err
}