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
//! TriG format serializer and parser
//!
//! TriG extends Turtle with support for named graphs, allowing multiple RDF graphs
//! to be serialized in a single document with graph-level organization.
//!
//! W3C Specification: <https://www.w3.org/TR/trig/>

use super::error::FormatError;
use crate::model::{
    GraphName, Literal, NamedNode, ObjectRef, PredicateRef, Quad, QuadRef, SubjectRef,
};
use std::collections::{BTreeMap, HashMap};
use std::io::Write;

/// TriG serializer for writing RDF quads with named graph support
#[derive(Debug, Clone)]
pub struct TriGSerializer {
    /// Base IRI for relative IRI resolution
    base_iri: Option<String>,
    /// Prefix declarations for compact serialization
    prefixes: HashMap<String, String>,
    /// Pretty printing with indentation
    pretty: bool,
}

impl TriGSerializer {
    /// Create a new TriG serializer
    pub fn new() -> Self {
        let mut prefixes = HashMap::new();

        // Add common prefixes
        prefixes.insert(
            "rdf".to_string(),
            "http://www.w3.org/1999/02/22-rdf-syntax-ns#".to_string(),
        );
        prefixes.insert(
            "rdfs".to_string(),
            "http://www.w3.org/2000/01/rdf-schema#".to_string(),
        );
        prefixes.insert(
            "xsd".to_string(),
            "http://www.w3.org/2001/XMLSchema#".to_string(),
        );

        Self {
            base_iri: None,
            prefixes,
            pretty: false,
        }
    }

    /// Set the base IRI
    pub fn with_base_iri(mut self, base: &str) -> Self {
        self.base_iri = Some(base.to_string());
        self
    }

    /// Add a prefix mapping
    pub fn with_prefix(mut self, prefix: &str, iri: &str) -> Self {
        self.prefixes.insert(prefix.to_string(), iri.to_string());
        self
    }

    /// Enable pretty printing
    pub fn pretty(mut self) -> Self {
        self.pretty = true;
        self
    }

    /// Wrap this serializer for a specific writer
    pub fn for_writer<W: Write + 'static>(self, writer: W) -> TriGWriter<W> {
        TriGWriter {
            writer,
            serializer: self,
            buffer: Vec::new(),
        }
    }

    /// Serialize quads grouped by graph
    fn serialize_quads<W: Write>(&self, quads: &[Quad], writer: &mut W) -> Result<(), FormatError> {
        // Write prefix declarations
        for (prefix, namespace) in &self.prefixes {
            writeln!(writer, "@prefix {}: <{}> .", prefix, namespace).map_err(FormatError::from)?;
        }

        if !self.prefixes.is_empty() {
            writeln!(writer).map_err(FormatError::from)?;
        }

        // Group quads by graph
        let grouped = self.group_quads_by_graph(quads);

        for (graph_name, graph_quads) in grouped {
            match graph_name {
                GraphName::DefaultGraph => {
                    // Serialize default graph triples directly
                    for quad in graph_quads {
                        self.serialize_triple(quad.as_ref(), writer)?;
                        writeln!(writer, " .").map_err(FormatError::from)?;
                    }
                }
                GraphName::NamedNode(node) => {
                    // Named graph
                    self.write_named_node(&node, writer)?;
                    writeln!(writer, " {{").map_err(FormatError::from)?;

                    for quad in graph_quads {
                        if self.pretty {
                            write!(writer, "    ").map_err(FormatError::from)?;
                        }
                        self.serialize_triple(quad.as_ref(), writer)?;
                        writeln!(writer, " .").map_err(FormatError::from)?;
                    }

                    writeln!(writer, "}}").map_err(FormatError::from)?;
                }
                GraphName::BlankNode(node) => {
                    // Blank node graph
                    let id = node.as_str();
                    let id = id.strip_prefix("_:").unwrap_or(id);
                    writeln!(writer, "_:{} {{", id).map_err(FormatError::from)?;

                    for quad in graph_quads {
                        if self.pretty {
                            write!(writer, "    ").map_err(FormatError::from)?;
                        }
                        self.serialize_triple(quad.as_ref(), writer)?;
                        writeln!(writer, " .").map_err(FormatError::from)?;
                    }

                    writeln!(writer, "}}").map_err(FormatError::from)?;
                }
                GraphName::Variable(_) => {
                    return Err(FormatError::InvalidData(
                        "Cannot serialize variable graph names".to_string(),
                    ));
                }
            }

            if self.pretty {
                writeln!(writer).map_err(FormatError::from)?;
            }
        }

        Ok(())
    }

    fn serialize_triple<W: Write>(
        &self,
        quad: QuadRef<'_>,
        writer: &mut W,
    ) -> Result<(), FormatError> {
        self.write_subject(quad.subject(), writer)?;
        write!(writer, " ").map_err(FormatError::from)?;

        self.write_predicate(quad.predicate(), writer)?;
        write!(writer, " ").map_err(FormatError::from)?;

        self.write_object(quad.object(), writer)?;

        Ok(())
    }

    fn write_subject<W: Write>(
        &self,
        subject: SubjectRef<'_>,
        writer: &mut W,
    ) -> Result<(), FormatError> {
        match subject {
            SubjectRef::NamedNode(node) => self.write_named_node(node, writer)?,
            SubjectRef::BlankNode(node) => {
                let id = node.as_str();
                let id = id.strip_prefix("_:").unwrap_or(id);
                write!(writer, "_:{}", id).map_err(FormatError::from)?;
            }
            SubjectRef::Variable(var) => {
                write!(writer, "?{}", var.name()).map_err(FormatError::from)?;
            }
            SubjectRef::QuotedTriple(qt) => self.write_quoted_triple(qt.inner(), writer)?,
        }
        Ok(())
    }

    fn write_quoted_triple<W: Write>(
        &self,
        inner: &crate::model::Triple,
        writer: &mut W,
    ) -> Result<(), FormatError> {
        write!(writer, "<< ").map_err(FormatError::from)?;
        self.write_subject(inner.subject().into(), writer)?;
        write!(writer, " ").map_err(FormatError::from)?;
        self.write_predicate(inner.predicate().into(), writer)?;
        write!(writer, " ").map_err(FormatError::from)?;
        self.write_object(inner.object().into(), writer)?;
        write!(writer, " >>").map_err(FormatError::from)?;
        Ok(())
    }

    fn write_predicate<W: Write>(
        &self,
        predicate: PredicateRef<'_>,
        writer: &mut W,
    ) -> Result<(), FormatError> {
        match predicate {
            PredicateRef::NamedNode(node) => {
                // Check for rdf:type abbreviation
                if node.as_str() == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" {
                    write!(writer, "a").map_err(FormatError::from)?;
                } else {
                    self.write_named_node(node, writer)?;
                }
            }
            PredicateRef::Variable(var) => {
                write!(writer, "?{}", var.name()).map_err(FormatError::from)?;
            }
        }
        Ok(())
    }

    fn write_object<W: Write>(
        &self,
        object: ObjectRef<'_>,
        writer: &mut W,
    ) -> Result<(), FormatError> {
        match object {
            ObjectRef::NamedNode(node) => self.write_named_node(node, writer)?,
            ObjectRef::BlankNode(node) => {
                let id = node.as_str();
                let id = id.strip_prefix("_:").unwrap_or(id);
                write!(writer, "_:{}", id).map_err(FormatError::from)?;
            }
            ObjectRef::Literal(literal) => self.write_literal(literal, writer)?,
            ObjectRef::Variable(var) => {
                write!(writer, "?{}", var.name()).map_err(FormatError::from)?;
            }
            ObjectRef::QuotedTriple(qt) => self.write_quoted_triple(qt.inner(), writer)?,
        }
        Ok(())
    }

    fn write_named_node<W: Write>(
        &self,
        node: &NamedNode,
        writer: &mut W,
    ) -> Result<(), FormatError> {
        let iri = node.as_str();

        // Try to use a prefix
        for (prefix, namespace) in &self.prefixes {
            if let Some(local) = iri.strip_prefix(namespace) {
                write!(writer, "{}:{}", prefix, local).map_err(FormatError::from)?;
                return Ok(());
            }
        }

        // Use full IRI
        write!(writer, "<{}>", iri).map_err(FormatError::from)?;
        Ok(())
    }

    fn write_literal<W: Write>(
        &self,
        literal: &Literal,
        writer: &mut W,
    ) -> Result<(), FormatError> {
        let value = literal.value();
        let escaped = self.escape_string(value);

        write!(writer, "\"{}\"", escaped).map_err(FormatError::from)?;

        if let Some(lang) = literal.language() {
            write!(writer, "@{}", lang).map_err(FormatError::from)?;
        } else {
            let datatype = literal.datatype();
            if datatype.as_str() != "http://www.w3.org/2001/XMLSchema#string" {
                write!(writer, "^^").map_err(FormatError::from)?;
                self.write_named_node(&datatype.into_owned(), writer)?;
            }
        }

        Ok(())
    }

    fn escape_string(&self, s: &str) -> String {
        let mut result = String::with_capacity(s.len());
        for ch in s.chars() {
            match ch {
                '\\' => result.push_str("\\\\"),
                '\"' => result.push_str("\\\""),
                '\n' => result.push_str("\\n"),
                '\r' => result.push_str("\\r"),
                '\t' => result.push_str("\\t"),
                c if c.is_control() => {
                    result.push_str(&format!("\\u{:04X}", c as u32));
                }
                c => result.push(c),
            }
        }
        result
    }

    fn group_quads_by_graph<'a>(&self, quads: &'a [Quad]) -> BTreeMap<GraphName, Vec<&'a Quad>> {
        let mut grouped = BTreeMap::new();

        for quad in quads {
            grouped
                .entry(quad.graph_name().clone())
                .or_insert_with(Vec::new)
                .push(quad);
        }

        grouped
    }
}

impl Default for TriGSerializer {
    fn default() -> Self {
        Self::new()
    }
}

/// Writer wrapper for TriG serialization
pub struct TriGWriter<W: Write> {
    writer: W,
    serializer: TriGSerializer,
    buffer: Vec<Quad>,
}

impl<W: Write> TriGWriter<W> {
    /// Serialize a single quad (buffered until finish)
    pub fn serialize_quad(&mut self, quad: QuadRef<'_>) -> Result<(), FormatError> {
        self.buffer.push(quad.into());
        Ok(())
    }

    /// Finish serialization and return the writer
    pub fn finish(mut self) -> Result<W, FormatError> {
        self.serializer
            .serialize_quads(&self.buffer, &mut self.writer)?;
        Ok(self.writer)
    }
}

/// Implement the QuadSerializer trait for integration with the format system
impl<W: Write> super::serializer::QuadSerializer<W> for TriGWriter<W> {
    fn serialize_quad(&mut self, quad: QuadRef<'_>) -> super::serializer::QuadSerializeResult {
        TriGWriter::serialize_quad(self, quad)
            .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))
    }

    fn finish(self: Box<Self>) -> super::error::SerializeResult<W> {
        TriGWriter::finish(*self).map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))
    }
}

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

    #[test]
    fn test_trig_serialize_default_graph() {
        let serializer = TriGSerializer::new();
        let mut writer = Vec::new();

        let triple = Triple::new(
            Subject::NamedNode(NamedNode::new("http://example.org/subject").expect("valid IRI")),
            NamedNode::new("http://example.org/predicate").expect("valid IRI"),
            Object::NamedNode(NamedNode::new("http://example.org/object").expect("valid IRI")),
        );

        let quads = vec![Quad::from(triple)];
        serializer
            .serialize_quads(&quads, &mut writer)
            .expect("operation should succeed");

        let output = String::from_utf8(writer).expect("bytes should be valid UTF-8");
        assert!(output.contains("<http://example.org/subject>"));
        assert!(output.contains("<http://example.org/predicate>"));
        assert!(output.contains("<http://example.org/object>"));
    }

    #[test]
    fn test_trig_serialize_named_graph() {
        let serializer = TriGSerializer::new();
        let mut writer = Vec::new();

        let quad = Quad::new(
            Subject::NamedNode(NamedNode::new("http://example.org/subject").expect("valid IRI")),
            NamedNode::new("http://example.org/predicate").expect("valid IRI"),
            Object::NamedNode(NamedNode::new("http://example.org/object").expect("valid IRI")),
            GraphName::NamedNode(NamedNode::new("http://example.org/graph").expect("valid IRI")),
        );

        let quads = vec![quad];
        serializer
            .serialize_quads(&quads, &mut writer)
            .expect("operation should succeed");

        let output = String::from_utf8(writer).expect("bytes should be valid UTF-8");
        assert!(output.contains("<http://example.org/graph>"));
        assert!(output.contains("{"));
        assert!(output.contains("}"));
    }
}