oxirs-core 0.4.1

Core RDF and SPARQL functionality for OxiRS - native Rust implementation with zero dependencies
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
//! RDF-star (RDF*) support for statement annotations
//!
//! This module implements RDF-star extensions for SPARQL 1.2 compliance,
//! allowing triples to be used as subjects or objects in other triples.

use crate::model::{
    NamedNode, Object, ObjectTerm, Predicate, RdfTerm, Subject, SubjectTerm, Triple,
};
use crate::query::algebra::{AlgebraTriplePattern, TermPattern};
use crate::OxirsError;
use std::fmt;
use std::sync::Arc;

/// A quoted triple that can be used as a subject or object in RDF-star
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct QuotedTriple {
    /// The inner triple being quoted
    inner: Arc<Triple>,
}

impl QuotedTriple {
    /// Create a new quoted triple
    pub fn new(triple: Triple) -> Self {
        QuotedTriple {
            inner: Arc::new(triple),
        }
    }

    /// Create from an existing `Arc<Triple>`
    pub fn from_arc(triple: Arc<Triple>) -> Self {
        QuotedTriple { inner: triple }
    }

    /// Get the inner triple
    pub fn inner(&self) -> &Triple {
        &self.inner
    }

    /// Get the subject of the quoted triple
    pub fn subject(&self) -> &Subject {
        self.inner.subject()
    }

    /// Get the predicate of the quoted triple
    pub fn predicate(&self) -> &Predicate {
        self.inner.predicate()
    }

    /// Get the object of the quoted triple
    pub fn object(&self) -> &Object {
        self.inner.object()
    }

    /// Convert to a triple reference
    pub fn as_ref(&self) -> QuotedTripleRef<'_> {
        QuotedTripleRef { inner: &self.inner }
    }
}

impl fmt::Display for QuotedTriple {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // `Triple`'s own `Display` appends a trailing " ." (it formats a
        // top-level Turtle statement), which is not valid inside `<< ... >>`
        // -- `<< s p o . >>` fails to re-parse ("expected exactly 3 terms").
        // Format the s/p/o components directly instead, matching
        // `serialize_quoted_triple` below.
        write!(
            f,
            "<< {} {} {} >>",
            self.inner.subject(),
            self.inner.predicate(),
            self.inner.object()
        )
    }
}

impl RdfTerm for QuotedTriple {
    fn as_str(&self) -> &str {
        // For quoted triples, we return a synthetic string representation
        "<<quoted-triple>>"
    }

    fn is_quoted_triple(&self) -> bool {
        true
    }
}

impl SubjectTerm for QuotedTriple {}
impl ObjectTerm for QuotedTriple {}

// Custom serialization for QuotedTriple to handle Arc<Triple>
#[cfg(feature = "serde")]
impl serde::Serialize for QuotedTriple {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        // Serialize the inner triple directly
        self.inner.as_ref().serialize(serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for QuotedTriple {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let triple = Triple::deserialize(deserializer)?;
        Ok(QuotedTriple::new(triple))
    }
}

/// A borrowed quoted triple reference
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct QuotedTripleRef<'a> {
    inner: &'a Triple,
}

impl<'a> QuotedTripleRef<'a> {
    /// Create a new quoted triple reference
    pub fn new(triple: &'a Triple) -> Self {
        QuotedTripleRef { inner: triple }
    }

    /// Get the inner triple
    pub fn inner(&self) -> &'a Triple {
        self.inner
    }

    /// Convert to owned quoted triple
    pub fn to_owned(&self) -> QuotedTriple {
        QuotedTriple::new(self.inner.clone())
    }
}

impl<'a> fmt::Display for QuotedTripleRef<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // See `QuotedTriple`'s `Display` impl: avoid `Triple`'s trailing
        // " ." statement terminator, which is invalid inside `<< ... >>`.
        write!(
            f,
            "<< {} {} {} >>",
            self.inner.subject(),
            self.inner.predicate(),
            self.inner.object()
        )
    }
}

impl<'a> RdfTerm for QuotedTripleRef<'a> {
    fn as_str(&self) -> &str {
        "<<quoted-triple>>"
    }

    fn is_quoted_triple(&self) -> bool {
        true
    }
}

/// RDF-star annotation syntax support
pub struct Annotation {
    /// The statement being annotated (as a quoted triple)
    pub statement: QuotedTriple,
    /// The annotation property
    pub property: NamedNode,
    /// The annotation value
    pub value: Object,
}

impl Annotation {
    /// Create a new annotation
    pub fn new(statement: Triple, property: NamedNode, value: Object) -> Self {
        Annotation {
            statement: QuotedTriple::new(statement),
            property,
            value,
        }
    }

    /// Convert annotation to a regular triple with quoted triple as subject
    pub fn to_triple(&self) -> Triple {
        Triple::new(
            Subject::QuotedTriple(Box::new(self.statement.clone())),
            self.property.clone(),
            self.value.clone(),
        )
    }
}

/// RDF-star pattern for SPARQL queries
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum StarPattern {
    /// A regular triple pattern
    Triple(AlgebraTriplePattern),
    /// A quoted triple pattern
    QuotedTriple {
        /// Subject pattern (can be nested quoted triple)
        subject: Box<StarPattern>,
        /// Predicate pattern
        predicate: TermPattern,
        /// Object pattern (can be nested quoted triple)
        object: Box<StarPattern>,
    },
    /// An annotation pattern
    Annotation {
        /// The annotated statement pattern
        statement: Box<StarPattern>,
        /// Annotation property pattern
        property: TermPattern,
        /// Annotation value pattern
        value: TermPattern,
    },
}

impl StarPattern {
    /// Check if pattern contains variables
    pub fn has_variables(&self) -> bool {
        match self {
            StarPattern::Triple(pattern) => {
                matches!(pattern.subject, TermPattern::Variable(_))
                    || matches!(pattern.predicate, TermPattern::Variable(_))
                    || matches!(pattern.object, TermPattern::Variable(_))
            }
            StarPattern::QuotedTriple {
                subject,
                predicate: _,
                object,
            } => subject.has_variables() || object.has_variables(),
            StarPattern::Annotation {
                statement,
                property: _,
                value: _,
            } => statement.has_variables(),
        }
    }

    /// Get all variables in the pattern
    pub fn variables(&self) -> Vec<crate::model::Variable> {
        let mut vars = Vec::new();
        self.collect_variables(&mut vars);
        vars
    }

    fn collect_variables(&self, _vars: &mut Vec<crate::model::Variable>) {
        match self {
            StarPattern::Triple(pattern) => {
                if let TermPattern::Variable(ref v) = pattern.subject {
                    _vars.push(v.clone());
                }
                if let TermPattern::Variable(ref v) = pattern.predicate {
                    _vars.push(v.clone());
                }
                if let TermPattern::Variable(ref v) = pattern.object {
                    _vars.push(v.clone());
                }
            }
            StarPattern::QuotedTriple {
                subject,
                predicate: _,
                object,
            } => {
                subject.collect_variables(_vars);
                object.collect_variables(_vars);
            }
            StarPattern::Annotation {
                statement,
                property: _,
                value: _,
            } => {
                statement.collect_variables(_vars);
            }
        }
    }
}

/// RDF-star serialization format extensions
pub mod serialization {
    use super::*;

    /// Turtle-star syntax extensions
    pub mod turtle_star {
        use super::*;

        /// Serialize a quoted triple in Turtle-star syntax
        pub fn serialize_quoted_triple(qt: &QuotedTriple) -> String {
            format!("<< {} {} {} >>", qt.subject(), qt.predicate(), qt.object())
        }

        /// Parse a quoted triple from Turtle-star syntax.
        ///
        /// Supports the RDF-star subject/object term grammar: absolute IRIs
        /// (`<...>`), blank nodes (`_:id`), plain/typed/language-tagged
        /// literals (`"..."`, `"..."^^<...>`, `"..."@lang`), and nested
        /// quoted triples (`<< ... >>`). This is a self-contained
        /// tokenizer scoped to the `<< s p o >>` grammar rather than a
        /// full Turtle document parser; prefixed names (`ex:foo`) are not
        /// supported since there is no prefix map in scope here.
        pub fn parse_quoted_triple(input: &str) -> Result<QuotedTriple, OxirsError> {
            let trimmed = input.trim();
            if !trimmed.starts_with("<<") || !trimmed.ends_with(">>") || trimmed.len() < 4 {
                return Err(OxirsError::Parse(
                    "Invalid quoted triple syntax: expected '<< subject predicate object >>'"
                        .to_string(),
                ));
            }

            let inner = trimmed[2..trimmed.len() - 2].trim();
            let terms = split_star_terms(inner)?;
            if terms.len() != 3 {
                return Err(OxirsError::Parse(format!(
                    "Invalid quoted triple: expected exactly 3 terms (subject, predicate, object), found {}",
                    terms.len()
                )));
            }

            let subject = parse_star_subject(terms[0])?;
            let predicate = parse_star_predicate(terms[1])?;
            let object = parse_star_object(terms[2])?;

            Ok(QuotedTriple::new(Triple::new(subject, predicate, object)))
        }

        /// Split the space-separated `subject predicate object` term list
        /// of a quoted triple, respecting nesting of `<< ... >>`, `<...>`,
        /// and `"..."` (including escaped quotes) so that whitespace
        /// inside those constructs does not cause a spurious split.
        fn split_star_terms(input: &str) -> Result<Vec<&str>, OxirsError> {
            let bytes = input.as_bytes();
            let mut terms = Vec::new();
            let mut i = 0usize;
            let mut depth = 0i32;
            let mut in_string = false;
            let mut escape = false;
            let mut term_start: Option<usize> = None;

            while i < bytes.len() {
                let c = bytes[i] as char;
                if in_string {
                    if escape {
                        escape = false;
                    } else if c == '\\' {
                        escape = true;
                    } else if c == '"' {
                        in_string = false;
                    }
                    i += 1;
                    continue;
                }

                match c {
                    '"' => {
                        in_string = true;
                        if term_start.is_none() {
                            term_start = Some(i);
                        }
                    }
                    '<' if input[i..].starts_with("<<") => {
                        depth += 1;
                        if term_start.is_none() {
                            term_start = Some(i);
                        }
                        i += 1; // consume the extra '<' of "<<"
                    }
                    '>' if input[i..].starts_with(">>") => {
                        depth -= 1;
                        if depth < 0 {
                            return Err(OxirsError::Parse(
                                "Unbalanced '>>' in quoted triple term".to_string(),
                            ));
                        }
                        i += 1; // consume the extra '>' of ">>"
                    }
                    '<' => {
                        if term_start.is_none() {
                            term_start = Some(i);
                        }
                    }
                    c if c.is_whitespace() && depth == 0 => {
                        if let Some(start) = term_start.take() {
                            terms.push(input[start..i].trim());
                        }
                    }
                    _ => {
                        if term_start.is_none() {
                            term_start = Some(i);
                        }
                    }
                }
                i += 1;
            }
            if in_string {
                return Err(OxirsError::Parse(
                    "Unterminated string literal in quoted triple".to_string(),
                ));
            }
            if depth != 0 {
                return Err(OxirsError::Parse(
                    "Unbalanced '<<'/'>>' nesting in quoted triple".to_string(),
                ));
            }
            if let Some(start) = term_start {
                terms.push(input[start..].trim());
            }
            Ok(terms.into_iter().filter(|t| !t.is_empty()).collect())
        }

        fn parse_star_iri(term: &str) -> Result<NamedNode, OxirsError> {
            let inner = term
                .strip_prefix('<')
                .and_then(|s| s.strip_suffix('>'))
                .ok_or_else(|| OxirsError::Parse(format!("Invalid IRI term: {term}")))?;
            NamedNode::new(inner)
        }

        fn parse_star_literal(term: &str) -> Result<crate::model::Literal, OxirsError> {
            use crate::model::Literal;

            // Locate the closing quote of the (possibly escaped) string body.
            let bytes = term.as_bytes();
            if bytes.first() != Some(&b'"') {
                return Err(OxirsError::Parse(format!("Invalid literal term: {term}")));
            }
            let mut end = None;
            let mut escape = false;
            for (idx, b) in bytes.iter().enumerate().skip(1) {
                if escape {
                    escape = false;
                } else if *b == b'\\' {
                    escape = true;
                } else if *b == b'"' {
                    end = Some(idx);
                    break;
                }
            }
            let end =
                end.ok_or_else(|| OxirsError::Parse(format!("Unterminated literal: {term}")))?;
            let raw_value = &term[1..end];
            let value = unescape_star_string(raw_value)?;
            let suffix = &term[end + 1..];

            if let Some(lang) = suffix.strip_prefix('@') {
                Literal::new_language_tagged_literal(value, lang)
                    .map_err(|e| OxirsError::Parse(format!("Invalid language tag: {e}")))
            } else if let Some(datatype) = suffix.strip_prefix("^^") {
                let datatype = parse_star_iri(datatype)?;
                Ok(Literal::new_typed(value, datatype))
            } else if suffix.is_empty() {
                Ok(Literal::new(value))
            } else {
                Err(OxirsError::Parse(format!(
                    "Invalid literal suffix in term: {term}"
                )))
            }
        }

        /// Consume exactly `digits` hex characters from `chars` and return the
        /// decoded `u32` codepoint. Used for `\uXXXX` (4 hex digits) and
        /// `\UXXXXXXXX` (8 hex digits) escapes.
        fn read_hex_escape(
            chars: &mut std::str::Chars<'_>,
            digits: usize,
            kind: char,
        ) -> Result<u32, OxirsError> {
            let mut value: u32 = 0;
            for _ in 0..digits {
                let c = chars.next().ok_or_else(|| {
                    OxirsError::Parse(format!(
                        "Truncated \\{kind} escape in quoted triple literal: expected {digits} hex digits"
                    ))
                })?;
                let digit = c.to_digit(16).ok_or_else(|| {
                    OxirsError::Parse(format!(
                        "Invalid hex digit '{c}' in \\{kind} escape in quoted triple literal"
                    ))
                })?;
                value = (value << 4) | digit;
            }
            Ok(value)
        }

        /// Unescape a quoted-triple literal's lexical body.
        ///
        /// Mirrors the N-Triples/Turtle `ECHAR`/`UCHAR` grammar used by this
        /// module's own serializer ([`crate::model::literal::print_quoted_str`]):
        /// `\t \b \n \r \f \" \' \\` plus `\uXXXX` (4 hex digits) and
        /// `\UXXXXXXXX` (8 hex digits) Unicode escapes. Without `\b`/`\f`/
        /// `\u`/`\U` support, a quoted-triple literal containing a control
        /// character or non-ASCII-escaped codepoint would serialize
        /// correctly but fail to re-parse.
        fn unescape_star_string(raw: &str) -> Result<String, OxirsError> {
            let mut result = String::with_capacity(raw.len());
            let mut chars = raw.chars();
            while let Some(c) = chars.next() {
                if c != '\\' {
                    result.push(c);
                    continue;
                }
                match chars.next() {
                    Some('n') => result.push('\n'),
                    Some('t') => result.push('\t'),
                    Some('r') => result.push('\r'),
                    Some('b') => result.push('\u{08}'),
                    Some('f') => result.push('\u{0C}'),
                    Some('"') => result.push('"'),
                    Some('\'') => result.push('\''),
                    Some('\\') => result.push('\\'),
                    Some('u') => {
                        let codepoint = read_hex_escape(&mut chars, 4, 'u')?;
                        let c = char::from_u32(codepoint).ok_or_else(|| {
                            OxirsError::Parse(format!(
                                "Invalid Unicode codepoint U+{codepoint:04X} in \\u escape in quoted triple literal"
                            ))
                        })?;
                        result.push(c);
                    }
                    Some('U') => {
                        let codepoint = read_hex_escape(&mut chars, 8, 'U')?;
                        let c = char::from_u32(codepoint).ok_or_else(|| {
                            OxirsError::Parse(format!(
                                "Invalid Unicode codepoint U+{codepoint:08X} in \\U escape in quoted triple literal"
                            ))
                        })?;
                        result.push(c);
                    }
                    Some(other) => {
                        return Err(OxirsError::Parse(format!(
                            "Unsupported escape sequence '\\{other}' in quoted triple literal"
                        )))
                    }
                    None => {
                        return Err(OxirsError::Parse(
                            "Trailing backslash in quoted triple literal".to_string(),
                        ))
                    }
                }
            }
            Ok(result)
        }

        fn parse_star_subject(term: &str) -> Result<Subject, OxirsError> {
            if term.starts_with("<<") {
                Ok(Subject::QuotedTriple(Box::new(parse_quoted_triple(term)?)))
            } else if let Some(id) = term.strip_prefix("_:") {
                Ok(Subject::BlankNode(crate::model::BlankNode::new(id)?))
            } else if term.starts_with('<') {
                Ok(Subject::NamedNode(parse_star_iri(term)?))
            } else {
                Err(OxirsError::Parse(format!(
                    "Unsupported subject term in quoted triple: {term}"
                )))
            }
        }

        fn parse_star_predicate(term: &str) -> Result<Predicate, OxirsError> {
            if term.starts_with('<') {
                Ok(Predicate::NamedNode(parse_star_iri(term)?))
            } else {
                Err(OxirsError::Parse(format!(
                    "Unsupported predicate term in quoted triple: {term}"
                )))
            }
        }

        fn parse_star_object(term: &str) -> Result<Object, OxirsError> {
            if term.starts_with("<<") {
                Ok(Object::QuotedTriple(Box::new(parse_quoted_triple(term)?)))
            } else if let Some(id) = term.strip_prefix("_:") {
                Ok(Object::BlankNode(crate::model::BlankNode::new(id)?))
            } else if term.starts_with('<') {
                Ok(Object::NamedNode(parse_star_iri(term)?))
            } else if term.starts_with('"') {
                Ok(Object::Literal(parse_star_literal(term)?))
            } else {
                Err(OxirsError::Parse(format!(
                    "Unsupported object term in quoted triple: {term}"
                )))
            }
        }
    }

    /// SPARQL-star syntax extensions
    pub mod sparql_star {
        use super::*;

        /// Format a star pattern for SPARQL
        pub fn format_star_pattern(pattern: &StarPattern) -> String {
            match pattern {
                StarPattern::Triple(pattern) => pattern.to_string(),
                StarPattern::QuotedTriple {
                    subject,
                    predicate: _,
                    object,
                } => {
                    format!(
                        "<< {} {} {} >>",
                        format_star_pattern(subject),
                        "PREDICATE",
                        format_star_pattern(object)
                    )
                }
                StarPattern::Annotation {
                    statement,
                    property: _,
                    value: _,
                } => {
                    format!(
                        "{} {} {}",
                        format_star_pattern(statement),
                        "PROPERTY",
                        "VALUE"
                    )
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{Literal, NamedNode};

    #[test]
    fn test_quoted_triple() {
        let subject = NamedNode::new("http://example.org/alice").expect("valid IRI");
        let predicate = NamedNode::new("http://example.org/says").expect("valid IRI");
        let object = Object::Literal(Literal::new("Hello"));

        let triple = Triple::new(subject, predicate, object);
        let quoted = QuotedTriple::new(triple.clone());

        assert_eq!(quoted.inner(), &triple);
        // No trailing " ." before the closing ">>": that form is not valid
        // RDF-star/Turtle-star syntax and doesn't round-trip through
        // `parse_quoted_triple` (see regression test below).
        assert_eq!(
            format!("{quoted}"),
            "<< <http://example.org/alice> <http://example.org/says> \"Hello\" >>"
        );
    }

    /// Regression test: `Display` output for `QuotedTriple`/`QuotedTripleRef`
    /// must be valid RDF-star syntax that round-trips through
    /// `parse_quoted_triple`, matching the hand-rolled `serialize_quoted_triple`
    /// exactly (both formatters must agree).
    #[test]
    fn regression_quoted_triple_display_round_trips() {
        use serialization::turtle_star::{parse_quoted_triple, serialize_quoted_triple};

        let subject = NamedNode::new("http://example.org/alice").expect("valid IRI");
        let predicate = NamedNode::new("http://example.org/says").expect("valid IRI");
        let object = Object::Literal(Literal::new("Hello"));
        let triple = Triple::new(subject, predicate, object);
        let quoted = QuotedTriple::new(triple);

        let displayed = format!("{quoted}");
        assert!(
            !displayed.contains(". >>"),
            "Display output must not contain a trailing statement terminator: {displayed}"
        );

        // `Display` and the hand-rolled serializer must agree.
        assert_eq!(displayed, serialize_quoted_triple(&quoted));

        // And the result must actually re-parse back to the same triple.
        let reparsed = parse_quoted_triple(&displayed).expect("Display output must re-parse");
        assert_eq!(reparsed, quoted);

        // `QuotedTripleRef`'s `Display` must match too.
        let triple_ref = quoted.inner().clone();
        let quoted_ref = QuotedTripleRef::new(&triple_ref);
        assert_eq!(format!("{quoted_ref}"), displayed);
    }

    #[test]
    fn test_annotation() {
        let subject = NamedNode::new("http://example.org/alice").expect("valid IRI");
        let predicate = NamedNode::new("http://example.org/age").expect("valid IRI");
        let object = Object::Literal(Literal::new_typed(
            "30",
            NamedNode::new("http://www.w3.org/2001/XMLSchema#integer").expect("valid IRI"),
        ));

        let statement = Triple::new(subject, predicate, object);
        let ann_property = NamedNode::new("http://example.org/confidence").expect("valid IRI");
        let ann_value = Object::Literal(Literal::new_typed(
            "0.9",
            NamedNode::new("http://www.w3.org/2001/XMLSchema#double").expect("valid IRI"),
        ));

        let annotation = Annotation::new(statement, ann_property, ann_value);
        let ann_triple = annotation.to_triple();

        assert!(matches!(ann_triple.subject(), Subject::QuotedTriple(_)));
    }

    #[test]
    fn test_parse_quoted_triple_basic() {
        use serialization::turtle_star::parse_quoted_triple;

        let parsed = parse_quoted_triple(
            "<< <http://example.org/alice> <http://example.org/says> \"Hello\" >>",
        )
        .expect("valid quoted triple");

        assert_eq!(
            parsed.subject(),
            &Subject::NamedNode(NamedNode::new("http://example.org/alice").expect("valid IRI"))
        );
        assert_eq!(
            parsed.predicate(),
            &Predicate::NamedNode(NamedNode::new("http://example.org/says").expect("valid IRI"))
        );
        assert_eq!(parsed.object(), &Object::Literal(Literal::new("Hello")));
    }

    #[test]
    fn test_parse_quoted_triple_roundtrip_with_serialize() {
        use serialization::turtle_star::{parse_quoted_triple, serialize_quoted_triple};

        let subject = NamedNode::new("http://example.org/alice").expect("valid IRI");
        let predicate = NamedNode::new("http://example.org/age").expect("valid IRI");
        let object = Object::Literal(Literal::new_typed(
            "30",
            NamedNode::new("http://www.w3.org/2001/XMLSchema#integer").expect("valid IRI"),
        ));
        let original = QuotedTriple::new(Triple::new(subject, predicate, object));

        let text = serialize_quoted_triple(&original);
        let parsed = parse_quoted_triple(&text).expect("round-trip parse");
        assert_eq!(parsed, original);
    }

    #[test]
    fn test_parse_quoted_triple_nested() {
        use serialization::turtle_star::parse_quoted_triple;

        let input = "<< << <http://example.org/a> <http://example.org/p> <http://example.org/b> >> <http://example.org/certainty> \"0.9\"^^<http://www.w3.org/2001/XMLSchema#double> >>";
        let parsed = parse_quoted_triple(input).expect("valid nested quoted triple");
        assert!(matches!(parsed.subject(), Subject::QuotedTriple(_)));
    }

    #[test]
    fn test_parse_quoted_triple_rejects_bad_syntax() {
        use serialization::turtle_star::parse_quoted_triple;

        assert!(parse_quoted_triple("not a quoted triple").is_err());
        assert!(
            parse_quoted_triple("<< <http://example.org/a> <http://example.org/b> >>").is_err()
        );
    }

    /// Regression test: a quoted-triple literal containing control
    /// characters that `print_quoted_str` escapes as `\b`, `\f`, or
    /// `\uXXXX` must round-trip through serialize -> parse, not fail with
    /// "Unsupported escape sequence".
    #[test]
    fn regression_unescape_star_string_supports_full_echar_uchar_grammar() {
        use serialization::turtle_star::{parse_quoted_triple, serialize_quoted_triple};

        // 0x08 (backspace, -> \b), 0x0C (form feed, -> \f), and 0x07 (bell,
        // control char without a short escape, -> ) per
        // `print_quoted_str`.
        let value = "back\u{08}space form\u{0C}feed bell\u{07}end";
        let subject = NamedNode::new("http://example.org/alice").expect("valid IRI");
        let predicate = NamedNode::new("http://example.org/note").expect("valid IRI");
        let original = QuotedTriple::new(Triple::new(
            subject,
            predicate,
            Object::Literal(Literal::new(value)),
        ));

        let text = serialize_quoted_triple(&original);
        assert!(text.contains("\\b"), "expected a \\b escape in: {text}");
        assert!(text.contains("\\f"), "expected a \\f escape in: {text}");
        assert!(
            text.contains("\\u0007"),
            "expected a \\u0007 escape in: {text}"
        );

        let parsed = parse_quoted_triple(&text).expect("must re-parse its own serialized form");
        assert_eq!(parsed, original);
        assert_eq!(parsed.object(), &Object::Literal(Literal::new(value)));
    }

    /// Regression test: `\uXXXX` (4 hex digits) and `\UXXXXXXXX` (8 hex
    /// digits) Unicode escapes are both accepted, including a codepoint
    /// outside the Basic Multilingual Plane that only `\U` can express.
    #[test]
    fn regression_unescape_star_string_handles_u_and_big_u_escapes() {
        use serialization::turtle_star::parse_quoted_triple;

        // A == 'A'; \U0001F600 == the grinning-face emoji.
        let input = "<< <http://example.org/s> <http://example.org/p> \"\\u0041-\\U0001F600\" >>";
        let parsed = parse_quoted_triple(input).expect("valid \\u/\\U escapes must parse");
        assert_eq!(
            parsed.object(),
            &Object::Literal(Literal::new("A-\u{1F600}"))
        );
    }

    /// Regression test: a malformed `\u`/`\U` escape (too few hex digits, or
    /// a non-hex character) must be rejected with an explicit parse error,
    /// never silently accepted or truncated.
    #[test]
    fn regression_unescape_star_string_rejects_truncated_unicode_escape() {
        use serialization::turtle_star::parse_quoted_triple;

        let truncated = "<< <http://example.org/s> <http://example.org/p> \"\\u12\" >>";
        assert!(parse_quoted_triple(truncated).is_err());

        let bad_digit = "<< <http://example.org/s> <http://example.org/p> \"\\u12ZZ\" >>";
        assert!(parse_quoted_triple(bad_digit).is_err());
    }
}