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
use std::path::Path;
use {Error, Parser, Result};
pub struct File {
text: String,
}
macro_rules! ok(
($result:expr) => (
match $result {
Ok(result) => result,
Err(error) => return Err(Error {
line: 0,
column: 0,
message: format!("{:?}", error),
}),
}
);
);
impl File {
pub fn open<T: AsRef<Path>>(path: T) -> Result<File> {
use std::fs;
use std::io::Read;
let mut text = String::new();
let mut file = ok!(fs::File::open(path));
ok!(file.read_to_string(&mut text));
Ok(File { text: text })
}
#[inline]
pub fn parse<'s>(&'s self) -> Parser<'s> {
Parser::new(&self.text)
}
}
#[cfg(test)]
mod tests {
use Event;
use super::File;
use tag::Tag;
use tests::find_fixture;
#[test]
fn parse() {
let file = File::open(find_fixture("benton")).unwrap();
let mut parser = file.parse();
macro_rules! test(
($parser:expr, $matcher:pat) => (
match $parser.next().unwrap() {
$matcher => {},
_ => assert!(false),
}
);
);
test!(parser, Event::Instruction);
test!(parser, Event::Comment);
test!(parser, Event::Declaration);
test!(parser, Event::Tag(Tag::Unknown(..)));
test!(parser, Event::Tag(Tag::Path(..)));
test!(parser, Event::Tag(Tag::Path(..)));
test!(parser, Event::Tag(Tag::Path(..)));
test!(parser, Event::Tag(Tag::Path(..)));
test!(parser, Event::Tag(Tag::Unknown(..)));
assert!(parser.next().is_none());
}
}