pgn_reader/visitor.rs
1use shakmaty::{san::SanPlus, Outcome};
2
3use crate::types::{Nag, RawComment, RawHeader, Skip};
4
5/// Consumes games from a reader.
6///
7/// 
8pub trait Visitor {
9 /// Value produced by the visitor after reading a game.
10 type Result;
11
12 /// Called at the start of the game.
13 fn begin_game(&mut self) {}
14
15 /// Called directly before reading game headers.
16 fn begin_headers(&mut self) {}
17 /// Called when parsing a game header like `[White "Deep Blue"]`.
18 fn header(&mut self, _key: &[u8], _value: RawHeader<'_>) {}
19 /// Called after reading the headers of a game. May skip quickly over
20 /// the following move text directly to
21 /// [`end_game()`](trait.Visitor.html#tymethod.end_game).
22 fn end_headers(&mut self) -> Skip {
23 Skip(false)
24 }
25
26 /// Called for each move, like `Nf3+`.
27 fn san(&mut self, _san_plus: SanPlus) {}
28 /// Called for each numeric annotation glyph like `!?` or `$7`.
29 fn nag(&mut self, _nag: Nag) {}
30 /// Called for each `{ comment }`.
31 fn comment(&mut self, _comment: RawComment<'_>) {}
32 /// Called for each `(`. May skip over the following variation directly
33 /// to [`end_variation()`](trait.Visitor.html#method.end_variation) (or to
34 /// [`end_game()`](trait.Visitor.html#tymethod.end_game) if no matching `)`
35 /// follows before the end of the game.
36 fn begin_variation(&mut self) -> Skip {
37 Skip(false)
38 }
39 /// Called for each `)`. It is *not* guaranteed that there was a
40 /// matching `(`.
41 fn end_variation(&mut self) {}
42 /// Called for each game termination, like `*` or `1-0`.
43 fn outcome(&mut self, _outcome: Option<Outcome>) {}
44
45 /// Called after parsing a game. Can produce a custom result.
46 fn end_game(&mut self) -> Self::Result;
47}
48
49pub(crate) struct SkipVisitor;
50
51impl Visitor for SkipVisitor {
52 type Result = ();
53
54 fn end_headers(&mut self) -> Skip {
55 Skip(true)
56 }
57 fn begin_variation(&mut self) -> Skip {
58 Skip(true)
59 }
60 fn end_game(&mut self) {}
61}