neotoma 0.1.1

A flexible, cached parser combinator framework for Rust.
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
//! Conditional parsing with until combinators.
//!
//! This module provides parsers that consume input until a specified end condition
//! is met. It includes both byte-level ([`Until`]) and UTF-8 character-level
//! ([`Utf8Until`]) variants for different parsing needs.
//!
//! Until parsers are useful for extracting content between delimiters, parsing
//! string literals, comments, or any scenario where you need to consume input
//! up to a specific boundary condition.

use crate::{
    cache::ParsingCache,
    parser::{Parsable, Parser, Source},
    result::{Error, ParseResult},
    utf8util::read_utf8_char,
};

/// A parser that consumes characters until an end condition is met.
///
/// Until reads characters one by one from the input until the end parser
/// would match at the current position. The end parser is not consumed -
/// it's just used to determine when to stop reading.
///
/// This is useful for parsing content up to a specific delimiter without
/// consuming the delimiter itself.
///
/// # Examples
///
/// ```rust
/// use neotoma::{until::Until, literal::Literal, parser::{parse, Source}};
/// use std::io::Cursor;
///
/// // Read characters until we hit "end"
/// let parser = Until::new(Literal::from_str("end"));
///
/// let mut input = Cursor::new(b"hello world end more text");
/// let mut source = Source::new(input);
/// let result = parse(parser, &mut source).unwrap();
/// assert_eq!(result, b"hello world ".to_vec());
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Until<E> {
    end: E,
    min_length: usize,
    max_length: Option<usize>,
}

impl<E> Until<E> {
    /// Create a new Until parser with default bounds (0 or more characters).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use neotoma::{until::Until, literal::Literal, parser::{parse, Source}};
    /// use std::io::Cursor;
    ///
    /// // Read until we see "."
    /// let parser = Until::new(Literal::from_str("."));
    ///
    /// let mut input = Cursor::new(b"hello world.more");
    /// let mut source = Source::new(input);
    /// let result = parse(parser, &mut source).unwrap();
    /// assert_eq!(result, b"hello world".to_vec());
    /// ```
    pub fn new(end: E) -> Self {
        Self {
            end,
            min_length: 0,
            max_length: None,
        }
    }

    /// Create an Until parser that requires at least `min` characters.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use neotoma::{until::Until, literal::Literal, parser::{parse, Source}};
    /// use std::io::Cursor;
    ///
    /// // Require at least 5 characters before "."
    /// let parser = Until::with_min(Literal::from_str("."), 5);
    ///
    /// let mut input1 = Cursor::new(b"hello.world");
    /// let mut source1 = Source::new(input1);
    /// let result1 = parse(parser, &mut source1).unwrap();
    /// assert_eq!(result1, b"hello".to_vec());
    ///
    /// // This would fail with only 2 characters
    /// let parser2 = Until::with_min(Literal::from_str("."), 5);
    /// let mut input2 = Cursor::new(b"hi.there");
    /// let mut source2 = Source::new(input2);
    /// let result2 = parse(parser2, &mut source2);
    /// assert!(result2.is_err());
    /// ```
    pub fn with_min(end: E, min: usize) -> Self {
        Self {
            end,
            min_length: min,
            max_length: None,
        }
    }

    /// Create an Until parser that reads at most `max` characters.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use neotoma::{until::Until, literal::Literal, parser::{parse, Source}};
    /// use std::io::Cursor;
    ///
    /// // Read at most 5 characters before "."
    /// let parser = Until::with_max(Literal::from_str("."), 5);
    ///
    /// let mut input = Cursor::new(b"hello world.more");
    /// let mut source = Source::new(input);
    /// let result = parse(parser, &mut source).unwrap();
    /// assert_eq!(result, b"hello".to_vec()); // Stops at 5 chars
    /// ```
    pub fn with_max(end: E, max: usize) -> Self {
        Self {
            end,
            min_length: 0,
            max_length: Some(max),
        }
    }

    /// Create an Until parser with both minimum and maximum bounds.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use neotoma::{until::Until, literal::Literal, parser::{parse, Source}};
    /// use std::io::Cursor;
    ///
    /// // Read 3-7 characters before "."
    /// let parser = Until::with_bounds(Literal::from_str("."), 3, 7);
    ///
    /// let mut input = Cursor::new(b"hello.world");
    /// let mut source = Source::new(input);
    /// let result = parse(parser, &mut source).unwrap();
    /// assert_eq!(result, b"hello".to_vec());
    /// ```
    pub fn with_bounds(end: E, min: usize, max: usize) -> Self {
        Self {
            end,
            min_length: min,
            max_length: Some(max),
        }
    }
}

impl<E, Ctx> Parser<Ctx> for Until<E>
where
    E: Parser<Ctx>,
{
    type Output = Vec<u8>;

    fn id(&self) -> u64 {
        use std::any::TypeId;
        use std::hash::{DefaultHasher, Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        TypeId::of::<Self>().hash(&mut hasher);
        self.end.id().hash(&mut hasher);
        self.min_length.hash(&mut hasher);
        self.max_length.hash(&mut hasher);
        hasher.finish()
    }

    fn read<S>(
        &self,
        source: &mut Source<S>,
        cache: &mut impl ParsingCache,
        context: &mut Ctx,
    ) -> ParseResult<Self::Output>
    where
        S: Parsable,
    {
        let mut result = Vec::new();

        loop {
            // Check if we've hit the maximum length
            if let Some(max) = self.max_length {
                if result.len() >= max {
                    break;
                }
            }

            // Check if the end condition matches at current position (without consuming)
            source.push();
            match self.end.parse(source, cache, context) {
                Ok(_) => {
                    // End condition matched - stop here
                    source.pop(); // Backtrack to before the end match
                    break;
                }
                Err(Error::NoMatch) => {
                    // End condition doesn't match - continue reading
                    source.pop(); // Backtrack to before the end attempt
                }
                Err(err) => {
                    // Other error (like IO error) - propagate it
                    source.pop();
                    return Err(err);
                }
            }

            // Try to read one byte
            match source.peek1() {
                Ok(byte) => {
                    result.push(byte);
                    source.advance(1);
                }
                Err(Error::NoMatch) => {
                    // End of input - we're done
                    break;
                }
                Err(err) => {
                    // Other error - propagate it
                    return Err(err);
                }
            }
        }

        // Check minimum requirement
        if result.len() < self.min_length {
            return Err(Error::NoMatch);
        }

        Ok(result)
    }
}

/// A UTF-8 aware parser that consumes characters until an end condition is met.
///
/// Utf8Until reads UTF-8 characters one by one from the input until the end parser
/// would match at the current position. Unlike `Until`, this parser respects UTF-8
/// character boundaries and operates on character counts rather than byte counts.
///
/// # Examples
///
/// ```rust
/// use neotoma::{until::Utf8Until, literal::Literal, parser::{parse, Source}};
/// use std::io::Cursor;
///
/// // Read UTF-8 characters until we hit "end"
/// let parser = Utf8Until::new(Literal::from_str("end"));
///
/// let mut input = Cursor::new("hello 世界 end more".as_bytes());
/// let mut source = Source::new(input);
/// let result = parse(parser, &mut source).unwrap();
/// assert_eq!(result, "hello 世界 ");
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Utf8Until<E> {
    end: E,
    min_chars: usize,
    max_chars: Option<usize>,
}

impl<E> Utf8Until<E> {
    /// Create a new Utf8Until parser with default bounds (0 or more characters).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use neotoma::{until::Utf8Until, literal::Literal, parser::{parse, Source}};
    /// use std::io::Cursor;
    ///
    /// // Read until we see "."
    /// let parser = Utf8Until::new(Literal::from_str("."));
    ///
    /// let mut input = Cursor::new("hello 世界.more".as_bytes());
    /// let mut source = Source::new(input);
    /// let result = parse(parser, &mut source).unwrap();
    /// assert_eq!(result, "hello 世界");
    /// ```
    pub fn new(end: E) -> Self {
        Self {
            end,
            min_chars: 0,
            max_chars: None,
        }
    }

    /// Create a Utf8Until parser that requires at least `min` characters.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use neotoma::{until::Utf8Until, literal::Literal, parser::{parse, Source}};
    /// use std::io::Cursor;
    ///
    /// // Require at least 3 characters before "."
    /// let parser = Utf8Until::with_min(Literal::from_str("."), 3);
    ///
    /// let mut input1 = Cursor::new("hello.world".as_bytes());
    /// let mut source1 = Source::new(input1);
    /// let result1 = parse(parser, &mut source1).unwrap();
    /// assert_eq!(result1, "hello");
    ///
    /// // This would fail with only 2 characters
    /// let parser2 = Utf8Until::with_min(Literal::from_str("."), 3);
    /// let mut input2 = Cursor::new("hi.there".as_bytes());
    /// let mut source2 = Source::new(input2);
    /// let result2 = parse(parser2, &mut source2);
    /// assert!(result2.is_err());
    /// ```
    pub fn with_min(end: E, min: usize) -> Self {
        Self {
            end,
            min_chars: min,
            max_chars: None,
        }
    }

    /// Create a Utf8Until parser that reads at most `max` characters.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use neotoma::{until::Utf8Until, literal::Literal, parser::{parse, Source}};
    /// use std::io::Cursor;
    ///
    /// // Read at most 2 characters before "."
    /// let parser = Utf8Until::with_max(Literal::from_str("."), 2);
    ///
    /// let mut input = Cursor::new("hello 世界.more".as_bytes());
    /// let mut source = Source::new(input);
    /// let result = parse(parser, &mut source).unwrap();
    /// // Note: this reads 2 UTF-8 characters, not 2 bytes
    /// ```
    pub fn with_max(end: E, max: usize) -> Self {
        Self {
            end,
            min_chars: 0,
            max_chars: Some(max),
        }
    }

    /// Create a Utf8Until parser with both minimum and maximum character bounds.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use neotoma::{until::Utf8Until, literal::Literal, parser::{parse, Source}};
    /// use std::io::Cursor;
    ///
    /// // Read 1-3 characters before "."
    /// let parser = Utf8Until::with_bounds(Literal::from_str("."), 1, 3);
    ///
    /// let mut input = Cursor::new("hello.world".as_bytes());
    /// let mut source = Source::new(input);
    /// let result = parse(parser, &mut source).unwrap();
    /// ```
    pub fn with_bounds(end: E, min: usize, max: usize) -> Self {
        Self {
            end,
            min_chars: min,
            max_chars: Some(max),
        }
    }
}

impl<E, Ctx> Parser<Ctx> for Utf8Until<E>
where
    E: Parser<Ctx>,
{
    type Output = String;

    fn id(&self) -> u64 {
        use std::any::TypeId;
        use std::hash::{DefaultHasher, Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        TypeId::of::<Self>().hash(&mut hasher);
        self.end.id().hash(&mut hasher);
        self.min_chars.hash(&mut hasher);
        self.max_chars.hash(&mut hasher);
        hasher.finish()
    }

    fn read<S>(
        &self,
        source: &mut Source<S>,
        cache: &mut impl ParsingCache,
        context: &mut Ctx,
    ) -> ParseResult<Self::Output>
    where
        S: Parsable,
    {
        let mut result = String::new();
        let mut char_count = 0;

        loop {
            // Check if we've hit the maximum character count
            if let Some(max) = self.max_chars {
                if char_count >= max {
                    break;
                }
            }

            // Check if the end condition matches at current position (without consuming)
            source.push();
            match self.end.parse(source, cache, context) {
                Ok(_) => {
                    // End condition matched - stop here
                    source.pop(); // Backtrack to before the end match
                    break;
                }
                Err(Error::NoMatch) => {
                    // End condition doesn't match - continue reading
                    source.pop(); // Backtrack to before the end attempt
                }
                Err(err) => {
                    // Other error (like IO error) - propagate it
                    source.pop();
                    return Err(err);
                }
            }

            // Try to read one UTF-8 character
            match read_utf8_char(source) {
                Ok(ch) => {
                    result.push(ch);
                    char_count += 1;
                }
                Err(Error::NoMatch) => {
                    // End of input - we're done
                    break;
                }
                Err(err) => {
                    // Other error - propagate it
                    return Err(err);
                }
            }
        }

        // Check minimum requirement
        if char_count < self.min_chars {
            return Err(Error::NoMatch);
        }

        Ok(result)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{literal::Literal, parser::parse};
    use std::io::Cursor;

    #[test]
    fn test_id_implementation_different_until_parsers() {
        // Test that Until implements proper id() method
        // Different Until parsers should have different IDs to avoid cache conflicts

        let until1 = Until::new(Literal::from_str("end"));
        let until2 = Until::new(Literal::from_str("stop"));

        // These should have different IDs because they have different end conditions
        // This test will FAIL if Until uses default id() implementation
        assert_ne!(
            <Until<_> as crate::parser::Parser<()>>::id(&until1),
            <Until<_> as crate::parser::Parser<()>>::id(&until2),
            "Different Until instances should have different IDs to avoid cache collisions"
        );
    }

    #[test]
    fn test_id_implementation_same_until_parsers() {
        // Test that identical Until parsers have the same ID
        let until1 = Until::new(Literal::from_str("end"));
        let until2 = Until::new(Literal::from_str("end"));

        assert_eq!(
            <Until<_> as crate::parser::Parser<()>>::id(&until1),
            <Until<_> as crate::parser::Parser<()>>::id(&until2),
            "Identical Until instances should have the same ID for cache efficiency"
        );
    }

    #[test]
    fn test_id_implementation_until_different_bounds() {
        // Test Until parsers with different bounds
        let until1 = Until::with_min(Literal::from_str("end"), 1);
        let until2 = Until::with_min(Literal::from_str("end"), 2);

        // These should have different IDs because they have different minimum bounds
        // This test will FAIL if Until uses default id() implementation
        assert_ne!(
            <Until<_> as crate::parser::Parser<()>>::id(&until1),
            <Until<_> as crate::parser::Parser<()>>::id(&until2),
            "Until instances with different bounds should have different IDs"
        );
    }

    #[test]
    fn test_id_implementation_utf8_until_parsers() {
        // Test that Utf8Until implements proper id() method
        let utf8_until1 = Utf8Until::new(Literal::from_str("end"));
        let utf8_until2 = Utf8Until::new(Literal::from_str("stop"));

        // These should have different IDs because they have different end conditions
        // This test will FAIL if Utf8Until uses default id() implementation
        assert_ne!(
            <Utf8Until<_> as crate::parser::Parser<()>>::id(&utf8_until1),
            <Utf8Until<_> as crate::parser::Parser<()>>::id(&utf8_until2),
            "Different Utf8Until instances should have different IDs to avoid cache collisions"
        );
    }

    #[test]
    fn test_until_basic() {
        let parser = Until::new(Literal::from_str("end"));

        let mut input = Cursor::new(b"hello world end more text");
        let mut source = crate::parser::Source::new(&mut input);

        let result = parse(parser, &mut source).unwrap();
        assert_eq!(result, b"hello world ".to_vec());

        // The "end" should still be available to parse
        let end_parser = Literal::from_str("end");
        let end_result = parse(end_parser, &mut source).unwrap();
        assert_eq!(end_result, b"end".as_slice().into());
    }

    #[test]
    fn test_until_single_character_end() {
        let parser = Until::new(Literal::from_str("."));

        let mut input = Cursor::new(b"hello.world");
        let mut source = crate::parser::Source::new(&mut input);

        let result = parse(parser, &mut source).unwrap();
        assert_eq!(result, b"hello".to_vec());

        // The "." should still be available
        let dot_parser = Literal::from_str(".");
        let dot_result = parse(dot_parser, &mut source).unwrap();
        assert_eq!(dot_result, b".".as_slice().into());
    }

    #[test]
    fn test_until_with_min() {
        let parser = Until::with_min(Literal::from_str("."), 5);

        // Should succeed with 5+ characters
        let mut input1 = Cursor::new(b"hello.world");
        let mut source1 = crate::parser::Source::new(&mut input1);

        let result1 = parse(parser, &mut source1).unwrap();
        assert_eq!(result1, b"hello".to_vec());

        // Should fail with only 2 characters
        let parser2 = Until::with_min(Literal::from_str("."), 5);
        let mut input2 = Cursor::new(b"hi.there");
        let mut source2 = crate::parser::Source::new(&mut input2);

        let result2 = parse(parser2, &mut source2);
        assert!(result2.is_err());
    }

    #[test]
    fn test_until_with_max() {
        let parser = Until::with_max(Literal::from_str("."), 5);

        let mut input = Cursor::new(b"hello world.more");
        let mut source = crate::parser::Source::new(&mut input);

        let result = parse(parser, &mut source).unwrap();
        assert_eq!(result, b"hello".to_vec()); // Stops at 5 characters

        // Should still have " world.more" available
        let remaining = source.peek(11).unwrap();
        assert_eq!(remaining, b" world.more");
    }

    #[test]
    fn test_until_with_bounds() {
        let parser = Until::with_bounds(Literal::from_str("."), 3, 7);

        let mut input = Cursor::new(b"hello world.more");
        let mut source = crate::parser::Source::new(&mut input);

        let result = parse(parser, &mut source).unwrap();
        assert_eq!(result, b"hello w".to_vec()); // Stops at 7 characters (max)
    }

    #[test]
    fn test_until_no_end_found() {
        let parser = Until::new(Literal::from_str("."));

        let mut input = Cursor::new(b"hello world");
        let mut source = crate::parser::Source::new(&mut input);

        let result = parse(parser, &mut source).unwrap();
        assert_eq!(result, b"hello world".to_vec()); // Consumes all input
    }

    #[test]
    fn test_until_immediate_end() {
        let parser = Until::new(Literal::from_str("hello"));

        let mut input = Cursor::new(b"hello world");
        let mut source = crate::parser::Source::new(&mut input);

        let result = parse(parser, &mut source).unwrap();
        assert_eq!(result, b"".to_vec()); // No characters consumed before "hello"

        // "hello" should still be available
        let hello_parser = Literal::from_str("hello");
        let hello_result = parse(hello_parser, &mut source).unwrap();
        assert_eq!(hello_result, b"hello".as_slice().into());
    }

    #[test]
    fn test_until_empty_input() {
        let parser = Until::new(Literal::from_str("."));

        let mut input = Cursor::new(b"");
        let mut source = crate::parser::Source::new(&mut input);

        let result = parse(parser, &mut source).unwrap();
        assert_eq!(result, b"".to_vec()); // No matches on empty input
    }

    #[test]
    fn test_until_multichar_delimiter() {
        let parser = Until::new(Literal::from_str("end"));

        let mut input = Cursor::new(b"start middle end final");
        let mut source = crate::parser::Source::new(&mut input);

        let result = parse(parser, &mut source).unwrap();
        assert_eq!(result, b"start middle ".to_vec());

        // "end" should still be available, then we can parse " final"
        let end_parser = Literal::from_str("end");
        let end_result = parse(end_parser, &mut source).unwrap();
        assert_eq!(end_result, b"end".as_slice().into());

        let final_parser = Literal::from_str(" final");
        let final_result = parse(final_parser, &mut source).unwrap();
        assert_eq!(final_result, b" final".as_slice().into());
    }

    #[test]
    fn test_until_in_sequence() {
        use crate::seq;

        let parser = seq![
            Literal::from_str("start"),
            Literal::from_str("123"),
            Literal::from_str("_"),
            Until::new(Literal::from_str("end")),
            Literal::from_str("end")
        ];

        let mut input = Cursor::new(b"start123_abcend");
        let mut source = crate::parser::Source::new(&mut input);

        let result = parse(parser, &mut source).unwrap();
        assert_eq!(result.0, b"start".as_slice().into());
        assert_eq!(result.1.0, b"123".as_slice().into());
        assert_eq!(result.1.1.0, b"_".as_slice().into());
        assert_eq!(result.1.1.1.0, b"abc".to_vec());
        assert_eq!(result.1.1.1.1.0, b"end".as_slice().into());
    }

    #[test]
    fn test_utf8_until_basic() {
        let parser = Utf8Until::new(Literal::from_str("end"));

        let mut input = Cursor::new("hello 世界 end more".as_bytes());
        let mut source = crate::parser::Source::new(&mut input);

        let result = parse(parser, &mut source).unwrap();
        assert_eq!(result, "hello 世界 ");

        // The "end" should still be available to parse
        let end_parser = Literal::from_str("end");
        let end_result = parse(end_parser, &mut source).unwrap();
        assert_eq!(end_result, b"end".as_slice().into());
    }

    #[test]
    fn test_utf8_until_character_boundaries() {
        let parser = Utf8Until::new(Literal::from_str("."));

        // Mix of ASCII and multi-byte UTF-8 characters
        let mut input = Cursor::new("café 世界.more".as_bytes());
        let mut source = crate::parser::Source::new(&mut input);

        let result = parse(parser, &mut source).unwrap();
        assert_eq!(result, "café 世界");

        // The "." should still be available
        let dot_parser = Literal::from_str(".");
        let dot_result = parse(dot_parser, &mut source).unwrap();
        assert_eq!(dot_result, b".".as_slice().into());
    }

    #[test]
    fn test_utf8_until_with_max_chars() {
        let parser = Utf8Until::with_max(Literal::from_str("."), 3);

        // Should stop at 3 UTF-8 characters, not 3 bytes
        let mut input = Cursor::new("café 世界 more.end".as_bytes());
        let mut source = crate::parser::Source::new(&mut input);

        let result = parse(parser, &mut source).unwrap();
        // "café" = 4 bytes but 4 characters, " " = 1 char, "世" = 1 char = 3 chars total
        assert_eq!(result, "caf");
    }

    #[test]
    fn test_utf8_until_with_min_chars() {
        let parser = Utf8Until::with_min(Literal::from_str("."), 3);

        // Should succeed with 3+ characters
        let mut input1 = Cursor::new("世界测.test".as_bytes());
        let mut source1 = crate::parser::Source::new(&mut input1);

        let result1 = parse(parser, &mut source1).unwrap();
        assert_eq!(result1, "世界测");

        // Should fail with only 2 characters
        let parser2 = Utf8Until::with_min(Literal::from_str("."), 3);
        let mut input2 = Cursor::new("世界.test".as_bytes());
        let mut source2 = crate::parser::Source::new(&mut input2);

        let result2 = parse(parser2, &mut source2);
        assert!(result2.is_err());
    }

    #[test]
    fn test_utf8_until_invalid_utf8() {
        let parser = Utf8Until::with_min(Literal::from_str("end"), 1);

        // Create invalid UTF-8 sequence at the start (0xFF is not valid UTF-8)
        let mut input = Cursor::new(b"\xFF\xFEend");
        let mut source = crate::parser::Source::new(&mut input);

        // Should fail when it tries to read invalid UTF-8
        let result = parse(parser, &mut source);
        assert!(result.is_err());
    }

    #[test]
    fn test_utf8_until_emoji() {
        let parser = Utf8Until::new(Literal::from_str("!"));

        // Test with emoji (4-byte UTF-8 characters)
        let mut input = Cursor::new("Hello 👋 World 🌍!".as_bytes());
        let mut source = crate::parser::Source::new(&mut input);

        let result = parse(parser, &mut source).unwrap();
        assert_eq!(result, "Hello 👋 World 🌍");

        // The "!" should still be available
        let bang_parser = Literal::from_str("!");
        let bang_result = parse(bang_parser, &mut source).unwrap();
        assert_eq!(bang_result, b"!".as_slice().into());
    }

    #[test]
    fn test_utf8_until_bounds() {
        let parser = Utf8Until::with_bounds(Literal::from_str("."), 2, 4);

        let mut input = Cursor::new("世界测试.more".as_bytes());
        let mut source = crate::parser::Source::new(&mut input);

        let result = parse(parser, &mut source).unwrap();
        assert_eq!(result, "世界测试"); // 4 characters (max)
    }

    #[test]
    fn test_until_complex_end_parser() {
        use crate::sequence::Sequence;

        // Use a sequence as the end condition
        let end_parser = Sequence::new(Literal::from_str("end"), Literal::from_str("_tag"));
        let parser = Until::new(end_parser);

        let mut input = Cursor::new(b"some content before end_tag and after");
        let mut source = crate::parser::Source::new(&mut input);

        let result = parse(parser, &mut source).unwrap();
        assert_eq!(result, b"some content before ".to_vec());

        // The end parser should still be available
        let end_parser2 = Sequence::new(Literal::from_str("end"), Literal::from_str("_tag"));
        let end_result = parse(end_parser2, &mut source).unwrap();
        assert_eq!(end_result.0, b"end".as_slice().into());
        assert_eq!(end_result.1, b"_tag".as_slice().into());
    }

    #[test]
    fn test_until_with_repeat_end_parser() {
        use crate::repeat::Repeat;

        // End condition is repeated 'x'
        let end_parser = Repeat::with_min(Literal::from_str("x"), 2);
        let parser = Until::new(end_parser);

        let mut input = Cursor::new(b"content before xxxx and after");
        let mut source = crate::parser::Source::new(&mut input);

        let result = parse(parser, &mut source).unwrap();
        assert_eq!(result, b"content before ".to_vec());
    }

    #[test]
    fn test_until_multiple_potential_ends() {
        // Test content with multiple instances of the end pattern
        let parser = Until::new(Literal::from_str("end"));

        let mut input = Cursor::new(b"start end middle end final");
        let mut source = crate::parser::Source::new(&mut input);

        let result = parse(parser, &mut source).unwrap();
        assert_eq!(result, b"start ".to_vec()); // Stops at first "end"

        // Should be positioned at the first "end"
        let remaining = source.peek(20).unwrap();
        assert_eq!(remaining, b"end middle end final");
    }

    #[test]
    fn test_until_overlapping_end_patterns() {
        // Test where end pattern overlaps with content
        let parser = Until::new(Literal::from_str("aba"));

        let mut input = Cursor::new(b"xyzababadef");
        let mut source = crate::parser::Source::new(&mut input);

        let result = parse(parser, &mut source).unwrap();
        assert_eq!(result, b"xyz".to_vec()); // Stops when it finds "aba"

        // Should be positioned at "aba"
        let remaining = source.peek(8).unwrap();
        assert_eq!(remaining, b"ababadef");
    }

    #[test]
    fn test_until_boundary_edge_cases() {
        // Test exactly at minimum bound
        let parser = Until::with_min(Literal::from_str("."), 5);

        let mut input1 = Cursor::new(b"12345.rest");
        let mut source1 = crate::parser::Source::new(&mut input1);

        let result1 = parse(parser, &mut source1).unwrap();
        assert_eq!(result1, b"12345".to_vec());

        // Test just below minimum bound
        let parser2 = Until::with_min(Literal::from_str("."), 5);
        let mut input2 = Cursor::new(b"1234.rest");
        let mut source2 = crate::parser::Source::new(&mut input2);

        let result2 = parse(parser2, &mut source2);
        assert!(result2.is_err());

        // Test exactly at maximum bound
        let parser3 = Until::with_max(Literal::from_str("."), 3);
        let mut input3 = Cursor::new(b"12345678.rest");
        let mut source3 = crate::parser::Source::new(&mut input3);

        let result3 = parse(parser3, &mut source3).unwrap();
        assert_eq!(result3, b"123".to_vec()); // Stops at max
    }

    #[test]
    fn test_until_very_large_content() {
        // Test with reasonably large content
        let large_content = b"x".repeat(10000);
        let mut input_bytes = Vec::new();
        input_bytes.extend_from_slice(&large_content);
        input_bytes.extend_from_slice(b"END");

        let parser = Until::new(Literal::from_str("END"));
        let mut input = Cursor::new(&input_bytes);
        let mut source = crate::parser::Source::new(&mut input);

        let result = parse(parser, &mut source).unwrap();
        assert_eq!(result.len(), 10000);
        assert!(result.iter().all(|&b| b == b'x'));
    }

    #[test]
    fn test_utf8_until_complex_end_condition() {
        use crate::utf8class::Utf8Class;

        // End condition is Unicode digits
        let end_parser = Utf8Class::unicode_digits();
        let parser = Utf8Until::new(end_parser);

        let mut input = Cursor::new("Hello 世界 ১২৩ more text".as_bytes());
        let mut source = crate::parser::Source::new(&mut input);

        let result = parse(parser, &mut source).unwrap();
        assert_eq!(result, "Hello 世界 ");
    }

    #[test]
    fn test_utf8_until_mixed_content() {
        // Test with mixed ASCII, Latin-1, and Unicode content
        let parser = Utf8Until::new(Literal::from_str("🔚"));

        let mut input = Cursor::new("ASCII café 世界 привет 🔚 end".as_bytes());
        let mut source = crate::parser::Source::new(&mut input);

        let result = parse(parser, &mut source).unwrap();
        assert_eq!(result, "ASCII café 世界 привет ");

        // Should be positioned at the emoji
        let remaining_bytes = source.peek(8).unwrap();
        assert_eq!(remaining_bytes, "🔚 end".as_bytes());
    }

    #[test]
    fn test_until_position_after_parsing() {
        // Verify position tracking through complex scenarios
        let parser = Until::new(Literal::from_str("||"));

        let mut input = Cursor::new(b"item1|item2||item3|item4");
        let mut source = crate::parser::Source::new(&mut input);

        let result = parse(parser, &mut source).unwrap();
        assert_eq!(result, b"item1|item2".to_vec());

        // Should be positioned at "||"
        let delimiter = source.peek(2).unwrap();
        assert_eq!(delimiter, b"||");

        // Advance past delimiter
        source.advance(2);

        // Should now be at "item3"
        let remaining = source.peek(5).unwrap();
        assert_eq!(remaining, b"item3");
    }

    #[test]
    fn test_until_with_empty_end_condition() {
        use crate::literal::Literal;

        // Create a literal that matches empty string (this might be a special case)
        let empty_literal = Literal::from_str("");
        let parser = Until::new(empty_literal);

        let mut input = Cursor::new(b"some content");
        let mut source = crate::parser::Source::new(&mut input);

        // This should immediately match at the start since empty string matches anywhere
        let result = parse(parser, &mut source).unwrap();
        assert_eq!(result, Vec::<u8>::new());
    }

    #[test]
    fn test_utf8_until_character_counting_accuracy() {
        // Verify character counting vs byte counting
        let parser = Utf8Until::with_max(Literal::from_str("!"), 3);

        // "🇺🇸" is 8 bytes but 1 grapheme cluster, "é" is 2 bytes but 1 character
        let mut input = Cursor::new("🇺🇸é!more".as_bytes());
        let mut source = crate::parser::Source::new(&mut input);

        let result = parse(parser, &mut source).unwrap();
        // Should get the flag emoji, é, and then stop due to max chars = 3
        // But this might depend on how Unicode characters are counted
        println!("Result: '{}', len: {}", result, result.chars().count());
    }
}