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
macro_rules! err {
($( $arg: expr ),*) => {
std::io::Error::new(std::io::ErrorKind::InvalidData, format!($( $arg ),*))
}
}
#[derive(Clone, Copy, Debug)]
pub struct SerializationConfig {
pub enum_names: bool,
}
pub static mut SERIALIZATION_CONFIG: SerializationConfig = SerializationConfig {
enum_names: false,
};
#[macro_use] #[doc(hidden)] pub mod frame_data;
#[macro_use] #[doc(hidden)] pub mod pseudo_bitmask;
#[macro_use] #[doc(hidden)] pub mod pseudo_enum;
pub mod action_state;
pub mod arrow;
pub mod attack;
pub mod buttons;
pub mod character;
pub mod frame;
pub mod game;
pub mod game_parser;
pub mod item;
pub mod metadata;
pub mod parse;
pub mod primitives;
pub mod slippi;
pub mod stage;
pub mod triggers;
pub mod ubjson;
#[cfg(test)] mod test;
use std::{
error,
fmt,
io::{self, Read},
};
#[derive(Debug)]
pub struct ParseError {
pub pos: Option<u64>,
pub error: io::Error,
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(pos) = self.pos {
write!(f, "error parsing game ({}): {}", pos, self.error)
} else {
write!(f, "error parsing game: {}", self.error)
}
}
}
impl error::Error for ParseError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
Some(&self.error)
}
}
struct TrackingReader<R> {
reader: R,
pos: u64,
}
impl<R: Read> Read for TrackingReader<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let result = self.reader.read(buf);
if let Ok(read) = result {
self.pos += read as u64;
}
result
}
}
pub fn parse<R: Read, H: parse::Handlers>(r: &mut R, handlers: &mut H, opts: Option<parse::Opts>) -> std::result::Result<(), ParseError> {
let mut r = TrackingReader {
pos: 0,
reader: r,
};
parse::parse(&mut r, handlers, opts)
.map_err(|e| ParseError { error: e, pos: Some(r.pos) })
}
pub fn game<R: Read>(r: &mut R, opts: Option<parse::Opts>) -> Result<game::Game, ParseError> {
let mut game_parser: game_parser::GameParser = Default::default();
parse(r, &mut game_parser, opts)
.and_then(|_| game_parser.into_game().map_err(|e| ParseError { error: e, pos: None }))
}