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
mod game;
mod time;
use nom::ErrorKind;
use std::error::Error;
use std::fmt;
use value::GameRecord;
use self::game::game_record;
#[derive(Debug)]
pub enum CsaError {
ParseError(ErrorKind),
}
impl fmt::Display for CsaError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
CsaError::ParseError(ref err) => write!(f, "failed to parse: {}", err.description()),
}
}
}
impl Error for CsaError {
fn description(&self) -> &str {
match *self {
CsaError::ParseError(_) => "failed to parse the csa content",
}
}
fn cause(&self) -> Option<&Error> {
match *self {
CsaError::ParseError(ref err) => Some(err),
}
}
}
impl From<ErrorKind> for CsaError {
fn from(err: ErrorKind) -> CsaError {
CsaError::ParseError(err)
}
}
pub fn parse_csa(s: &str) -> Result<GameRecord, CsaError> {
let record = game_record(s.as_bytes()).to_result()?;
Ok(record)
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs::{self, File};
use std::io::Read;
use std::path::Path;
#[test]
fn load_fixtures() {
let fixtures_dir = Path::new("fixtures/");
let dir_entries = fs::read_dir(fixtures_dir).unwrap();
for entry in dir_entries {
let path = entry.unwrap().path();
if !path.is_file() {
continue;
}
let mut file = File::open(&path).unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).expect(
"failed to load a fixuture content",
);
let res = parse_csa(&contents);
assert_eq!(res.is_ok(), true);
}
}
}