jomini 0.34.1

Low level, performance oriented parser for save and game files from EU4, CK3, HOI4, Vic3, Imperator, and other PDS titles
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
use super::{
    LexError, LexemeId, LexerError, Token,
    lexer::{read_id, read_rgb, read_string},
};
use crate::{
    Scalar,
    binary::{Rgb, lexer::TokenKind},
    buffer::{BufferError, BufferWindow, BufferWindowBuilder},
    util::get_split,
};
use std::{fmt, io::Read};

/// [Lexer](crate::binary::Lexer) that works over a [Read] implementation
///
/// Example of computing the max nesting depth using a [TokenReader].
///
/// ```rust
/// use jomini::binary::{TokenReader, Token};
/// let data = [0x2d, 0x28, 0x01, 0x00, 0x03, 0x00, 0x03, 0x00, 0x04, 0x00, 0x04, 0x00];
/// let mut reader = TokenReader::new(&data[..]);
/// let mut max_depth = 0;
/// let mut current_depth = 0;
/// while let Some(token) = reader.next()? {
///   match token {
///     Token::Open => {
///       current_depth += 1;
///       max_depth = max_depth.max(current_depth);
///     }
///     Token::Close => current_depth -= 1,
///     _ => {}
///   }
/// }
/// assert_eq!(max_depth, 2);
/// # Ok::<(), jomini::binary::ReaderError>(())
/// ```
///
/// The tokens yielded from a [TokenReader] are not fully parsed. Some things to
/// be aware of:
///
/// - Ghost objects will not be skipped (eg: `foo={ {} a=b }`).
/// - [TokenReader] can not inform the caller if the container is an array or
///   object (or neither).
///
///  This is a much more raw view of the data that can be used to construct
/// higher level parsers, melters, and deserializers that operate over a stream
/// of data.
///
/// [TokenReader] operates over a fixed size buffer, so using a
/// [BufRead](std::io::BufRead) affords no benefits. An error will be returned
/// for tokens that are impossible to fit within the buffer (eg: if the provided
/// with 100 byte buffer but there is a binary string that is 101 bytes long).
#[derive(Debug)]
pub struct TokenReader<R> {
    reader: R,
    buf: BufferWindow,
    data: [u8; 8],
}

impl TokenReader<()> {
    /// Read from a byte slice without memcpy's
    #[inline]
    pub fn from_slice(data: &[u8]) -> TokenReader<&'_ [u8]> {
        TokenReader {
            reader: data,
            buf: BufferWindow::from_slice(data),
            data: [0; 8],
        }
    }
}

impl<R> TokenReader<R>
where
    R: Read,
{
    /// Convenience method for constructing the default token reader
    #[inline]
    pub fn new(reader: R) -> Self {
        TokenReader::builder().build(reader)
    }

    /// Returns the byte position of the data stream that has been processed.
    ///
    /// ```rust
    /// use jomini::binary::{TokenReader, Token};
    /// let mut reader = TokenReader::new(&[0xd2, 0x28, 0xff][..]);
    /// assert_eq!(reader.read().unwrap(), Token::Id(0x28d2));
    /// assert_eq!(reader.position(), 2);
    /// ```
    #[inline]
    pub fn position(&self) -> usize {
        self.buf.position()
    }

    /// Advance a given number of bytes and return them.
    ///
    /// The internal buffer must be large enough to accomodate all bytes.
    ///
    /// ```rust
    /// use jomini::binary::{TokenReader, LexError, ReaderErrorKind};
    /// let mut reader = TokenReader::new(&b"EU4bin"[..]);
    /// assert_eq!(reader.read_bytes(6).unwrap(), &b"EU4bin"[..]);
    /// assert!(matches!(reader.read_bytes(1).unwrap_err().kind(), ReaderErrorKind::Lexer(LexError::Eof)));
    /// ```
    #[inline]
    pub fn read_bytes(&mut self, bytes: usize) -> Result<&[u8], ReaderError> {
        while self.buf.window_len() < bytes {
            match self.buf.fill_buf(&mut self.reader) {
                Ok(0) => return Err(self.lex_error(LexError::Eof)),
                Ok(_) => {}
                Err(e) => return Err(self.buffer_error(e)),
            }
        }

        Ok(self.buf.split(bytes))
    }

    /// Advance through the containing block until the closing token is consumed
    ///
    /// ```rust
    /// use jomini::binary::{TokenReader, Token};
    /// let mut reader = TokenReader::new(&[
    ///     0xd2, 0x28, 0x01, 0x00, 0x03, 0x00, 0x03, 0x00,
    ///     0x04, 0x00, 0x04, 0x00, 0xff, 0xff
    /// ][..]);
    /// assert_eq!(reader.read().unwrap(), Token::Id(0x28d2));
    /// assert_eq!(reader.read().unwrap(), Token::Equal);
    /// assert_eq!(reader.read().unwrap(), Token::Open);
    /// assert!(reader.skip_container().is_ok());
    /// assert_eq!(reader.read().unwrap(), Token::Id(0xffff));
    /// ```
    #[inline]
    pub fn skip_container(&mut self) -> Result<(), ReaderError> {
        let mut depth = 1;
        loop {
            let mut window = self.buf.window();
            while let Ok((id, data)) = read_id(window) {
                match id {
                    LexemeId::CLOSE => {
                        depth -= 1;
                        if depth == 0 {
                            self.buf.advance_to(data.as_ptr());
                            return Ok(());
                        }
                        window = data;
                    }
                    LexemeId::OPEN => {
                        window = data;
                        depth += 1
                    }
                    LexemeId::BOOL => match data.get(1..) {
                        Some(d) => window = d,
                        None => break,
                    },
                    LexemeId::F32 | LexemeId::U32 | LexemeId::I32 => match data.get(4..) {
                        Some(d) => window = d,
                        None => break,
                    },
                    LexemeId::F64 | LexemeId::I64 | LexemeId::U64 => match data.get(8..) {
                        Some(d) => window = d,
                        None => break,
                    },
                    LexemeId::QUOTED | LexemeId::UNQUOTED => match read_string(data) {
                        Ok((_, d)) => window = d,
                        Err(_) => break,
                    },
                    LexemeId::LOOKUP_U8 => match data.get(1..) {
                        Some(d) => window = d,
                        None => break,
                    },
                    LexemeId::LOOKUP_U16 => match data.get(2..) {
                        Some(d) => window = d,
                        None => break,
                    },
                    LexemeId::LOOKUP_U8_ALT => match data.get(1..) {
                        Some(d) => window = d,
                        None => break,
                    },
                    LexemeId::LOOKUP_U16_ALT => match data.get(2..) {
                        Some(d) => window = d,
                        None => break,
                    },
                    LexemeId::LOOKUP_U24 => match data.get(3..) {
                        Some(d) => window = d,
                        None => break,
                    },
                    LexemeId::FIXED5_ZERO => window = data,
                    LexemeId::FIXED5_U8 | LexemeId::FIXED5_I8 => match data.get(1..) {
                        Some(d) => window = d,
                        None => break,
                    },
                    LexemeId::FIXED5_U16 | LexemeId::FIXED5_I16 => match data.get(2..) {
                        Some(d) => window = d,
                        None => break,
                    },
                    LexemeId::FIXED5_U24 | LexemeId::FIXED5_I24 => match data.get(3..) {
                        Some(d) => window = d,
                        None => break,
                    },
                    LexemeId::FIXED5_U32 | LexemeId::FIXED5_I32 => match data.get(4..) {
                        Some(d) => window = d,
                        None => break,
                    },
                    LexemeId::FIXED5_U40 | LexemeId::FIXED5_I40 => match data.get(5..) {
                        Some(d) => window = d,
                        None => break,
                    },
                    LexemeId::FIXED5_U48 | LexemeId::FIXED5_I48 => match data.get(6..) {
                        Some(d) => window = d,
                        None => break,
                    },
                    LexemeId::FIXED5_U56 | LexemeId::FIXED5_I56 => match data.get(7..) {
                        Some(d) => window = d,
                        None => break,
                    },
                    _ => window = data,
                }
            }

            self.buf.advance_to(window.as_ptr());
            match self.buf.fill_buf(&mut self.reader) {
                Ok(0) => return Err(self.lex_error(LexError::Eof)),
                Ok(_) => {}
                Err(e) => return Err(self.buffer_error(e)),
            }
        }
    }

    /// Consume the token reader and return the internal buffer and reader. This
    /// allows the buffer to be reused.
    ///
    /// ```rust
    /// use jomini::binary::TokenReader;
    /// let data = b"EU4bin";
    /// let mut reader = TokenReader::new(&data[..]);
    /// assert_eq!(reader.read_bytes(6).unwrap(), &data[..]);
    ///
    /// let (buf, _) = reader.into_parts();
    /// let data = b"HOI4bin";
    /// let mut reader = TokenReader::builder().buffer(buf).build(&data[..]);
    /// assert_eq!(reader.read_bytes(7).unwrap(), &data[..]);
    /// ```
    #[inline]
    pub fn into_parts(self) -> (Box<[u8]>, R) {
        (self.buf.buf, self.reader)
    }

    /// Read the next token in the stream. Will error if not enough data remains
    /// to decode a token.
    ///
    /// ```rust
    /// use jomini::binary::{TokenReader, Token, ReaderErrorKind, LexError};
    /// let mut reader = TokenReader::new(&[
    ///     0xd2, 0x28, 0x01, 0x00, 0x03, 0x00, 0x04, 0x00
    /// ][..]);
    /// assert_eq!(reader.read().unwrap(), Token::Id(0x28d2));
    /// assert_eq!(reader.read().unwrap(), Token::Equal);
    /// assert_eq!(reader.read().unwrap(), Token::Open);
    /// assert_eq!(reader.read().unwrap(), Token::Close);
    /// assert!(matches!(reader.read().unwrap_err().kind(), ReaderErrorKind::Lexer(LexError::Eof)));
    /// ```
    #[inline]
    pub fn read(&mut self) -> Result<Token<'_>, ReaderError> {
        let s = std::ptr::addr_of!(self);
        self.next()?
            .ok_or_else(|| unsafe { s.read().lex_error(LexError::Eof) })
    }

    fn refill_with<T>(
        &mut self,
        f: impl FnOnce(&mut Self) -> Result<Option<T>, ReaderError>,
    ) -> Result<Option<T>, ReaderError> {
        match self.buf.fill_buf(&mut self.reader) {
            Ok(0) if self.buf.window_len() == 0 => Ok(None),
            Ok(0) => Err(self.lex_error(LexError::Eof)),
            Ok(_) => f(self),
            Err(e) => Err(self.buffer_error(e)),
        }
    }

    /// Read a token, returning none when all the data has been consumed
    ///
    /// ```rust
    /// use jomini::binary::{TokenReader, Token};
    /// let mut reader = TokenReader::new(&[
    ///     0xd2, 0x28, 0x01, 0x00, 0x03, 0x00, 0x04, 0x00
    /// ][..]);
    /// assert_eq!(reader.next().unwrap(), Some(Token::Id(0x28d2)));
    /// assert_eq!(reader.next().unwrap(), Some(Token::Equal));
    /// assert_eq!(reader.next().unwrap(), Some(Token::Open));
    /// assert_eq!(reader.next().unwrap(), Some(Token::Close));
    /// assert_eq!(reader.next().unwrap(), None);
    /// ```
    #[inline]
    #[allow(clippy::should_implement_trait)]
    pub fn next(&mut self) -> Result<Option<Token<'_>>, ReaderError> {
        match self.next_token()? {
            Some(kind) => Ok(Some(self.token_from_kind(kind))),
            None => Ok(None),
        }
    }

    /// Construct a [Token] from a [TokenKind] using stored data
    #[inline]
    fn token_from_kind(&self, kind: TokenKind) -> Token<'_> {
        match kind {
            TokenKind::Open => Token::Open,
            TokenKind::Close => Token::Close,
            TokenKind::Equal => Token::Equal,
            TokenKind::U32 => Token::U32(self.u32_data()),
            TokenKind::U64 => Token::U64(self.u64_data()),
            TokenKind::I32 => Token::I32(self.i32_data()),
            TokenKind::Bool => Token::Bool(self.bool_data()),
            TokenKind::Quoted => Token::Quoted(unsafe { self.scalar_data() }),
            TokenKind::Unquoted => Token::Unquoted(unsafe { self.scalar_data() }),
            TokenKind::F32 => Token::F32(self.f32_data()),
            TokenKind::F64 => Token::F64(self.f64_data()),
            TokenKind::Rgb => Token::Rgb(self.rgb_data()),
            TokenKind::I64 => Token::I64(self.i64_data()),
            TokenKind::Lookup => Token::Lookup(self.lookup_data()),
            TokenKind::Id => Token::Id(self.token_id()),
        }
    }

    /// Return the id associated with the last [`TokenKind::Id`] token.
    #[inline]
    pub fn token_id(&self) -> u16 {
        u16::from_le_bytes([self.data[0], self.data[1]])
    }

    /// Return the scalar data associated with [`TokenKind::Quoted`] and
    /// [`TokenKind::Unquoted`].
    ///
    /// # Safety
    ///
    /// It is undefined behavior if this method is called and the previous
    /// [`Self::next_token`] or [`Self::read_token`] did not return [`TokenKind::Quoted`] or
    /// [`TokenKind::Unquoted`].
    #[inline]
    pub unsafe fn scalar_data(&self) -> Scalar<'_> {
        let len = u16::from_le_bytes([self.data[0], self.data[1]]);
        let data = unsafe {
            std::slice::from_raw_parts(self.buf.start.byte_sub(len as usize), len as usize)
        };
        Scalar::new(data)
    }

    /// Return the u64 data associated with [`TokenKind::U64`].
    #[inline]
    pub fn u64_data(&self) -> u64 {
        u64::from_le_bytes(self.data)
    }

    /// Return the i64 data associated with [`TokenKind::I64`].
    #[inline]
    pub fn i64_data(&self) -> i64 {
        i64::from_le_bytes(self.data)
    }

    /// Return the f64 data associated with [`TokenKind::F64`].
    #[inline]
    pub fn f64_data(&self) -> [u8; 8] {
        self.data
    }

    /// Return the u32 data associated with [`TokenKind::U32`].
    #[inline]
    pub fn u32_data(&self) -> u32 {
        u32::from_le_bytes([self.data[0], self.data[1], self.data[2], self.data[3]])
    }

    /// Return the i32 data associated with [`TokenKind::I32`].
    #[inline]
    pub fn i32_data(&self) -> i32 {
        i32::from_le_bytes([self.data[0], self.data[1], self.data[2], self.data[3]])
    }

    /// Return the f32 data associated with [`TokenKind::F32`].
    #[inline]
    pub fn f32_data(&self) -> [u8; 4] {
        [self.data[0], self.data[1], self.data[2], self.data[3]]
    }

    /// Return the bool data associated with [`TokenKind::Bool`].
    #[inline]
    pub fn bool_data(&self) -> bool {
        self.data[0] != 0
    }

    /// Return the 32-bit data associated with [`TokenKind::Lookup`].
    #[inline]
    pub fn lookup_data(&self) -> u32 {
        u32::from_le_bytes([self.data[0], self.data[1], self.data[2], 0])
    }

    /// Return the RGB data associated with [`TokenKind::Rgb`].
    ///
    /// # Safety
    ///
    /// It is undefined behavior if this method is called and the previous
    /// [`Self::next_token`] or [`Self::read_token`] did not return [`TokenKind::Rgb`].
    #[inline]
    pub fn rgb_data(&self) -> Rgb {
        let size = self.data[0] as usize;
        let data = unsafe { std::slice::from_raw_parts(self.buf.start.byte_sub(size), size) };
        let (result, _data) = read_rgb(data).expect("valid rgb data");
        result
    }

    #[inline]
    fn next_token_fast(&mut self, window: &[u8]) -> Option<TokenKind> {
        let (id, rest) = get_split::<2>(window).unwrap();
        let lexeme = LexemeId::new(u16::from_le_bytes(*id));
        match lexeme {
            LexemeId::OPEN => {
                self.buf.advance_to(rest.as_ptr());
                Some(TokenKind::Open)
            }
            LexemeId::CLOSE => {
                self.buf.advance_to(rest.as_ptr());
                Some(TokenKind::Close)
            }
            LexemeId::EQUAL => {
                self.buf.advance_to(rest.as_ptr());
                Some(TokenKind::Equal)
            }
            LexemeId::U32 | LexemeId::I32 | LexemeId::F32 => {
                let (data, rest) = rest.split_at(4);
                self.data[..4].copy_from_slice(data);
                self.buf.advance_to(rest.as_ptr());
                if lexeme == LexemeId::F32 {
                    Some(TokenKind::F32)
                } else if lexeme == LexemeId::U32 {
                    Some(TokenKind::U32)
                } else {
                    Some(TokenKind::I32)
                }
            }
            LexemeId::U64 | LexemeId::I64 | LexemeId::F64 => {
                let (data, rest) = rest.split_at(8);
                self.data[..8].copy_from_slice(data);
                self.buf.advance_to(rest.as_ptr());
                if lexeme == LexemeId::F64 {
                    Some(TokenKind::F64)
                } else if lexeme == LexemeId::U64 {
                    Some(TokenKind::U64)
                } else {
                    Some(TokenKind::I64)
                }
            }
            LexemeId::BOOL => {
                let (data, rest) = rest.split_at(1);
                self.data[0] = data[0];
                self.buf.advance_to(rest.as_ptr());
                Some(TokenKind::Bool)
            }
            LexemeId::QUOTED | LexemeId::UNQUOTED => {
                let (len_data, rest) = get_split::<2>(rest).unwrap();
                let len = u16::from_le_bytes(*len_data) as usize;
                let (_str_data, rest) = rest.split_at_checked(len)?;
                self.data[0..2].copy_from_slice(len_data);
                self.buf.advance_to(rest.as_ptr());
                if lexeme == LexemeId::UNQUOTED {
                    Some(TokenKind::Unquoted)
                } else {
                    Some(TokenKind::Quoted)
                }
            }
            LexemeId::LOOKUP_U8 | LexemeId::LOOKUP_U8_ALT => {
                let (data, rest) = rest.split_at(1);
                let mut tmp = [0u8; 8];
                tmp[0] = data[0];
                self.data = tmp;
                self.buf.advance_to(rest.as_ptr());
                Some(TokenKind::Lookup)
            }
            LexemeId::LOOKUP_U16 | LexemeId::LOOKUP_U16_ALT => {
                let (data, rest) = get_split::<2>(rest).unwrap();
                let mut tmp = [0u8; 8];
                tmp[0..2].copy_from_slice(data);
                self.data = tmp;
                self.buf.advance_to(rest.as_ptr());
                Some(TokenKind::Lookup)
            }
            LexemeId::LOOKUP_U24 => {
                let (data, rest) = get_split::<3>(rest).unwrap();
                let mut tmp = [0u8; 8];
                tmp[0..3].copy_from_slice(data);
                self.data = tmp;
                self.buf.advance_to(rest.as_ptr());
                Some(TokenKind::Lookup)
            }
            LexemeId::RGB => None,
            lexeme if lexeme >= LexemeId::FIXED5_ZERO && lexeme <= LexemeId::FIXED5_I56 => {
                let offset = lexeme.0 - LexemeId::FIXED5_ZERO.0;
                let is_negative = offset > 7;
                let byte_count = offset - (is_negative as u16 * 7);
                let (data, rest) = rest.split_at(byte_count as usize);
                // Use a temporary zero-initialized array to avoid leftover bytes from previous tokens
                let mut buf = [0u8; 8];
                buf[..byte_count as usize].copy_from_slice(data);
                self.buf.advance_to(rest.as_ptr());
                let sign = 1i64 - (is_negative as i64) * 2;
                self.data = (u64::from_le_bytes(buf) as i64 * sign).to_le_bytes();
                Some(TokenKind::F64)
            }
            _ => {
                self.data[..2].copy_from_slice(id);
                self.buf.advance_to(rest.as_ptr());
                Some(TokenKind::Id)
            }
        }
    }

    /// Read the next token in the stream. Will error if not enough data
    /// remains.
    ///
    /// Use one of the `*_data` methods to get the associated data for the
    /// token.
    #[inline]
    pub fn read_token(&mut self) -> Result<TokenKind, ReaderError> {
        let s = std::ptr::addr_of!(self);
        self.next_token()?
            .ok_or_else(|| unsafe { s.read().lex_error(LexError::Eof) })
    }

    fn next_token_slow(&mut self) -> Result<TokenKind, LexError> {
        let window = unsafe { std::slice::from_raw_parts(self.buf.start, self.buf.window_len()) };
        let (id, rest) = get_split::<2>(window).ok_or(LexError::Eof)?;
        let lexeme = LexemeId::new(u16::from_le_bytes(*id));
        match lexeme {
            LexemeId::OPEN => {
                self.buf.advance_to(rest.as_ptr());
                Ok(TokenKind::Open)
            }
            LexemeId::CLOSE => {
                self.buf.advance_to(rest.as_ptr());
                Ok(TokenKind::Close)
            }
            LexemeId::EQUAL => {
                self.buf.advance_to(rest.as_ptr());
                Ok(TokenKind::Equal)
            }
            LexemeId::U32 | LexemeId::I32 | LexemeId::F32 => {
                let (data, rest) = get_split::<4>(rest).ok_or(LexError::Eof)?;
                self.data[..4].copy_from_slice(data);
                self.buf.advance_to(rest.as_ptr());
                if lexeme == LexemeId::F32 {
                    Ok(TokenKind::F32)
                } else if lexeme == LexemeId::U32 {
                    Ok(TokenKind::U32)
                } else {
                    Ok(TokenKind::I32)
                }
            }
            LexemeId::U64 | LexemeId::I64 | LexemeId::F64 => {
                let (data, rest) = get_split::<8>(rest).ok_or(LexError::Eof)?;
                self.data[..8].copy_from_slice(data);
                self.buf.advance_to(rest.as_ptr());
                if lexeme == LexemeId::F64 {
                    Ok(TokenKind::F64)
                } else if lexeme == LexemeId::U64 {
                    Ok(TokenKind::U64)
                } else {
                    Ok(TokenKind::I64)
                }
            }
            LexemeId::BOOL => {
                let (data, rest) = get_split::<1>(rest).ok_or(LexError::Eof)?;
                self.data[0] = data[0];
                self.buf.advance_to(rest.as_ptr());
                Ok(TokenKind::Bool)
            }
            LexemeId::QUOTED | LexemeId::UNQUOTED => {
                let (len_data, rest) = get_split::<2>(rest).ok_or(LexError::Eof)?;
                let len = u16::from_le_bytes(*len_data) as usize;
                let rest = rest.get(len..).ok_or(LexError::Eof)?;
                self.data[0..2].copy_from_slice(len_data);
                self.buf.advance_to(rest.as_ptr());
                if lexeme == LexemeId::UNQUOTED {
                    Ok(TokenKind::Unquoted)
                } else {
                    Ok(TokenKind::Quoted)
                }
            }
            LexemeId::LOOKUP_U8 | LexemeId::LOOKUP_U8_ALT => {
                let (data, rest) = get_split::<1>(rest).ok_or(LexError::Eof)?;
                let mut tmp = [0u8; 8];
                tmp[0] = data[0];
                self.data = tmp;
                self.buf.advance_to(rest.as_ptr());
                Ok(TokenKind::Lookup)
            }
            LexemeId::LOOKUP_U16 | LexemeId::LOOKUP_U16_ALT => {
                let (data, rest) = get_split::<2>(rest).ok_or(LexError::Eof)?;
                let mut tmp = [0u8; 8];
                tmp[0..2].copy_from_slice(data);
                self.data = tmp;
                self.buf.advance_to(rest.as_ptr());
                Ok(TokenKind::Lookup)
            }
            LexemeId::LOOKUP_U24 => {
                let (data, rest) = get_split::<3>(rest).ok_or(LexError::Eof)?;
                let mut tmp = [0u8; 8];
                tmp[0..3].copy_from_slice(data);
                self.data = tmp;
                self.buf.advance_to(rest.as_ptr());
                Ok(TokenKind::Lookup)
            }
            LexemeId::RGB => {
                let (_, nrest) = read_rgb(rest)?;
                let size = nrest.as_ptr() as usize - rest.as_ptr() as usize;
                self.data[0] = size as u8;
                self.buf.advance_to(nrest.as_ptr());
                Ok(TokenKind::Rgb)
            }
            lexeme if lexeme >= LexemeId::FIXED5_ZERO && lexeme <= LexemeId::FIXED5_I56 => {
                let offset = lexeme.0 - LexemeId::FIXED5_ZERO.0;
                let is_negative = offset > 7;
                let byte_count = offset - (is_negative as u16 * 7);
                let (data, rest) = rest
                    .split_at_checked(byte_count as usize)
                    .ok_or(LexError::Eof)?;
                let mut buf = [0u8; 8];
                buf[..byte_count as usize].copy_from_slice(data);
                self.buf.advance_to(rest.as_ptr());
                let sign = 1i64 - (is_negative as i64) * 2;
                self.data = (u64::from_le_bytes(buf) as i64 * sign).to_le_bytes();
                Ok(TokenKind::F64)
            }
            _ => {
                self.data[..2].copy_from_slice(id);
                self.buf.advance_to(rest.as_ptr());
                Ok(TokenKind::Id)
            }
        }
    }

    #[inline(never)]
    fn next_token_slow_refill(&mut self) -> Result<Option<TokenKind>, ReaderError> {
        match self.next_token_slow() {
            Ok(kind) => Ok(Some(kind)),
            Err(LexError::Eof) => self.refill_with(|s| s.next_token()),
            Err(e) => Err(self.lex_error(e)),
        }
    }

    /// Read the next token in the stream. Will return None when all data has
    /// been consumed.
    ///
    /// Use one of the `*_data` methods to get the associated data for the
    /// token.
    #[inline]
    pub fn next_token(&mut self) -> Result<Option<TokenKind>, ReaderError> {
        let window = unsafe { std::slice::from_raw_parts(self.buf.start, self.buf.window_len()) };

        // If we have enough data we can use the fast path to avoid most bound checks
        if window.len() >= 16
            && let Some(kind) = self.next_token_fast(window)
        {
            return Ok(Some(kind));
        }

        self.next_token_slow_refill()
    }

    #[cold]
    #[inline(never)]
    fn buffer_error(&self, e: BufferError) -> ReaderError {
        ReaderError {
            position: self.position(),
            kind: ReaderErrorKind::from(e),
        }
    }

    #[cold]
    #[inline(never)]
    fn lex_error(&self, e: LexError) -> ReaderError {
        ReaderError::from(e.at(self.position()))
    }
}

impl TokenReader<()> {
    /// Initializes a default [TokenReaderBuilder]
    pub fn builder() -> TokenReaderBuilder {
        TokenReaderBuilder::default()
    }
}

/// Creates a binary token reader
#[derive(Debug, Default)]
pub struct TokenReaderBuilder {
    buffer: BufferWindowBuilder,
}

impl TokenReaderBuilder {
    /// Set the fixed size buffer to the given buffer
    ///
    /// See [buffer_len](Self::buffer_len) for more information
    #[inline]
    pub fn buffer(mut self, val: Box<[u8]>) -> TokenReaderBuilder {
        self.buffer = self.buffer.buffer(val);
        self
    }

    /// Set the length of the buffer if no buffer is provided
    ///
    /// The size of the buffer must be large enough to decode an entire binary
    /// token, not just the contained binary data. For instance, for quoted
    /// scalars there are 4 bytes of additional data to the token (2 bytes for
    /// token discriminant and 2 to the string size).
    ///
    /// With how the binary format is laid out, a minimal buffer size that can
    /// handle all inputs can be derived
    ///
    /// ```rust
    /// use jomini::binary::TokenReader;
    /// let len = usize::from(u16::MAX) + 4;
    /// let reader = TokenReader::builder().buffer_len(len);
    /// # let _reader2 = reader;
    /// ```
    #[inline]
    pub fn buffer_len(mut self, val: usize) -> TokenReaderBuilder {
        self.buffer = self.buffer.buffer_len(val);
        self
    }

    /// Create a binary token reader around a given reader.
    #[inline]
    pub fn build<R>(self, reader: R) -> TokenReader<R> {
        let buf = self.buffer.build();
        TokenReader {
            reader,
            buf,
            data: [0; 8],
        }
    }
}

/// The specific binary reader error type.
#[derive(Debug)]
pub enum ReaderErrorKind {
    /// An underlying error from a [Read]er
    Read(std::io::Error),

    /// The internal buffer does not have enough room to store data for the next
    /// token
    BufferFull,

    /// The data is corrupted
    Lexer(LexError),
}

/// An binary lexing error over a `Read` implementation
#[derive(Debug)]
pub struct ReaderError {
    position: usize,
    kind: ReaderErrorKind,
}

impl ReaderError {
    /// Return the byte position where the error occurred
    pub fn position(&self) -> usize {
        self.position
    }

    /// Return a reference the error kind
    pub fn kind(&self) -> &ReaderErrorKind {
        &self.kind
    }

    /// Consume self and return the error kind
    #[must_use]
    pub fn into_kind(self) -> ReaderErrorKind {
        self.kind
    }
}

impl std::error::Error for ReaderError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match &self.kind {
            ReaderErrorKind::Read(cause) => Some(cause),
            _ => None,
        }
    }
}

impl std::fmt::Display for ReaderError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &self.kind {
            ReaderErrorKind::Read { .. } => {
                write!(f, "failed to read past position: {}", self.position)
            }
            ReaderErrorKind::BufferFull => {
                write!(f, "max buffer size exceeded at position: {}", self.position)
            }
            ReaderErrorKind::Lexer(cause) => {
                write!(f, "{} at position: {}", cause, self.position)
            }
        }
    }
}

impl From<LexerError> for ReaderError {
    fn from(value: LexerError) -> Self {
        ReaderError {
            position: value.position(),
            kind: ReaderErrorKind::Lexer(value.into_kind()),
        }
    }
}

impl From<BufferError> for ReaderErrorKind {
    fn from(value: BufferError) -> Self {
        match value {
            BufferError::Io(x) => ReaderErrorKind::Read(x),
            BufferError::BufferFull => ReaderErrorKind::BufferFull,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Scalar, binary::Rgb};
    use rstest::*;

    #[rstest]
    #[case(&[
        Token::Id(0x2838),
        Token::Equal,
        Token::Open,
        Token::Id(0x2863),
        Token::Equal,
        Token::Unquoted(Scalar::new(b"western")),
        Token::Quoted(Scalar::new(b"1446.5.31")),
        Token::Equal,
        Token::Id(0x2838),
        Token::Close,
    ])]
    #[case(&[
        Token::Id(0x2ec9),
        Token::Equal,
        Token::Open,
        Token::Id(0x28e2),
        Token::Equal,
        Token::I32(1),
        Token::Id(0x28e3),
        Token::Equal,
        Token::I32(11),
        Token::Id(0x2ec7),
        Token::Equal,
        Token::I32(4),
        Token::Id(0x2ec8),
        Token::Equal,
        Token::I32(0),
        Token::Close,
    ])]
    #[case(&[
        Token::Id(0x053a),
        Token::Equal,
        Token::Rgb(Rgb {
            r: 110,
            g: 28,
            b: 27,
            a: None
        })
    ])]
    #[case(&[
        Token::Id(0x053a),
        Token::Equal,
        Token::Rgb(Rgb {
            r: 110,
            g: 28,
            b: 27,
            a: Some(128),
        })
    ])]
    #[case(&[
        Token::Id(0x326b), Token::Equal, Token::U64(128),
        Token::Id(0x326b), Token::Equal, Token::I64(-1),
        Token::Id(0x2d82), Token::Equal, Token::F64([0xc7, 0xe4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
        Token::Id(0x2d82), Token::Equal, Token::F32([0x8f, 0xc2, 0x75, 0x3e]),
        Token::Id(0x2d82), Token::Equal, Token::U32(89)
    ])]
    #[case(&[
        Token::Id(0x2d82),
        Token::Equal,
        Token::Lookup(0),
        Token::Id(0x2d82),
        Token::Equal,
        Token::Lookup(255),
        Token::Id(0x2d82),
        Token::Equal,
        Token::Lookup(0),
        Token::Id(0x2d82),
        Token::Equal,
        Token::Lookup(65535),
    ])]
    fn test_roundtrip(#[case] input: &[Token]) {
        let data = Vec::new();
        let mut writer = std::io::Cursor::new(data);
        for tok in input {
            tok.write(&mut writer).unwrap();
        }

        let data = writer.into_inner();

        // `Read`
        let mut reader = TokenReader::new(data.as_slice());
        for (i, e) in input.iter().enumerate() {
            assert_eq!(*e, reader.read().unwrap(), "failure at token idx: {}", i);
        }

        reader.read().unwrap_err();
        assert_eq!(reader.position(), data.len());

        // `from_slice`
        let mut reader = TokenReader::from_slice(data.as_slice());
        for (i, e) in input.iter().enumerate() {
            assert_eq!(*e, reader.read().unwrap(), "failure at token idx: {}", i);
        }

        reader.read().unwrap_err();
        assert_eq!(reader.position(), data.len());

        // reader buffer size
        for i in 30..40 {
            let mut reader = TokenReader::builder().buffer_len(i).build(data.as_slice());
            for e in input {
                assert_eq!(*e, reader.read().unwrap(), "failure at token idx: {}", i);
            }

            reader.read().unwrap_err();
            assert_eq!(reader.position(), data.len());
        }
    }

    #[test]
    fn test_not_enough_data() {
        let mut reader = TokenReader::new(&[0x43][..]);
        assert!(matches!(
            reader.read().unwrap_err().kind(),
            &ReaderErrorKind::Lexer(LexError::Eof)
        ));
    }
}