rsshogi-csa 0.2.0

Engine-agnostic CSA shogi server protocol command model, parser, and formatter
Documentation
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
//! Reading and writing the `BEGIN Game_Summary` block.
//!
//! The block is not a single line, so it does not go through
//! [`parse_line`](crate::parse_line). Feed its lines to a [`GameSummaryReader`]
//! instead, starting with `BEGIN Game_Summary` itself. The reader is a nested
//! state machine: `BEGIN Time`, `BEGIN Time+`, `BEGIN Time-`, and
//! `BEGIN Position` each push a sub-state, which is what lets the position body
//! be captured verbatim instead of being mistaken for commands.

use std::mem;

use crate::error::{ParseError, ParseErrorKind};
use crate::model::{Color, GameSummary, TimeConfig, TimeScale, TimeUnit};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum State {
    AwaitBegin,
    Summary,
    Time(Option<Color>),
    Position,
    Done,
}

/// Assembles a [`GameSummary`] from the lines of one `BEGIN Game_Summary` block.
///
/// ```
/// use rsshogi_csa::GameSummaryReader;
///
/// let block = "\
/// BEGIN Game_Summary
/// Protocol_Version:1.2
/// Your_Turn:+
/// BEGIN Time
/// Time_Unit:1sec
/// Total_Time:900
/// END Time
/// BEGIN Position
/// PI
/// +
/// END Position
/// END Game_Summary";
///
/// let mut reader = GameSummaryReader::new();
/// let mut summary = None;
/// for line in block.lines() {
///     if let Some(done) = reader.feed(line).unwrap() {
///         summary = Some(done);
///     }
/// }
///
/// let summary = summary.expect("block is complete");
/// assert_eq!(summary.time_black.total_time, Some(900));
/// assert_eq!(summary.position_lines, vec!["PI".to_string(), "+".to_string()]);
/// ```
#[derive(Debug, Clone)]
pub struct GameSummaryReader {
    state: State,
    summary: GameSummary,
}

impl GameSummaryReader {
    /// A reader waiting for `BEGIN Game_Summary`.
    #[must_use]
    pub fn new() -> Self {
        Self { state: State::AwaitBegin, summary: GameSummary::default() }
    }

    /// Whether the block has been closed by `END Game_Summary`.
    #[must_use]
    pub const fn is_complete(&self) -> bool {
        matches!(self.state, State::Done)
    }

    /// Feeds one line, returning the summary once `END Game_Summary` arrives.
    ///
    /// Lines inside `BEGIN Position` are stored verbatim apart from trailing
    /// newline characters, because board rows carry significant trailing spaces.
    pub fn feed(&mut self, raw: &str) -> Result<Option<GameSummary>, ParseError> {
        let line = raw.trim_end_matches(['\r', '\n']);
        match self.state {
            State::AwaitBegin => {
                if line.trim() == "BEGIN Game_Summary" {
                    self.state = State::Summary;
                    Ok(None)
                } else {
                    Err(ParseError::new(ParseErrorKind::UnexpectedLine, line))
                }
            }
            State::Summary => self.feed_summary(line),
            State::Time(color) => self.feed_time(line, color),
            State::Position => {
                if line.trim() == "END Position" {
                    self.state = State::Summary;
                } else if line.trim() == "END Game_Summary" {
                    return Err(ParseError::new(ParseErrorKind::UnexpectedLine, line));
                } else {
                    self.summary.position_lines.push(line.to_owned());
                }
                Ok(None)
            }
            State::Done => Err(ParseError::new(ParseErrorKind::UnexpectedLine, line)),
        }
    }

    fn feed_summary(&mut self, line: &str) -> Result<Option<GameSummary>, ParseError> {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            return Ok(None);
        }
        match trimmed {
            "END Game_Summary" => {
                self.state = State::Done;
                return Ok(Some(mem::take(&mut self.summary)));
            }
            "BEGIN Time" => self.state = State::Time(None),
            "BEGIN Time+" => self.state = State::Time(Some(Color::Black)),
            "BEGIN Time-" => self.state = State::Time(Some(Color::White)),
            "BEGIN Position" => self.state = State::Position,
            _ => {
                let (key, value) = split_field(trimmed)?;
                self.set_summary_field(trimmed, key, value)?;
            }
        }
        Ok(None)
    }

    fn feed_time(
        &mut self,
        line: &str,
        color: Option<Color>,
    ) -> Result<Option<GameSummary>, ParseError> {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            return Ok(None);
        }
        if trimmed == time_terminator(color) {
            self.state = State::Summary;
            return Ok(None);
        }
        let (key, value) = split_field(trimmed)?;
        self.set_time_field(trimmed, key, value, color)?;
        Ok(None)
    }

    fn set_summary_field(&mut self, line: &str, key: &str, value: &str) -> Result<(), ParseError> {
        let summary = &mut self.summary;
        match key {
            "Protocol_Version" => summary.protocol_version = Some(value.to_owned()),
            "Protocol_Mode" => summary.protocol_mode = Some(value.to_owned()),
            "Format" => summary.format = Some(value.to_owned()),
            "Declaration" => summary.declaration = Some(value.to_owned()),
            "Game_ID" => summary.game_id = Some(value.to_owned()),
            "Name+" => summary.name_black = Some(value.to_owned()),
            "Name-" => summary.name_white = Some(value.to_owned()),
            "Your_Turn" => summary.your_turn = Some(parse_color(line, "Your_Turn", value)?),
            "To_Move" => summary.to_move = Some(parse_color(line, "To_Move", value)?),
            "Rematch_On_Draw" => summary.rematch_on_draw = Some(value.to_owned()),
            "Max_Moves" => summary.max_moves = Some(parse_u32(line, "Max_Moves", value)?),
            _ => summary.extras.push((key.to_owned(), value.to_owned())),
        }
        Ok(())
    }

    fn set_time_field(
        &mut self,
        line: &str,
        key: &str,
        value: &str,
        color: Option<Color>,
    ) -> Result<(), ParseError> {
        match key {
            "Time_Unit" => {
                let unit = parse_time_unit(line, value)?;
                self.apply_time(color, |config| config.time_unit = Some(unit));
            }
            "Total_Time" => {
                let parsed = parse_u64(line, "Total_Time", value)?;
                self.apply_time(color, |config| config.total_time = Some(parsed));
            }
            "Byoyomi" => {
                let parsed = parse_u64(line, "Byoyomi", value)?;
                self.apply_time(color, |config| config.byoyomi = Some(parsed));
            }
            "Delay" => {
                let parsed = parse_u64(line, "Delay", value)?;
                self.apply_time(color, |config| config.delay = Some(parsed));
            }
            "Increment" => {
                let parsed = parse_u64(line, "Increment", value)?;
                self.apply_time(color, |config| config.increment = Some(parsed));
            }
            "Least_Time_Per_Move" => {
                let parsed = parse_u64(line, "Least_Time_Per_Move", value)?;
                self.apply_time(color, |config| config.least_time_per_move = Some(parsed));
            }
            "Time_Roundup" => {
                let parsed = parse_yes_no(line, "Time_Roundup", value)?;
                self.apply_time(color, |config| config.time_roundup = Some(parsed));
            }
            "Remaining_Time" => {
                let parsed = parse_u64(line, "Remaining_Time", value)?;
                self.apply_time(color, |config| config.remaining_time = Some(parsed));
            }
            "Remaining_Time+" => {
                let parsed = parse_u64(line, "Remaining_Time+", value)?;
                self.summary.time_black.remaining_time = Some(parsed);
            }
            "Remaining_Time-" => {
                let parsed = parse_u64(line, "Remaining_Time-", value)?;
                self.summary.time_white.remaining_time = Some(parsed);
            }
            "Last_Move" => self.summary.last_move = Some(value.to_owned()),
            "Current_Turn" => {
                self.summary.current_turn = Some(parse_u32(line, "Current_Turn", value)?);
            }
            _ => {
                let entry = (key.to_owned(), value.to_owned());
                self.apply_time(color, |config| config.extras.push(entry.clone()));
            }
        }
        Ok(())
    }

    fn apply_time(&mut self, color: Option<Color>, mut apply: impl FnMut(&mut TimeConfig)) {
        match color {
            Some(Color::Black) => apply(&mut self.summary.time_black),
            Some(Color::White) => apply(&mut self.summary.time_white),
            None => {
                apply(&mut self.summary.time_black);
                apply(&mut self.summary.time_white);
            }
        }
    }
}

const fn time_terminator(color: Option<Color>) -> &'static str {
    match color {
        None => "END Time",
        Some(Color::Black) => "END Time+",
        Some(Color::White) => "END Time-",
    }
}

impl Default for GameSummaryReader {
    fn default() -> Self {
        Self::new()
    }
}

impl TimeConfig {
    /// Whether no field of this time control was declared.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        *self == Self::default()
    }

    fn push_body(&self, out: &mut Vec<String>) {
        push_field(out, "Time_Unit", self.time_unit.map(|unit| unit.to_string()));
        push_field(out, "Total_Time", self.total_time.map(|v| v.to_string()));
        push_field(out, "Byoyomi", self.byoyomi.map(|v| v.to_string()));
        push_field(out, "Delay", self.delay.map(|v| v.to_string()));
        push_field(out, "Increment", self.increment.map(|v| v.to_string()));
        push_field(out, "Least_Time_Per_Move", self.least_time_per_move.map(|v| v.to_string()));
        push_field(
            out,
            "Time_Roundup",
            self.time_roundup.map(|v| if v { "YES" } else { "NO" }.to_owned()),
        );
        for (key, value) in &self.extras {
            out.push(format!("{key}:{value}"));
        }
    }
}

impl GameSummary {
    /// Renders the summary as protocol lines, without trailing newlines.
    ///
    /// Field order follows `shogi-server`, so a proposal parsed from it renders
    /// back byte for byte. A block that declared identical per-color time
    /// sections is re-emitted as one shared `BEGIN Time` section.
    #[must_use]
    pub fn to_lines(&self) -> Vec<String> {
        let mut out = vec!["BEGIN Game_Summary".to_owned()];
        push_field(&mut out, "Protocol_Version", self.protocol_version.clone());
        push_field(&mut out, "Protocol_Mode", self.protocol_mode.clone());
        push_field(&mut out, "Format", self.format.clone());
        push_field(&mut out, "Declaration", self.declaration.clone());
        push_field(&mut out, "Game_ID", self.game_id.clone());
        push_field(&mut out, "Name+", self.name_black.clone());
        push_field(&mut out, "Name-", self.name_white.clone());
        push_field(&mut out, "Your_Turn", self.your_turn.map(|c| c.to_string()));
        push_field(&mut out, "Rematch_On_Draw", self.rematch_on_draw.clone());
        push_field(&mut out, "To_Move", self.to_move.map(|c| c.to_string()));
        push_field(&mut out, "Max_Moves", self.max_moves.map(|v| v.to_string()));
        for (key, value) in &self.extras {
            out.push(format!("{key}:{value}"));
        }

        self.push_time_section(&mut out);

        if !self.position_lines.is_empty() {
            out.push("BEGIN Position".to_owned());
            out.extend(self.position_lines.iter().cloned());
            out.push("END Position".to_owned());
        }

        out.push("END Game_Summary".to_owned());
        out
    }

    fn push_time_section(&self, out: &mut Vec<String>) {
        if self.time_black.is_empty() && self.time_white.is_empty() {
            return;
        }

        if self.time_black.eq_ignoring_remaining(&self.time_white) {
            out.push("BEGIN Time".to_owned());
            self.time_black.push_body(out);
            push_field(
                out,
                "Remaining_Time+",
                self.time_black.remaining_time.map(|v| v.to_string()),
            );
            push_field(
                out,
                "Remaining_Time-",
                self.time_white.remaining_time.map(|v| v.to_string()),
            );
            self.push_show_fields(out);
            out.push("END Time".to_owned());
            return;
        }

        out.push("BEGIN Time+".to_owned());
        self.time_black.push_body(out);
        push_field(out, "Remaining_Time+", self.time_black.remaining_time.map(|v| v.to_string()));
        self.push_show_fields(out);
        out.push("END Time+".to_owned());

        out.push("BEGIN Time-".to_owned());
        self.time_white.push_body(out);
        push_field(out, "Remaining_Time-", self.time_white.remaining_time.map(|v| v.to_string()));
        out.push("END Time-".to_owned());
    }

    fn push_show_fields(&self, out: &mut Vec<String>) {
        push_field(out, "Last_Move", self.last_move.clone());
        push_field(out, "Current_Turn", self.current_turn.map(|v| v.to_string()));
    }
}

fn push_field(out: &mut Vec<String>, key: &str, value: Option<String>) {
    if let Some(value) = value {
        out.push(format!("{key}:{value}"));
    }
}

fn split_field(line: &str) -> Result<(&str, &str), ParseError> {
    line.split_once(':').ok_or_else(|| ParseError::new(ParseErrorKind::UnexpectedLine, line))
}

fn parse_color(line: &str, key: &'static str, value: &str) -> Result<Color, ParseError> {
    let mut chars = value.chars();
    let (Some(first), None) = (chars.next(), chars.next()) else {
        return Err(ParseError::new(ParseErrorKind::InvalidField(key), line));
    };
    Color::from_csa(first).ok_or_else(|| ParseError::new(ParseErrorKind::InvalidField(key), line))
}

fn parse_u64(line: &str, key: &'static str, value: &str) -> Result<u64, ParseError> {
    value.parse().map_err(|_| ParseError::new(ParseErrorKind::InvalidNumber(key), line))
}

fn parse_u32(line: &str, key: &'static str, value: &str) -> Result<u32, ParseError> {
    value.parse().map_err(|_| ParseError::new(ParseErrorKind::InvalidNumber(key), line))
}

fn parse_yes_no(line: &str, key: &'static str, value: &str) -> Result<bool, ParseError> {
    match value.to_ascii_uppercase().as_str() {
        "YES" | "TRUE" | "1" => Ok(true),
        "NO" | "FALSE" | "0" => Ok(false),
        _ => Err(ParseError::new(ParseErrorKind::InvalidField(key), line)),
    }
}

fn parse_time_unit(line: &str, value: &str) -> Result<TimeUnit, ParseError> {
    const SCALES: [(&str, TimeScale); 3] =
        [("msec", TimeScale::Msec), ("sec", TimeScale::Sec), ("min", TimeScale::Min)];

    for (suffix, scale) in SCALES {
        let Some(head) = value.strip_suffix(suffix) else {
            continue;
        };
        let count = if head.is_empty() {
            1
        } else {
            head.parse()
                .map_err(|_| ParseError::new(ParseErrorKind::InvalidNumber("Time_Unit"), line))?
        };
        return Ok(TimeUnit { count, scale });
    }
    Err(ParseError::new(ParseErrorKind::InvalidField("Time_Unit"), line))
}

#[cfg(test)]
mod tests {
    use super::*;

    const PROPOSAL: &str = "\
BEGIN Game_Summary
Protocol_Version:1.2
Protocol_Mode:Server
Format:Shogi 1.0
Declaration:Jishogi 1.1
Game_ID:wdoor+floodgate-900-0+black+white+20260804120000
Name+:black
Name-:white
Your_Turn:+
Rematch_On_Draw:NO
To_Move:+
Max_Moves:256
BEGIN Time
Time_Unit:1sec
Total_Time:900
Byoyomi:0
Increment:0
Least_Time_Per_Move:0
END Time
BEGIN Position
P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
+
END Position
END Game_Summary";

    fn read(block: &str) -> GameSummary {
        let mut reader = GameSummaryReader::new();
        let mut result = None;
        for line in block.lines() {
            let done = reader
                .feed(line)
                .unwrap_or_else(|err| panic!("`{line}` should be accepted: {err}"));
            if let Some(summary) = done {
                result = Some(summary);
            }
        }
        assert!(reader.is_complete(), "reader should reach END Game_Summary");
        result.expect("block should produce a summary")
    }

    #[test]
    fn proposal_round_trips_byte_for_byte() {
        let summary = read(PROPOSAL);
        assert_eq!(summary.to_lines().join("\n"), PROPOSAL);
    }

    #[test]
    fn proposal_fields_are_typed() {
        let summary = read(PROPOSAL);
        assert_eq!(summary.protocol_version.as_deref(), Some("1.2"));
        assert_eq!(summary.your_turn, Some(Color::Black));
        assert_eq!(summary.to_move, Some(Color::Black));
        assert_eq!(summary.max_moves, Some(256));
        assert_eq!(summary.name_black.as_deref(), Some("black"));
        assert_eq!(summary.time_black.unit(), TimeUnit::SECOND);
        assert_eq!(summary.time_black.total_time, Some(900));
        assert_eq!(summary.time_white.total_time, Some(900));
        assert_eq!(summary.position_lines.len(), 3);
    }

    #[test]
    fn position_body_keeps_significant_trailing_spaces() {
        // The board row is built here rather than written inline, because a
        // trailing space in a source literal does not survive editors or hooks.
        let row = format!("P2 * -HI *  *  *  *  * -KA *{}", ' ');
        let block =
            format!("BEGIN Game_Summary\nBEGIN Position\n{row}\n+\nEND Position\nEND Game_Summary");
        let summary = read(&block);
        assert_eq!(summary.position_lines[0], row);
        assert_eq!(summary.to_lines().join("\n"), block);
    }

    #[test]
    fn per_color_time_blocks_are_kept_apart() {
        let block = "\
BEGIN Game_Summary
BEGIN Time+
Time_Unit:1sec
Total_Time:600
END Time+
BEGIN Time-
Time_Unit:1sec
Total_Time:900
END Time-
END Game_Summary";
        let summary = read(block);
        assert_eq!(summary.time_black.total_time, Some(600));
        assert_eq!(summary.time_white.total_time, Some(900));
        assert_eq!(summary.to_lines().join("\n"), block);
    }

    #[test]
    fn time_blocks_require_matching_terminators() {
        for (begin, end) in
            [("BEGIN Time", "END Time+"), ("BEGIN Time+", "END Time-"), ("BEGIN Time-", "END Time")]
        {
            let mut reader = GameSummaryReader::new();
            reader.feed("BEGIN Game_Summary").expect("begin summary");
            reader.feed(begin).expect("begin time block");
            assert!(reader.feed(end).is_err(), "{begin} must reject {end}");
        }

        for (begin, end, black, white) in [
            ("BEGIN Time", "END Time", Some(60), Some(60)),
            ("BEGIN Time+", "END Time+", Some(60), None),
            ("BEGIN Time-", "END Time-", None, Some(60)),
        ] {
            let summary = read(&format!(
                "BEGIN Game_Summary\n{begin}\nTotal_Time:60\n{end}\nEND Game_Summary"
            ));
            assert_eq!(summary.time_black.total_time, black);
            assert_eq!(summary.time_white.total_time, white);
        }
    }

    #[test]
    fn position_block_must_end_before_the_summary() {
        let mut reader = GameSummaryReader::new();
        reader.feed("BEGIN Game_Summary").expect("begin summary");
        reader.feed("BEGIN Position").expect("begin position");
        reader.feed("PI").expect("position line");
        assert!(reader.feed("END Game_Summary").is_err());
    }

    #[test]
    fn show_output_round_trips() {
        let block = "\
BEGIN Game_Summary
Protocol_Version:1.2
Game_ID:game-1
Rematch_On_Draw:NO
To_Move:+
Max_Moves:256
BEGIN Time
Time_Unit:1sec
Total_Time:1500
Byoyomi:0
Increment:0
Least_Time_Per_Move:0
Remaining_Time+:1400
Remaining_Time-:1450
Last_Move:+7776FU,T12
Current_Turn:2
END Time
END Game_Summary";
        let summary = read(block);
        assert_eq!(summary.time_black.remaining_time, Some(1400));
        assert_eq!(summary.time_white.remaining_time, Some(1450));
        assert_eq!(summary.last_move.as_deref(), Some("+7776FU,T12"));
        assert_eq!(summary.current_turn, Some(2));
        assert_eq!(summary.to_lines().join("\n"), block);
    }

    #[test]
    fn resumed_position_carries_moves() {
        let block = "\
BEGIN Game_Summary
Your_Turn:-
BEGIN Position
PI
+
+7776FU,T2
-3334FU,T5
END Position
END Game_Summary";
        let summary = read(block);
        let (board, moves) = summary.position_split();
        assert_eq!(board, ["PI".to_owned(), "+".to_owned()]);
        assert_eq!(moves, ["+7776FU,T2".to_owned(), "-3334FU,T5".to_owned()]);
    }

    #[test]
    fn unknown_fields_are_preserved_in_order() {
        let block = "\
BEGIN Game_Summary
Protocol_Version:1.2
Future_Field:whatever
BEGIN Time
Total_Time:60
Future_Time_Field:7
END Time
END Game_Summary";
        let summary = read(block);
        assert_eq!(summary.extras, [("Future_Field".to_owned(), "whatever".to_owned())]);
        assert_eq!(summary.time_black.extras, [("Future_Time_Field".to_owned(), "7".to_owned())]);
        assert_eq!(summary.to_lines().join("\n"), block);
    }

    #[test]
    fn time_unit_variants() {
        assert_eq!(
            parse_time_unit("Time_Unit:1sec", "1sec").expect("1sec"),
            TimeUnit { count: 1, scale: TimeScale::Sec }
        );
        assert_eq!(
            parse_time_unit("Time_Unit:1min", "1min").expect("1min"),
            TimeUnit { count: 1, scale: TimeScale::Min }
        );
        assert_eq!(
            parse_time_unit("Time_Unit:500msec", "500msec").expect("500msec"),
            TimeUnit { count: 500, scale: TimeScale::Msec }
        );
        assert!(parse_time_unit("Time_Unit:1hour", "1hour").is_err());
        assert!(parse_time_unit("Time_Unit:xsec", "xsec").is_err());
    }

    #[test]
    fn reader_rejects_lines_outside_the_block() {
        let mut reader = GameSummaryReader::new();
        assert!(reader.feed("Protocol_Version:1.2").is_err());

        let mut reader = GameSummaryReader::new();
        reader.feed("BEGIN Game_Summary").expect("begin");
        assert!(reader.feed("not a field").is_err());
    }

    #[test]
    fn reader_rejects_lines_after_the_block() {
        let mut reader = GameSummaryReader::new();
        reader.feed("BEGIN Game_Summary").expect("begin");
        reader.feed("END Game_Summary").expect("end");
        assert!(reader.feed("Protocol_Version:1.2").is_err());
    }

    #[test]
    fn malformed_numbers_are_reported() {
        let mut reader = GameSummaryReader::new();
        reader.feed("BEGIN Game_Summary").expect("begin");
        assert!(reader.feed("Max_Moves:many").is_err());
        assert!(reader.feed("Your_Turn:?").is_err());
    }
}