babbel_core 0.1.1

Shared core I/O, encoding, numeric, error, and AST primitives for the Babbel data format family
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
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
use super::traits::{
    IByteStream, ICharStream, IIndentationAware, ILineReader, ILocationAware, IPositionAware,
    IRewindable, ISource, IStatefulStream, SaveState,
};
#[cfg(not(feature = "std"))]
use alloc::string::String;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

/// Zero-copy input source reading from an in-memory byte or UTF-8 slice.
#[derive(Debug, Clone)]
pub struct SliceSource<'a> {
    data: &'a str,
    chars: core::str::CharIndices<'a>,
    current_char: Option<char>,
    pos: usize,
    line: usize,
    column: usize,
}

impl<'a> SliceSource<'a> {
    /// Creates a new `SliceSource` wrapping a string slice.
    pub fn new(data: &'a str) -> Self {
        let mut s = Self {
            data,
            chars: data.char_indices(),
            current_char: None,
            pos: 0,
            line: 0,
            column: 0,
        };
        s.next();
        s
    }

    /// Advances to the next character.
    pub fn next(&mut self) {
        if let Some(ch) = self.current_char {
            if ch == '\n' {
                self.line += 1;
                self.column = 0;
            } else if ch == '\r' {
                let mut peek_chars = self.chars.clone();
                if let Some((_, '\n')) = peek_chars.next() {
                    // CRLF: \n will handle line increment
                } else {
                    self.line += 1;
                    self.column = 0;
                }
            } else {
                self.column += 1;
            }
        }
        if let Some((idx, ch)) = self.chars.next() {
            self.pos = idx;
            self.current_char = Some(ch);
        } else {
            self.pos = self.data.len();
            self.current_char = None;
        }
    }

    /// Returns the character at the current position.
    pub fn current(&mut self) -> Option<char> {
        self.current_char
    }

    /// Checks if more characters are available.
    pub fn more(&mut self) -> bool {
        self.current_char.is_some()
    }

    /// Resets reading position to the beginning.
    pub fn reset(&mut self) {
        self.chars = self.data.char_indices();
        self.pos = 0;
        self.line = 0;
        self.column = 0;
        self.current_char = None;
        self.next();
    }

    /// Returns absolute byte offset in slice.
    pub fn position(&self) -> usize {
        self.pos
    }

    /// Reads the next line into a String without trailing newline.
    pub fn read_line(&mut self) -> Option<String> {
        let mut s = String::new();
        if self.read_line_into(&mut s) {
            Some(s)
        } else {
            None
        }
    }

    /// Reads the next line into an existing buffer. Returns false at EOF.
    pub fn read_line_into(&mut self, buf: &mut String) -> bool {
        if !self.more() {
            return false;
        }
        while let Some(ch) = self.current() {
            if ch == '\n' {
                self.next();
                break;
            } else if ch == '\r' {
                self.next();
                if self.current() == Some('\n') {
                    self.next();
                }
                break;
            } else {
                buf.push(ch);
                self.next();
            }
        }
        true
    }

    /// Zero-copy read of the next line as a borrowed string slice without newline characters.
    pub fn read_line_slice(&mut self) -> Option<&'a str> {
        if !self.more() {
            return None;
        }
        let start = self.pos;
        let mut end = self.pos;
        while let Some(ch) = self.current() {
            if ch == '\n' {
                end = self.pos;
                self.next();
                return Some(&self.data[start..end]);
            } else if ch == '\r' {
                end = self.pos;
                self.next();
                if self.current() == Some('\n') {
                    self.next();
                }
                return Some(&self.data[start..end]);
            } else {
                self.next();
                end = self.pos;
            }
        }
        Some(&self.data[start..end])
    }
}

impl<'a> ISource for SliceSource<'a> {
    fn next(&mut self) {
        self.next();
    }
    fn current(&mut self) -> Option<char> {
        self.current()
    }
    fn more(&mut self) -> bool {
        self.more()
    }
    fn reset(&mut self) {
        self.reset();
    }
}

impl<'a> ICharStream for SliceSource<'a> {
    fn next(&mut self) {
        self.next();
    }
    fn current(&mut self) -> Option<char> {
        self.current()
    }
    fn more(&mut self) -> bool {
        self.more()
    }
}

impl<'a> IRewindable for SliceSource<'a> {
    fn reset(&mut self) {
        self.reset();
    }
}

impl<'a> IPositionAware for SliceSource<'a> {
    fn position(&self) -> usize {
        self.pos
    }
}

impl<'a> ILineReader for SliceSource<'a> {
    fn read_line_into(&mut self, buf: &mut String) -> bool {
        self.read_line_into(buf)
    }
}

impl<'a> IIndentationAware for SliceSource<'a> {
    fn get_current_indent_level(&self) -> usize {
        self.column
    }
}

impl<'a> ILocationAware for SliceSource<'a> {
    fn line(&self) -> usize {
        self.line + 1
    }
    fn column(&self) -> usize {
        self.column + 1
    }
}

impl<'a> IStatefulStream for SliceSource<'a> {
    fn save_state(&mut self) -> SaveState {
        SaveState {
            pos: self.pos as u64,
            current_byte: self.current_char.map(|c| (c as u32 & 0xFF) as u8),
            column: self.column,
            line: self.line,
        }
    }

    fn restore_state(&mut self, state: SaveState) {
        self.pos = state.pos as usize;
        self.line = state.line;
        self.column = state.column;
        if self.pos < self.data.len() {
            self.chars = self.data[self.pos..].char_indices();
            self.current_char = None;
            self.next();
        } else {
            self.chars = self.data[self.data.len()..].char_indices();
            self.current_char = None;
        }
    }
}

/// Owned string input source.
#[derive(Debug, Clone)]
pub struct StringSource {
    content: String,
    pos: usize,
}

impl StringSource {
    /// Creates a new `StringSource`.
    pub fn new(content: impl Into<String>) -> Self {
        Self {
            content: content.into(),
            pos: 0,
        }
    }

    /// Advances to the next character.
    pub fn next(&mut self) {
        if self.pos < self.content.len() {
            if let Some(ch) = self.content[self.pos..].chars().next() {
                self.pos += ch.len_utf8();
            }
        }
    }

    /// Returns the current character.
    pub fn current(&mut self) -> Option<char> {
        if self.pos < self.content.len() {
            self.content[self.pos..].chars().next()
        } else {
            None
        }
    }

    /// Checks if more characters are available.
    pub fn more(&mut self) -> bool {
        self.pos < self.content.len()
    }

    /// Resets reading position.
    pub fn reset(&mut self) {
        self.pos = 0;
    }

    /// Returns current byte offset in source string.
    pub fn position(&self) -> usize {
        self.pos
    }

    /// Reads the next line into a String without trailing newline.
    pub fn read_line(&mut self) -> Option<String> {
        let mut s = String::new();
        if self.read_line_into(&mut s) {
            Some(s)
        } else {
            None
        }
    }

    /// Reads the next line into an existing buffer. Returns false at EOF.
    pub fn read_line_into(&mut self, buf: &mut String) -> bool {
        if !self.more() {
            return false;
        }
        while let Some(ch) = self.current() {
            if ch == '\n' {
                self.next();
                break;
            } else if ch == '\r' {
                self.next();
                if self.current() == Some('\n') {
                    self.next();
                }
                break;
            } else {
                buf.push(ch);
                self.next();
            }
        }
        true
    }
}

impl IPositionAware for StringSource {
    fn position(&self) -> usize {
        self.pos
    }
}

impl ISource for StringSource {
    fn next(&mut self) {
        self.next();
    }
    fn current(&mut self) -> Option<char> {
        self.current()
    }
    fn more(&mut self) -> bool {
        self.more()
    }
    fn reset(&mut self) {
        self.reset();
    }
}

impl ICharStream for StringSource {
    fn next(&mut self) {
        self.next();
    }
    fn current(&mut self) -> Option<char> {
        self.current()
    }
    fn more(&mut self) -> bool {
        self.more()
    }
}

impl IRewindable for StringSource {
    fn reset(&mut self) {
        self.reset();
    }
}

impl ILineReader for StringSource {
    fn read_line_into(&mut self, buf: &mut String) -> bool {
        self.read_line_into(buf)
    }
}

/// Raw byte slice reader implementing `IByteStream` and `IByteReader`.
#[derive(Debug, Clone)]
pub struct ByteSliceSource<'a> {
    slice: &'a [u8],
    pos: usize,
}

impl<'a> ByteSliceSource<'a> {
    /// Creates a new byte slice source.
    pub fn new(slice: &'a [u8]) -> Self {
        Self { slice, pos: 0 }
    }

    /// Returns current byte offset.
    pub fn position(&self) -> usize {
        self.pos
    }
}

impl<'a> IPositionAware for ByteSliceSource<'a> {
    fn position(&self) -> usize {
        self.pos
    }
}

impl<'a> IByteStream for ByteSliceSource<'a> {
    fn peek_byte(&mut self) -> Option<u8> {
        self.slice.get(self.pos).copied()
    }

    fn read_byte(&mut self) -> Option<u8> {
        let b = self.slice.get(self.pos).copied()?;
        self.pos += 1;
        Some(b)
    }

    fn advance(&mut self) {
        if self.pos < self.slice.len() {
            self.pos += 1;
        }
    }

    fn has_more(&mut self) -> bool {
        self.pos < self.slice.len()
    }
}

impl<'a> IRewindable for ByteSliceSource<'a> {
    fn reset(&mut self) {
        self.pos = 0;
    }
}

/// In-memory byte vector input source supporting both binary byte streaming
/// and Unicode UTF-8 character streaming.
#[derive(Debug, Clone, Default)]
pub struct BufferSource {
    buffer: Vec<u8>,
    position: usize,
    line: usize,
    column: usize,
}

impl BufferSource {
    /// Creates a new BufferSource from a byte slice.
    pub fn new(data: &[u8]) -> Self {
        Self {
            buffer: data.to_vec(),
            position: 0,
            line: 0,
            column: 0,
        }
    }

    /// Creates a new BufferSource by taking ownership of a byte vector (zero-copy).
    pub fn from_vec(buffer: Vec<u8>) -> Self {
        Self {
            buffer,
            position: 0,
            line: 0,
            column: 0,
        }
    }

    /// Advances to the next character.
    pub fn next(&mut self) {
        if self.position < self.buffer.len() {
            let current_byte = self.buffer[self.position];
            if current_byte == b'\n' {
                self.line += 1;
                self.column = 0;
            } else if current_byte == b'\r' {
                if self.position + 1 < self.buffer.len() && self.buffer[self.position + 1] == b'\n' {
                    // CRLF: newline will handle line increment
                } else {
                    self.line += 1;
                    self.column = 0;
                }
            } else {
                self.column += 1;
            }

            if let Some(ch) = self.current() {
                let len = ch.len_utf8();
                if len > 0 && self.position + len <= self.buffer.len() {
                    self.position += len;
                    return;
                }
            }
            self.position += 1;
        }
    }

    /// Returns the character at the current position.
    pub fn current(&mut self) -> Option<char> {
        if self.position >= self.buffer.len() {
            return None;
        }
        match core::str::from_utf8(&self.buffer[self.position..]) {
            Ok(s) => s.chars().next(),
            Err(e) => {
                if e.valid_up_to() > 0 {
                    let valid = unsafe { core::str::from_utf8_unchecked(&self.buffer[self.position..self.position + e.valid_up_to()]) };
                    valid.chars().next()
                } else {
                    Some(self.buffer[self.position] as char)
                }
            }
        }
    }

    /// Checks if more characters are available.
    pub fn more(&mut self) -> bool {
        self.position < self.buffer.len()
    }

    /// Resets the position to 0.
    pub fn reset(&mut self) {
        self.position = 0;
        self.line = 0;
        self.column = 0;
    }

    /// Converts the buffer to a UTF-8 string.
    pub fn to_string(&self) -> String {
        #[cfg(feature = "std")]
        return String::from_utf8_lossy(&self.buffer).into_owned();
        #[cfg(not(feature = "std"))]
        {
            if let Ok(s) = core::str::from_utf8(&self.buffer) {
                alloc::string::ToString::to_string(s)
            } else {
                alloc::string::ToString::to_string(&alloc::string::String::from_utf8_lossy(&self.buffer))
            }
        }
    }

    /// Returns a slice of the underlying buffer.
    pub fn as_slice(&self) -> &[u8] {
        &self.buffer
    }

    /// Returns current byte offset.
    pub fn position(&self) -> usize {
        self.position
    }

    /// Reads the next line into a String without trailing newline.
    pub fn read_line(&mut self) -> Option<String> {
        let mut s = String::new();
        if self.read_line_into(&mut s) {
            Some(s)
        } else {
            None
        }
    }

    /// Reads the next line into an existing buffer. Returns false at EOF.
    pub fn read_line_into(&mut self, buf: &mut String) -> bool {
        if !self.more() {
            return false;
        }
        while let Some(ch) = self.current() {
            if ch == '\n' {
                self.next();
                break;
            } else if ch == '\r' {
                self.next();
                if self.current() == Some('\n') {
                    self.next();
                }
                break;
            } else {
                buf.push(ch);
                self.next();
            }
        }
        true
    }
}

impl IPositionAware for BufferSource {
    fn position(&self) -> usize {
        self.position
    }
}

impl ISource for BufferSource {
    fn next(&mut self) {
        self.next();
    }
    fn current(&mut self) -> Option<char> {
        self.current()
    }
    fn more(&mut self) -> bool {
        self.more()
    }
    fn reset(&mut self) {
        self.reset();
    }
}

impl ICharStream for BufferSource {
    fn next(&mut self) {
        self.next();
    }
    fn current(&mut self) -> Option<char> {
        self.current()
    }
    fn more(&mut self) -> bool {
        self.more()
    }
}

impl IRewindable for BufferSource {
    fn reset(&mut self) {
        self.reset();
    }
}

impl ILineReader for BufferSource {
    fn read_line_into(&mut self, buf: &mut String) -> bool {
        self.read_line_into(buf)
    }
}

impl IIndentationAware for BufferSource {
    fn get_current_indent_level(&self) -> usize {
        self.column
    }
}

impl ILocationAware for BufferSource {
    fn line(&self) -> usize {
        self.line + 1
    }
    fn column(&self) -> usize {
        self.column + 1
    }
}

impl IStatefulStream for BufferSource {
    fn save_state(&mut self) -> SaveState {
        SaveState {
            pos: self.position as u64,
            current_byte: if self.more() {
                Some(self.buffer[self.position])
            } else {
                None
            },
            column: self.column,
            line: self.line,
        }
    }

    fn restore_state(&mut self, state: SaveState) {
        self.position = state.pos as usize;
        self.column = state.column;
        self.line = state.line;
    }
}

impl IByteStream for BufferSource {
    fn peek_byte(&mut self) -> Option<u8> {
        self.buffer.get(self.position).copied()
    }

    fn read_byte(&mut self) -> Option<u8> {
        if self.position < self.buffer.len() {
            let b = self.buffer[self.position];
            self.position += 1;
            Some(b)
        } else {
            None
        }
    }

    fn advance(&mut self) {
        self.position += 1;
    }

    fn has_more(&mut self) -> bool {
        self.position < self.buffer.len()
    }
}

#[cfg(feature = "file-io")]
/// File input source reading binary or text data from disk.
#[derive(Debug, Clone)]
pub struct FileSource {
    path: std::path::PathBuf,
    inner: BufferSource,
}

#[cfg(feature = "file-io")]
impl FileSource {
    /// Default maximum file size permitted to prevent memory exhaustion DoS (64 MB).
    pub const DEFAULT_MAX_BYTES: u64 = 64 * 1024 * 1024;

    /// Opens and reads an entire file into memory as raw bytes.
    pub fn new(path: impl AsRef<std::path::Path>) -> std::io::Result<Self> {
        Self::open(path)
    }

    /// Opens and reads an entire file into memory with default size limit (64 MB).
    pub fn open(path: impl AsRef<std::path::Path>) -> std::io::Result<Self> {
        Self::open_with_limit(path, Self::DEFAULT_MAX_BYTES)
    }

    /// Opens and reads a file into memory with a configurable maximum byte limit.
    pub fn open_with_limit(path: impl AsRef<std::path::Path>, max_bytes: u64) -> std::io::Result<Self> {
        let p = path.as_ref().to_path_buf();
        let metadata = std::fs::metadata(&p)?;
        if metadata.len() > max_bytes {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                alloc::format!(
                    "File size {} exceeds maximum permitted limit of {} bytes",
                    metadata.len(),
                    max_bytes
                ),
            ));
        }
        let bytes = std::fs::read(&p)?;
        Ok(Self {
            path: p,
            inner: BufferSource::from_vec(bytes),
        })
    }

    /// Advances to the next character.
    pub fn next(&mut self) {
        self.inner.next();
    }

    /// Returns the character at the current position.
    pub fn current(&mut self) -> Option<char> {
        self.inner.current()
    }

    /// Checks if more characters are available.
    pub fn more(&mut self) -> bool {
        self.inner.more()
    }

    /// Resets reading position.
    pub fn reset(&mut self) {
        self.inner.reset();
    }

    /// Returns the file path name.
    pub fn file_name(&self) -> &str {
        self.path.to_str().unwrap_or("")
    }

    /// Reads the next line into a String without trailing newline.
    pub fn read_line(&mut self) -> Option<String> {
        self.inner.read_line()
    }

    /// Reads the next line into an existing buffer. Returns false at EOF.
    pub fn read_line_into(&mut self, buf: &mut String) -> bool {
        self.inner.read_line_into(buf)
    }

    /// Returns the current byte position in the file.
    pub fn position(&self) -> usize {
        self.inner.position()
    }

    /// Returns buffer as UTF-8 string.
    pub fn to_string(&self) -> String {
        self.inner.to_string()
    }

    /// Peeks at current byte without advancing.
    pub fn peek_byte(&mut self) -> Option<u8> {
        self.inner.peek_byte()
    }

    /// Reads current byte and advances position by one byte.
    pub fn read_byte(&mut self) -> Option<u8> {
        self.inner.read_byte()
    }

    /// Advances position by one byte.
    pub fn advance(&mut self) {
        self.inner.advance();
    }

    /// Checks if there are more bytes available.
    pub fn has_more(&mut self) -> bool {
        self.inner.has_more()
    }
}

#[cfg(feature = "file-io")]
impl IPositionAware for FileSource {
    fn position(&self) -> usize {
        self.inner.position()
    }
}

#[cfg(feature = "file-io")]
impl ISource for FileSource {
    fn next(&mut self) {
        self.next();
    }
    fn current(&mut self) -> Option<char> {
        self.current()
    }
    fn more(&mut self) -> bool {
        self.more()
    }
    fn reset(&mut self) {
        self.reset();
    }
}

#[cfg(feature = "file-io")]
impl ICharStream for FileSource {
    fn next(&mut self) {
        self.next();
    }
    fn current(&mut self) -> Option<char> {
        self.current()
    }
    fn more(&mut self) -> bool {
        self.more()
    }
}

#[cfg(feature = "file-io")]
impl IByteStream for FileSource {
    fn peek_byte(&mut self) -> Option<u8> {
        self.inner.peek_byte()
    }
    fn read_byte(&mut self) -> Option<u8> {
        self.inner.read_byte()
    }
    fn advance(&mut self) {
        self.inner.advance();
    }
    fn has_more(&mut self) -> bool {
        self.inner.has_more()
    }
}

#[cfg(feature = "file-io")]
impl IRewindable for FileSource {
    fn reset(&mut self) {
        self.reset();
    }
}

#[cfg(feature = "file-io")]
impl ILineReader for FileSource {
    fn read_line_into(&mut self, buf: &mut String) -> bool {
        self.inner.read_line_into(buf)
    }
}

#[cfg(feature = "file-io")]
impl IIndentationAware for FileSource {
    fn get_current_indent_level(&self) -> usize {
        self.inner.get_current_indent_level()
    }
}

#[cfg(feature = "file-io")]
impl ILocationAware for FileSource {
    fn line(&self) -> usize {
        self.inner.line()
    }
    fn column(&self) -> usize {
        self.inner.column()
    }
}

#[cfg(feature = "file-io")]
impl IStatefulStream for FileSource {
    fn save_state(&mut self) -> SaveState {
        self.inner.save_state()
    }

    fn restore_state(&mut self, state: SaveState) {
        self.inner.restore_state(state);
    }
}

#[cfg(feature = "std")]
/// Streaming reader input source wrapping any `std::io::Read` implementor with internal buffering.
#[derive(Debug)]
pub struct ReaderSource<R> {
    reader: std::io::BufReader<R>,
    current_byte: Option<u8>,
    pos: usize,
    line: usize,
    column: usize,
}

#[cfg(feature = "std")]
impl<R: std::io::Read> ReaderSource<R> {
    /// Creates a new `ReaderSource` wrapping a reader.
    pub fn new(reader: R) -> Self {
        let mut s = Self {
            reader: std::io::BufReader::new(reader),
            current_byte: None,
            pos: 0,
            line: 0,
            column: 0,
        };
        s.read_next_byte();
        s
    }

    fn read_next_byte(&mut self) {
        use std::io::Read;
        let mut buf = [0u8; 1];
        match self.reader.read(&mut buf) {
            Ok(1) => {
                self.current_byte = Some(buf[0]);
            }
            _ => {
                self.current_byte = None;
            }
        }
    }

    /// Advances to next byte/character.
    pub fn next(&mut self) {
        if let Some(b) = self.current_byte {
            self.pos += 1;
            if b == b'\n' {
                self.line += 1;
                self.column = 0;
            } else if b == b'\r' {
                self.line += 1;
                self.column = 0;
            } else {
                self.column += 1;
            }
            self.read_next_byte();
        }
    }

    /// Returns current character.
    pub fn current(&mut self) -> Option<char> {
        self.current_byte.map(|b| b as char)
    }

    /// Checks if more bytes are available.
    pub fn more(&mut self) -> bool {
        self.current_byte.is_some()
    }
}

#[cfg(feature = "std")]
impl<R: std::io::Read> ICharStream for ReaderSource<R> {
    fn next(&mut self) {
        self.next();
    }
    fn current(&mut self) -> Option<char> {
        self.current()
    }
    fn more(&mut self) -> bool {
        self.more()
    }
}

#[cfg(feature = "std")]
impl<R: std::io::Read> IPositionAware for ReaderSource<R> {
    fn position(&self) -> usize {
        self.pos
    }
}

#[cfg(feature = "std")]
impl<R: std::io::Read> ILocationAware for ReaderSource<R> {
    fn line(&self) -> usize {
        self.line + 1
    }
    fn column(&self) -> usize {
        self.column + 1
    }
}

#[cfg(feature = "std")]
impl<R: std::io::Read> IIndentationAware for ReaderSource<R> {
    fn get_current_indent_level(&self) -> usize {
        self.column
    }
}

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

    #[test]
    fn test_slice_source() {
        let mut src = SliceSource::new("abc");
        assert_eq!(src.current(), Some('a'));
        src.next();
        assert_eq!(src.current(), Some('b'));
        src.next();
        assert_eq!(src.current(), Some('c'));
        src.next();
        assert_eq!(src.current(), None);
        assert!(!src.more());
        src.reset();
        assert_eq!(src.current(), Some('a'));
    }

    #[test]
    fn test_byte_slice_source() {
        let mut src = ByteSliceSource::new(b"xyz");
        assert_eq!(src.peek_byte(), Some(b'x'));
        assert_eq!(src.read_byte(), Some(b'x'));
        assert_eq!(src.read_byte(), Some(b'y'));
        assert_eq!(src.read_byte(), Some(b'z'));
        assert_eq!(src.read_byte(), None);
        assert!(!src.has_more());
    }

    #[test]
    fn test_buffer_source_utf8() {
        let mut src = BufferSource::new("hello 🦀".as_bytes());
        assert_eq!(src.current(), Some('h'));
        src.next();
        assert_eq!(src.current(), Some('e'));
        src.next();
        src.next();
        src.next();
        src.next(); // at space
        assert_eq!(src.current(), Some(' '));
        src.next(); // at crab emoji (4-byte UTF-8)
        assert_eq!(src.current(), Some('🦀'));
        src.next();
        assert_eq!(src.current(), None);
        assert!(!src.more());
    }

    #[test]
    fn test_line_reading_crlf_and_lf() {
        let text = "First line\r\nSecond line\nThird line\rFourth line";
        let mut src = SliceSource::new(text);
        assert_eq!(src.read_line().as_deref(), Some("First line"));
        assert_eq!(src.read_line().as_deref(), Some("Second line"));
        assert_eq!(src.read_line().as_deref(), Some("Third line"));
        assert_eq!(src.read_line().as_deref(), Some("Fourth line"));
        assert_eq!(src.read_line(), None);

        let mut buf_src = BufferSource::new(text.as_bytes());
        let lines: Vec<String> = buf_src.lines().collect();
        assert_eq!(lines, vec!["First line", "Second line", "Third line", "Fourth line"]);

        let mut slice_src = SliceSource::new(text);
        assert_eq!(slice_src.read_line_slice(), Some("First line"));
        assert_eq!(slice_src.read_line_slice(), Some("Second line"));
        assert_eq!(slice_src.read_line_slice(), Some("Third line"));
        assert_eq!(slice_src.read_line_slice(), Some("Fourth line"));
        assert_eq!(slice_src.read_line_slice(), None);
    }
}