oxirs-wasm 0.2.4

WebAssembly bindings for OxiRS - Run RDF/SPARQL in the browser
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
//! # Streaming / Incremental RDF Document Parser
//!
//! Chunk-based parser that processes RDF input incrementally, emitting parse events
//! as complete tokens become available.
//!
//! Supported formats:
//! - **N-Triples** — one triple per line `<s> <p> <o> .`
//! - **N-Quads** — one quad per line `<s> <p> <o> <g> .`
//! - **Turtle** — triples with PREFIX/BASE directives
//! - **TriG** — quads with named graphs (simplified)

use std::collections::VecDeque;

// ─── Public types ─────────────────────────────────────────────────────────────

/// Events emitted by the streaming parser
#[derive(Debug, Clone, PartialEq)]
pub enum ParseEvent {
    /// A complete RDF triple was parsed
    Triple { s: String, p: String, o: String },
    /// A complete RDF quad was parsed (triple in a named graph)
    Quad {
        s: String,
        p: String,
        o: String,
        g: String,
    },
    /// A PREFIX declaration was recognised
    Prefix { prefix: String, iri: String },
    /// A BASE IRI declaration was recognised
    BaseIri(String),
    /// A parse error was encountered (parsing continues)
    Error(String),
    /// End of input — all buffered data has been processed
    End,
}

/// Cumulative parse statistics
#[derive(Debug, Clone, Default)]
pub struct ParseStats {
    pub triples: usize,
    pub quads: usize,
    pub prefixes: usize,
    pub errors: usize,
    pub bytes_processed: usize,
}

/// RDF serialisation formats understood by the parser
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StreamFormat {
    Turtle,
    NTriples,
    NQuads,
    TriG,
}

/// Configuration for the streaming parser
#[derive(Debug, Clone)]
pub struct StreamingParserConfig {
    pub format: StreamFormat,
    /// Whether to perform basic validation on IRIs / blank node labels
    pub validate: bool,
    /// Optional base IRI
    pub base_iri: Option<String>,
}

impl Default for StreamingParserConfig {
    fn default() -> Self {
        Self {
            format: StreamFormat::NTriples,
            validate: false,
            base_iri: None,
        }
    }
}

/// Incremental (chunk-based) RDF parser
pub struct StreamingParser {
    config: StreamingParserConfig,
    /// Partial line / incomplete token accumulation buffer
    buffer: String,
    stats: ParseStats,
    /// Events queued for retrieval by the caller
    events: VecDeque<ParseEvent>,
}

impl StreamingParser {
    /// Create a new streaming parser with the given configuration
    pub fn new(config: StreamingParserConfig) -> Self {
        let mut parser = Self {
            config,
            buffer: String::new(),
            stats: ParseStats::default(),
            events: VecDeque::new(),
        };

        // Emit a base IRI event if configured
        if let Some(ref base) = parser.config.base_iri.clone() {
            parser.events.push_back(ParseEvent::BaseIri(base.clone()));
        }

        parser
    }

    /// Feed a chunk of input to the parser.
    ///
    /// The chunk is appended to the internal buffer and complete lines / tokens
    /// are extracted and parsed immediately.  Parsed events are returned.
    pub fn feed(&mut self, chunk: &str) -> Vec<ParseEvent> {
        self.stats.bytes_processed += chunk.len();
        self.buffer.push_str(chunk);

        let mut emitted = Vec::new();
        self.process_buffer(&mut emitted);
        emitted
    }

    /// Flush remaining buffer content and emit an `End` event.
    ///
    /// Any incomplete line in the buffer is treated as an error (unless empty).
    pub fn flush(&mut self) -> Vec<ParseEvent> {
        let mut emitted = Vec::new();

        // Emit any queued events first
        while let Some(ev) = self.events.pop_front() {
            emitted.push(ev);
        }

        // Process remainder of buffer
        let leftover = std::mem::take(&mut self.buffer);
        let trimmed = leftover.trim();
        if !trimmed.is_empty() {
            // Try to parse the leftover as a complete token
            let mut chunk_events = Vec::new();
            self.parse_line(trimmed, &mut chunk_events);
            if chunk_events.is_empty() {
                self.stats.errors += 1;
                emitted.push(ParseEvent::Error(format!(
                    "Incomplete token at end of input: {trimmed}"
                )));
            } else {
                emitted.extend(chunk_events);
            }
        }

        emitted.push(ParseEvent::End);
        emitted
    }

    /// Reference to cumulative parse statistics
    pub fn stats(&self) -> &ParseStats {
        &self.stats
    }

    /// Number of events currently queued (not yet returned to the caller)
    pub fn pending_events(&self) -> usize {
        self.events.len()
    }

    /// Reset the parser to its initial state (clear buffer, stats and event queue)
    pub fn reset(&mut self) {
        self.buffer.clear();
        self.stats = ParseStats::default();
        self.events.clear();
    }

    // ─── private ─────────────────────────────────────────────────────────────

    /// Process all complete lines in the buffer
    fn process_buffer(&mut self, emitted: &mut Vec<ParseEvent>) {
        // Drain any pre-queued events first
        while let Some(ev) = self.events.pop_front() {
            emitted.push(ev);
        }

        while let Some(nl_pos) = self.buffer.find('\n') {
            let line = self.buffer[..nl_pos].to_string();
            self.buffer = self.buffer[nl_pos + 1..].to_string();

            let trimmed = line.trim();
            if trimmed.is_empty() || trimmed.starts_with('#') {
                continue; // blank / comment line
            }
            self.parse_line(trimmed, emitted);
        }
    }

    /// Parse a single complete line and push events into `emitted`
    fn parse_line(&mut self, line: &str, emitted: &mut Vec<ParseEvent>) {
        match self.config.format {
            StreamFormat::NTriples => self.parse_ntriples_line(line, emitted),
            StreamFormat::NQuads => self.parse_nquads_line(line, emitted),
            StreamFormat::Turtle => self.parse_turtle_line(line, emitted),
            StreamFormat::TriG => self.parse_trig_line(line, emitted),
        }
    }

    // ── N-Triples: <s> <p> <o> .  or  _:blank <p> <o> .
    fn parse_ntriples_line(&mut self, line: &str, emitted: &mut Vec<ParseEvent>) {
        // Directive check (shouldn't appear in N-Triples but handle gracefully)
        if line.starts_with('@') {
            self.stats.errors += 1;
            emitted.push(ParseEvent::Error(format!(
                "Directives not allowed in N-Triples: {line}"
            )));
            return;
        }

        let parts = split_ntriples(line);
        if parts.len() < 3 {
            if !line.trim_end_matches('.').trim().is_empty() {
                self.stats.errors += 1;
                emitted.push(ParseEvent::Error(format!("Invalid N-Triple: {line}")));
            }
            return;
        }

        // Always validate that terms have recognised N-Triples syntax
        // (must start with '<', '"', or '_:')
        for part in &parts[..3] {
            if let Err(msg) = validate_term(part) {
                self.stats.errors += 1;
                emitted.push(ParseEvent::Error(msg));
                return;
            }
        }

        // Optional strict validation (IRI closure etc.)
        if self.config.validate {
            // validate_term already covers the basic checks above; extended checks
            // would go here in future.
        }

        self.stats.triples += 1;
        emitted.push(ParseEvent::Triple {
            s: parts[0].clone(),
            p: parts[1].clone(),
            o: parts[2].clone(),
        });
    }

    // ── N-Quads: <s> <p> <o> <g> .
    fn parse_nquads_line(&mut self, line: &str, emitted: &mut Vec<ParseEvent>) {
        let parts = split_ntriples(line);
        if parts.len() < 4 {
            // Could be a triple (no graph)
            if parts.len() == 3 {
                self.stats.triples += 1;
                emitted.push(ParseEvent::Triple {
                    s: parts[0].clone(),
                    p: parts[1].clone(),
                    o: parts[2].clone(),
                });
            } else {
                self.stats.errors += 1;
                emitted.push(ParseEvent::Error(format!("Invalid N-Quad: {line}")));
            }
            return;
        }

        self.stats.quads += 1;
        emitted.push(ParseEvent::Quad {
            s: parts[0].clone(),
            p: parts[1].clone(),
            o: parts[2].clone(),
            g: parts[3].clone(),
        });
    }

    // ── Turtle: PREFIX declarations + triple lines
    fn parse_turtle_line(&mut self, line: &str, emitted: &mut Vec<ParseEvent>) {
        let upper = line.to_uppercase();

        // @prefix or PREFIX (SPARQL-style)
        if upper.starts_with("@PREFIX") || upper.starts_with("PREFIX") {
            if let Some(ev) = parse_prefix_directive(line) {
                self.stats.prefixes += 1;
                emitted.push(ev);
            } else {
                self.stats.errors += 1;
                emitted.push(ParseEvent::Error(format!(
                    "Malformed prefix directive: {line}"
                )));
            }
            return;
        }

        // @base or BASE
        if upper.starts_with("@BASE") || upper.starts_with("BASE") {
            if let Some(iri) = parse_base_directive(line) {
                emitted.push(ParseEvent::BaseIri(iri));
            } else {
                self.stats.errors += 1;
                emitted.push(ParseEvent::Error(format!(
                    "Malformed base directive: {line}"
                )));
            }
            return;
        }

        // Try triple parse
        let parts = split_ntriples(line);
        if parts.len() >= 3 {
            self.stats.triples += 1;
            emitted.push(ParseEvent::Triple {
                s: parts[0].clone(),
                p: parts[1].clone(),
                o: parts[2].clone(),
            });
        } else {
            self.stats.errors += 1;
            emitted.push(ParseEvent::Error(format!(
                "Unrecognised Turtle statement: {line}"
            )));
        }
    }

    // ── TriG: same as Turtle but also produces Quads for GRAPH blocks (simplified)
    fn parse_trig_line(&mut self, line: &str, emitted: &mut Vec<ParseEvent>) {
        let upper = line.to_uppercase();

        if upper.starts_with("@PREFIX") || upper.starts_with("PREFIX") {
            if let Some(ev) = parse_prefix_directive(line) {
                self.stats.prefixes += 1;
                emitted.push(ev);
            } else {
                self.stats.errors += 1;
                emitted.push(ParseEvent::Error(format!(
                    "Malformed prefix directive (TriG): {line}"
                )));
            }
            return;
        }

        if upper.starts_with("@BASE") || upper.starts_with("BASE") {
            if let Some(iri) = parse_base_directive(line) {
                emitted.push(ParseEvent::BaseIri(iri));
            } else {
                self.stats.errors += 1;
                emitted.push(ParseEvent::Error(format!(
                    "Malformed base directive (TriG): {line}"
                )));
            }
            return;
        }

        // N-Quads-style line
        let parts = split_ntriples(line);
        if parts.len() >= 4 {
            self.stats.quads += 1;
            emitted.push(ParseEvent::Quad {
                s: parts[0].clone(),
                p: parts[1].clone(),
                o: parts[2].clone(),
                g: parts[3].clone(),
            });
            return;
        }
        if parts.len() >= 3 {
            self.stats.triples += 1;
            emitted.push(ParseEvent::Triple {
                s: parts[0].clone(),
                p: parts[1].clone(),
                o: parts[2].clone(),
            });
            return;
        }

        // Structural TriG tokens (GRAPH keyword, braces) are silently skipped
        let upper_trim = line.trim().to_uppercase();
        if upper_trim == "{" || upper_trim == "}" || upper_trim.starts_with("GRAPH ") {
            return;
        }

        self.stats.errors += 1;
        emitted.push(ParseEvent::Error(format!(
            "Unrecognised TriG statement: {line}"
        )));
    }
}

// ─── Parsing helpers ──────────────────────────────────────────────────────────

/// Split an N-Triples / N-Quads line into terms, stripping the trailing dot
fn split_ntriples(line: &str) -> Vec<String> {
    let line = line.trim_end_matches('.').trim();
    let mut parts = Vec::new();
    let mut chars = line.chars().peekable();

    while let Some(&ch) = chars.peek() {
        match ch {
            ' ' | '\t' => {
                chars.next();
            }
            '<' => {
                // IRI
                chars.next();
                let mut iri = "<".to_string();
                for c in chars.by_ref() {
                    iri.push(c);
                    if c == '>' {
                        break;
                    }
                }
                parts.push(iri);
            }
            '"' => {
                // Literal — collect until unescaped closing quote, then optional lang/type
                let mut lit = String::new();
                lit.push('"');
                chars.next();
                let mut escaped = false;
                for c in chars.by_ref() {
                    if escaped {
                        lit.push(c);
                        escaped = false;
                    } else if c == '\\' {
                        lit.push(c);
                        escaped = true;
                    } else {
                        lit.push(c);
                        if c == '"' {
                            break;
                        }
                    }
                }
                // Optional @lang or ^^<type>
                while let Some(&nc) = chars.peek() {
                    if nc == '@' || nc == '^' {
                        let c = chars.next().expect("peeked char");
                        lit.push(c);
                    } else if nc == '<' {
                        // Datatype IRI
                        chars.next();
                        lit.push('<');
                        for c in chars.by_ref() {
                            lit.push(c);
                            if c == '>' {
                                break;
                            }
                        }
                    } else if nc.is_alphabetic() || nc == '-' {
                        lit.push(chars.next().expect("peeked char"));
                    } else {
                        break;
                    }
                }
                parts.push(lit);
            }
            '_' => {
                // Blank node _:label
                let mut bn = String::new();
                for c in chars.by_ref() {
                    if c == ' ' || c == '\t' {
                        break;
                    }
                    bn.push(c);
                }
                parts.push(bn);
            }
            _ => {
                // Unknown token — consume to next whitespace
                let mut tok = String::new();
                for c in chars.by_ref() {
                    if c == ' ' || c == '\t' {
                        break;
                    }
                    tok.push(c);
                }
                if !tok.is_empty() {
                    parts.push(tok);
                }
            }
        }
    }

    parts
}

/// Parse `@prefix foo: <iri> .` or `PREFIX foo: <iri>` → `ParseEvent::Prefix`
fn parse_prefix_directive(line: &str) -> Option<ParseEvent> {
    // Strip leading keyword
    let rest = line
        .trim_start_matches("@prefix")
        .trim_start_matches("@PREFIX")
        .trim_start_matches("PREFIX")
        .trim_start_matches("prefix")
        .trim();

    // Collect prefix label (up to ':')
    let colon = rest.find(':')?;
    let prefix = rest[..colon].trim().to_string();
    let after_colon = rest[colon + 1..].trim();

    // IRI in angle brackets
    let iri_start = after_colon.find('<')?;
    let iri_end = after_colon.rfind('>')?;
    if iri_end <= iri_start {
        return None;
    }
    let iri = after_colon[iri_start + 1..iri_end].to_string();

    Some(ParseEvent::Prefix { prefix, iri })
}

/// Parse `@base <iri> .` or `BASE <iri>` → base IRI string
fn parse_base_directive(line: &str) -> Option<String> {
    let rest = line
        .trim_start_matches("@base")
        .trim_start_matches("@BASE")
        .trim_start_matches("BASE")
        .trim_start_matches("base")
        .trim();

    let iri_start = rest.find('<')?;
    let iri_end = rest.rfind('>')?;
    if iri_end <= iri_start {
        return None;
    }
    Some(rest[iri_start + 1..iri_end].to_string())
}

/// Basic validation of an RDF term string
fn validate_term(term: &str) -> Result<(), String> {
    if term.starts_with('<') {
        if !term.ends_with('>') {
            return Err(format!("Unclosed IRI: {term}"));
        }
    } else if term.starts_with('"') {
        if !term.contains('"') {
            return Err(format!("Unclosed literal: {term}"));
        }
    } else if !term.starts_with("_:") {
        return Err(format!("Unknown term format: {term}"));
    }
    Ok(())
}

// ─── Tests ────────────────────────────────────────────────────────────────────

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

    fn ntriples_config() -> StreamingParserConfig {
        StreamingParserConfig {
            format: StreamFormat::NTriples,
            validate: false,
            base_iri: None,
        }
    }

    fn turtle_config() -> StreamingParserConfig {
        StreamingParserConfig {
            format: StreamFormat::Turtle,
            validate: false,
            base_iri: None,
        }
    }

    fn nquads_config() -> StreamingParserConfig {
        StreamingParserConfig {
            format: StreamFormat::NQuads,
            validate: false,
            base_iri: None,
        }
    }

    fn trig_config() -> StreamingParserConfig {
        StreamingParserConfig {
            format: StreamFormat::TriG,
            validate: false,
            base_iri: None,
        }
    }

    // ─── basic N-Triples feeding ──────────────────────────────────────────────

    #[test]
    fn test_feed_ntriples_single_line() {
        let mut p = StreamingParser::new(ntriples_config());
        let events = p.feed("<http://s> <http://p> <http://o> .\n");
        assert_eq!(events.len(), 1);
        assert!(matches!(events[0], ParseEvent::Triple { .. }));
    }

    #[test]
    fn test_feed_ntriples_stats_triples() {
        let mut p = StreamingParser::new(ntriples_config());
        p.feed("<http://s> <http://p> <http://o> .\n");
        p.feed("<http://s2> <http://p2> <http://o2> .\n");
        assert_eq!(p.stats().triples, 2);
    }

    #[test]
    fn test_feed_ntriples_subject_predicate_object() {
        let mut p = StreamingParser::new(ntriples_config());
        let events = p.feed("<http://s> <http://p> \"hello\" .\n");
        if let ParseEvent::Triple { s, p: pred, o } = &events[0] {
            assert_eq!(s, "<http://s>");
            assert_eq!(pred, "<http://p>");
            assert!(o.contains("hello"));
        } else {
            panic!("expected Triple event");
        }
    }

    #[test]
    fn test_feed_multiple_lines_in_one_chunk() {
        let mut p = StreamingParser::new(ntriples_config());
        let events =
            p.feed("<http://s1> <http://p> <http://o1> .\n<http://s2> <http://p> <http://o2> .\n");
        assert_eq!(events.len(), 2);
    }

    #[test]
    fn test_feed_empty_chunk_no_events() {
        let mut p = StreamingParser::new(ntriples_config());
        let events = p.feed("");
        assert!(events.is_empty());
    }

    #[test]
    fn test_feed_blank_lines_ignored() {
        let mut p = StreamingParser::new(ntriples_config());
        let events = p.feed("\n\n\n");
        assert!(events.is_empty());
    }

    #[test]
    fn test_feed_comment_lines_ignored() {
        let mut p = StreamingParser::new(ntriples_config());
        let events = p.feed("# This is a comment\n");
        assert!(events.is_empty());
    }

    // ─── partial line across chunks ───────────────────────────────────────────

    #[test]
    fn test_partial_line_across_chunks() {
        let mut p = StreamingParser::new(ntriples_config());
        let events1 = p.feed("<http://s> <http://p>");
        assert!(events1.is_empty(), "no complete line yet");
        let events2 = p.feed(" <http://o> .\n");
        assert_eq!(events2.len(), 1, "complete triple after second chunk");
    }

    #[test]
    fn test_bytes_processed_tracks_correctly() {
        let mut p = StreamingParser::new(ntriples_config());
        let chunk = "<http://s> <http://p> <http://o> .\n";
        p.feed(chunk);
        assert_eq!(p.stats().bytes_processed, chunk.len());
    }

    #[test]
    fn test_bytes_processed_accumulates() {
        let mut p = StreamingParser::new(ntriples_config());
        p.feed("abc");
        p.feed("def");
        assert_eq!(p.stats().bytes_processed, 6);
    }

    // ─── flush emits End ──────────────────────────────────────────────────────

    #[test]
    fn test_flush_emits_end() {
        let mut p = StreamingParser::new(ntriples_config());
        p.feed("<http://s> <http://p> <http://o> .\n");
        let flush_events = p.flush();
        assert!(
            flush_events.contains(&ParseEvent::End),
            "flush must emit End"
        );
    }

    #[test]
    fn test_flush_empty_parser_emits_end() {
        let mut p = StreamingParser::new(ntriples_config());
        let events = p.flush();
        assert_eq!(events, vec![ParseEvent::End]);
    }

    #[test]
    fn test_flush_incomplete_line_is_error() {
        let mut p = StreamingParser::new(ntriples_config());
        p.feed("<http://s> incomplete");
        let events = p.flush();
        let has_error = events.iter().any(|e| matches!(e, ParseEvent::Error(_)));
        assert!(has_error, "incomplete buffer should yield an Error event");
        assert!(events.contains(&ParseEvent::End));
    }

    // ─── reset ────────────────────────────────────────────────────────────────

    #[test]
    fn test_reset_clears_buffer() {
        let mut p = StreamingParser::new(ntriples_config());
        p.feed("<partial>");
        p.reset();
        let events = p.flush();
        // After reset, buffer is cleared — only End
        assert_eq!(events, vec![ParseEvent::End]);
    }

    #[test]
    fn test_reset_clears_stats() {
        let mut p = StreamingParser::new(ntriples_config());
        p.feed("<http://s> <http://p> <http://o> .\n");
        p.reset();
        assert_eq!(p.stats().triples, 0);
        assert_eq!(p.stats().bytes_processed, 0);
    }

    #[test]
    fn test_reset_clears_events() {
        let mut p = StreamingParser::new(ntriples_config());
        p.feed("<http://s> <http://p> <http://o> .\n");
        p.reset();
        assert_eq!(p.pending_events(), 0);
    }

    // ─── Turtle: PREFIX ───────────────────────────────────────────────────────

    #[test]
    fn test_turtle_prefix_event() {
        let mut p = StreamingParser::new(turtle_config());
        let events = p.feed("@prefix ex: <http://example.org/> .\n");
        let prefix_ev = events
            .iter()
            .find(|e| matches!(e, ParseEvent::Prefix { .. }));
        assert!(prefix_ev.is_some(), "should emit Prefix event");
    }

    #[test]
    fn test_turtle_prefix_stats() {
        let mut p = StreamingParser::new(turtle_config());
        p.feed("@prefix ex: <http://example.org/> .\n");
        p.feed("@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n");
        assert_eq!(p.stats().prefixes, 2);
    }

    #[test]
    fn test_turtle_prefix_values() {
        let mut p = StreamingParser::new(turtle_config());
        let events = p.feed("PREFIX foo: <http://foo.org/> \n");
        if let Some(ParseEvent::Prefix { prefix, iri }) = events.first() {
            assert_eq!(prefix, "foo");
            assert_eq!(iri, "http://foo.org/");
        } else {
            panic!("expected Prefix event, got {:?}", events);
        }
    }

    #[test]
    fn test_turtle_base_iri_event() {
        let mut p = StreamingParser::new(turtle_config());
        let events = p.feed("@base <http://base.example.org/> .\n");
        let base_ev = events.iter().find(|e| matches!(e, ParseEvent::BaseIri(_)));
        assert!(base_ev.is_some(), "should emit BaseIri event");
    }

    #[test]
    fn test_turtle_triple_after_prefix() {
        let mut p = StreamingParser::new(turtle_config());
        p.feed("@prefix ex: <http://example.org/> .\n");
        let events = p.feed("<http://s> <http://p> <http://o> .\n");
        assert!(events
            .iter()
            .any(|e| matches!(e, ParseEvent::Triple { .. })));
    }

    // ─── N-Quads ──────────────────────────────────────────────────────────────

    #[test]
    fn test_nquads_emits_quad_event() {
        let mut p = StreamingParser::new(nquads_config());
        let events = p.feed("<http://s> <http://p> <http://o> <http://g> .\n");
        assert!(events.iter().any(|e| matches!(e, ParseEvent::Quad { .. })));
    }

    #[test]
    fn test_nquads_stats_quads() {
        let mut p = StreamingParser::new(nquads_config());
        p.feed("<http://s> <http://p> <http://o> <http://g> .\n");
        p.feed("<http://s2> <http://p2> <http://o2> <http://g2> .\n");
        assert_eq!(p.stats().quads, 2);
    }

    #[test]
    fn test_nquads_quad_fields() {
        let mut p = StreamingParser::new(nquads_config());
        let events = p.feed("<http://s> <http://p> <http://o> <http://g> .\n");
        if let ParseEvent::Quad { s, p: pred, o, g } = &events[0] {
            assert_eq!(s, "<http://s>");
            assert_eq!(pred, "<http://p>");
            assert_eq!(o, "<http://o>");
            assert_eq!(g, "<http://g>");
        } else {
            panic!("expected Quad event, got {:?}", events);
        }
    }

    // ─── Validation ───────────────────────────────────────────────────────────

    #[test]
    fn test_validate_flag_enabled_error_on_bad_term() {
        let config = StreamingParserConfig {
            format: StreamFormat::NTriples,
            validate: true,
            base_iri: None,
        };
        let mut p = StreamingParser::new(config);
        // Unclosed IRI
        let events = p.feed("<http://s <http://p> <http://o> .\n");
        assert!(events.iter().any(|e| matches!(e, ParseEvent::Error(_))));
    }

    #[test]
    fn test_validate_flag_disabled_no_error_on_partial() {
        let config = StreamingParserConfig {
            format: StreamFormat::NTriples,
            validate: false,
            base_iri: None,
        };
        let mut p = StreamingParser::new(config);
        // Only 2 terms — not a valid triple but validate=false
        let events = p.feed("<http://s> <http://p> .\n");
        // Should emit an error because parts < 3, but no validation-level check
        let _ = events; // just ensure no panic
    }

    // ─── base IRI config ──────────────────────────────────────────────────────

    #[test]
    fn test_base_iri_config_emits_event_on_new() {
        let config = StreamingParserConfig {
            format: StreamFormat::Turtle,
            validate: false,
            base_iri: Some("http://base.example.org/".to_string()),
        };
        let p = StreamingParser::new(config);
        // The event is queued but not yet returned (pending_events)
        assert_eq!(p.pending_events(), 1);
    }

    // ─── TriG ─────────────────────────────────────────────────────────────────

    #[test]
    fn test_trig_quad_event() {
        let mut p = StreamingParser::new(trig_config());
        let events = p.feed("<http://s> <http://p> <http://o> <http://g> .\n");
        assert!(events.iter().any(|e| matches!(e, ParseEvent::Quad { .. })));
    }

    #[test]
    fn test_trig_prefix_event() {
        let mut p = StreamingParser::new(trig_config());
        let events = p.feed("PREFIX g: <http://graph.org/> \n");
        assert!(events
            .iter()
            .any(|e| matches!(e, ParseEvent::Prefix { .. })));
    }

    // ─── error stat ───────────────────────────────────────────────────────────

    #[test]
    fn test_error_stat_increments() {
        let mut p = StreamingParser::new(ntriples_config());
        p.feed("this is garbage\n");
        assert!(p.stats().errors > 0);
    }

    #[test]
    fn test_stats_triple_count_accumulated() {
        let mut p = StreamingParser::new(ntriples_config());
        for _ in 0..5 {
            p.feed("<http://s> <http://p> <http://o> .\n");
        }
        assert_eq!(p.stats().triples, 5);
    }

    #[test]
    fn test_flush_includes_remaining_triples() {
        let mut p = StreamingParser::new(ntriples_config());
        // Feed without trailing newline
        p.feed("<http://s> <http://p> <http://o> .");
        let events = p.flush();
        // The leftover should be parsed as a triple
        let has_triple = events
            .iter()
            .any(|e| matches!(e, ParseEvent::Triple { .. }));
        let has_end = events.contains(&ParseEvent::End);
        assert!(has_triple, "flush should parse leftover triple");
        assert!(has_end, "flush should emit End");
    }

    // ─── Additional tests (round 11 extra coverage) ───────────────────────────

    #[test]
    fn test_stats_bytes_processed() {
        let mut p = StreamingParser::new(ntriples_config());
        let chunk = "<http://s> <http://p> <http://o> .\n";
        p.feed(chunk);
        assert_eq!(p.stats().bytes_processed, chunk.len());
    }

    #[test]
    fn test_stats_bytes_accumulate() {
        let mut p = StreamingParser::new(ntriples_config());
        let chunk = "<http://s> <http://p> <http://o> .\n";
        p.feed(chunk);
        p.feed(chunk);
        assert_eq!(p.stats().bytes_processed, chunk.len() * 2);
    }

    #[test]
    fn test_ntriples_literal_subject() {
        let mut p = StreamingParser::new(ntriples_config());
        let events = p.feed("\"hello\" <http://p> <http://o> .\n");
        // "hello" starts with '"' which is a valid N-Triples term
        let has_triple = events
            .iter()
            .any(|e| matches!(e, ParseEvent::Triple { .. }));
        assert!(has_triple, "literal subject should produce Triple");
    }

    #[test]
    fn test_ntriples_blank_node_subject() {
        let mut p = StreamingParser::new(ntriples_config());
        let events = p.feed("_:blank <http://p> <http://o> .\n");
        let has_triple = events
            .iter()
            .any(|e| matches!(e, ParseEvent::Triple { .. }));
        assert!(has_triple, "blank node subject should produce Triple");
    }

    #[test]
    fn test_pending_events_count() {
        let p = StreamingParser::new(ntriples_config());
        assert_eq!(p.pending_events(), 0);
    }

    #[test]
    fn test_base_iri_config_bytes_counted() {
        let config = StreamingParserConfig {
            format: StreamFormat::NTriples,
            validate: false,
            base_iri: Some("http://base.org/".to_string()),
        };
        let p = StreamingParser::new(config);
        // After construction, no bytes processed
        assert_eq!(p.stats().bytes_processed, 0);
    }

    #[test]
    fn test_ntriples_multiple_triples_count() {
        let mut p = StreamingParser::new(ntriples_config());
        for _ in 0..10 {
            p.feed("<http://s> <http://p> <http://o> .\n");
        }
        assert_eq!(p.stats().triples, 10);
    }

    #[test]
    fn test_ntriples_comment_not_counted() {
        let mut p = StreamingParser::new(ntriples_config());
        p.feed("# This is a comment\n");
        assert_eq!(p.stats().triples, 0);
        assert_eq!(p.stats().errors, 0);
    }

    #[test]
    fn test_format_ntriples_enum() {
        let config = StreamingParserConfig {
            format: StreamFormat::NTriples,
            validate: false,
            base_iri: None,
        };
        assert_eq!(config.format, StreamFormat::NTriples);
    }

    #[test]
    fn test_format_nquads_enum() {
        assert_eq!(StreamFormat::NQuads, StreamFormat::NQuads);
    }

    #[test]
    fn test_reset_zeroes_bytes_processed() {
        let mut p = StreamingParser::new(ntriples_config());
        p.feed("<http://s> <http://p> <http://o> .\n");
        assert!(p.stats().bytes_processed > 0);
        p.reset();
        assert_eq!(p.stats().bytes_processed, 0);
    }

    #[test]
    fn test_turtle_base_event_value() {
        let mut p = StreamingParser::new(turtle_config());
        let events = p.feed("BASE <http://base.example.org/>\n");
        let base_event = events.iter().find(|e| matches!(e, ParseEvent::BaseIri(_)));
        assert!(
            base_event.is_some(),
            "BASE directive should emit BaseIri event"
        );
        if let Some(ParseEvent::BaseIri(iri)) = base_event {
            assert!(
                iri.contains("base.example.org"),
                "IRI should contain domain"
            );
        }
    }

    #[test]
    fn test_parse_event_error_message() {
        let mut p = StreamingParser::new(ntriples_config());
        let events = p.feed("garbage line without valid rdf terms\n");
        // Should produce an Error event
        let error_event = events.iter().find(|e| matches!(e, ParseEvent::Error(_)));
        assert!(
            error_event.is_some(),
            "invalid line should produce Error event"
        );
    }
}