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 {
#[inline]
pub fn open(path: &Path) -> Result<File> {
use std::old_io::File as IoFile;
Ok(File {
text: ok!(ok!(IoFile::open(path)).read_to_string()),
})
}
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());
}
}