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
//! # SPARQL Results CSV/TSV Serializer
//!
//! Implements the W3C SPARQL Query Results CSV and TSV Formats specification
//! for serializing SPARQL SELECT query results.
//!
//! ## Features
//!
//! - **CSV format**: RFC 4180-compliant CSV output with proper quoting
//! - **TSV format**: Tab-separated output per W3C spec
//! - **Header row**: Variable names as the first row
//! - **Type handling**: Proper serialization of IRIs, literals, blank nodes
//! - **Streaming**: Can serialize row-by-row for large result sets
//!
//! ## Reference
//!
//! - [W3C SPARQL 1.1 Query Results CSV and TSV Formats](https://www.w3.org/TR/sparql11-results-csv-tsv/)

use serde::{Deserialize, Serialize};

// ─────────────────────────────────────────────
// Result types
// ─────────────────────────────────────────────

/// An RDF term in a SPARQL result binding.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum RdfTerm {
    /// An IRI reference.
    Iri(String),
    /// A plain literal with optional language tag.
    Literal {
        value: String,
        language: Option<String>,
        datatype: Option<String>,
    },
    /// A blank node.
    BlankNode(String),
    /// Unbound (no value for this variable in this row).
    Unbound,
}

impl RdfTerm {
    pub fn iri(s: impl Into<String>) -> Self {
        RdfTerm::Iri(s.into())
    }

    pub fn literal(value: impl Into<String>) -> Self {
        RdfTerm::Literal {
            value: value.into(),
            language: None,
            datatype: None,
        }
    }

    pub fn lang_literal(value: impl Into<String>, lang: impl Into<String>) -> Self {
        RdfTerm::Literal {
            value: value.into(),
            language: Some(lang.into()),
            datatype: None,
        }
    }

    pub fn typed_literal(value: impl Into<String>, datatype: impl Into<String>) -> Self {
        RdfTerm::Literal {
            value: value.into(),
            language: None,
            datatype: Some(datatype.into()),
        }
    }

    pub fn blank_node(id: impl Into<String>) -> Self {
        RdfTerm::BlankNode(id.into())
    }
}

/// A single row of SPARQL result bindings.
#[derive(Debug, Clone)]
pub struct ResultRow {
    /// Values for each variable (same order as variables).
    pub values: Vec<RdfTerm>,
}

impl ResultRow {
    pub fn new(values: Vec<RdfTerm>) -> Self {
        Self { values }
    }
}

/// A complete SPARQL SELECT result set.
#[derive(Debug, Clone)]
pub struct SparqlResultSet {
    /// Variable names (without '?').
    pub variables: Vec<String>,
    /// Result rows.
    pub rows: Vec<ResultRow>,
}

impl SparqlResultSet {
    pub fn new(variables: Vec<String>) -> Self {
        Self {
            variables,
            rows: Vec::new(),
        }
    }

    pub fn add_row(&mut self, row: ResultRow) {
        self.rows.push(row);
    }

    /// Number of variables.
    pub fn width(&self) -> usize {
        self.variables.len()
    }

    /// Number of rows.
    pub fn height(&self) -> usize {
        self.rows.len()
    }
}

// ─────────────────────────────────────────────
// CSV/TSV serialization format
// ─────────────────────────────────────────────

/// Output format.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ResultFormat {
    Csv,
    Tsv,
}

/// Configuration for the serializer.
#[derive(Debug, Clone)]
pub struct SerializerConfig {
    /// Output format.
    pub format: ResultFormat,
    /// Whether to include the header row (default: true).
    pub include_header: bool,
    /// For TSV: whether to prefix IRIs with '<' and '>' (default: true).
    pub tsv_bracket_iris: bool,
}

impl Default for SerializerConfig {
    fn default() -> Self {
        Self {
            format: ResultFormat::Csv,
            include_header: true,
            tsv_bracket_iris: true,
        }
    }
}

// ─────────────────────────────────────────────
// Serializers
// ─────────────────────────────────────────────

/// Serialize a result set to CSV format (W3C SPARQL Results CSV Format).
pub fn serialize_csv(result_set: &SparqlResultSet) -> String {
    let config = SerializerConfig {
        format: ResultFormat::Csv,
        ..Default::default()
    };
    serialize(result_set, &config)
}

/// Serialize a result set to TSV format (W3C SPARQL Results TSV Format).
pub fn serialize_tsv(result_set: &SparqlResultSet) -> String {
    let config = SerializerConfig {
        format: ResultFormat::Tsv,
        ..Default::default()
    };
    serialize(result_set, &config)
}

/// Serialize a result set with custom configuration.
pub fn serialize(result_set: &SparqlResultSet, config: &SerializerConfig) -> String {
    let separator = match config.format {
        ResultFormat::Csv => ',',
        ResultFormat::Tsv => '\t',
    };
    let mut output = String::new();

    // Header row
    if config.include_header {
        let header: Vec<String> = result_set
            .variables
            .iter()
            .map(|v| match config.format {
                ResultFormat::Csv => csv_escape(v),
                ResultFormat::Tsv => format!("?{v}"),
            })
            .collect();
        output.push_str(&header.join(&separator.to_string()));
        output.push_str("\r\n");
    }

    // Data rows
    for row in &result_set.rows {
        let values: Vec<String> = row
            .values
            .iter()
            .map(|term| format_term(term, config))
            .collect();
        output.push_str(&values.join(&separator.to_string()));
        output.push_str("\r\n");
    }

    output
}

/// Serialize a single row (for streaming).
pub fn serialize_row(row: &ResultRow, config: &SerializerConfig) -> String {
    let separator = match config.format {
        ResultFormat::Csv => ',',
        ResultFormat::Tsv => '\t',
    };
    let values: Vec<String> = row
        .values
        .iter()
        .map(|term| format_term(term, config))
        .collect();
    let mut output = values.join(&separator.to_string());
    output.push_str("\r\n");
    output
}

/// Serialize the header row only.
pub fn serialize_header(variables: &[String], config: &SerializerConfig) -> String {
    let separator = match config.format {
        ResultFormat::Csv => ',',
        ResultFormat::Tsv => '\t',
    };
    let header: Vec<String> = variables
        .iter()
        .map(|v| match config.format {
            ResultFormat::Csv => csv_escape(v),
            ResultFormat::Tsv => format!("?{v}"),
        })
        .collect();
    let mut output = header.join(&separator.to_string());
    output.push_str("\r\n");
    output
}

// ─── Internal formatting ─────────────────────────────────

fn format_term(term: &RdfTerm, config: &SerializerConfig) -> String {
    match config.format {
        ResultFormat::Csv => format_term_csv(term),
        ResultFormat::Tsv => format_term_tsv(term, config.tsv_bracket_iris),
    }
}

/// CSV format: IRIs as bare strings, literals as values, blank nodes as _:id
fn format_term_csv(term: &RdfTerm) -> String {
    match term {
        RdfTerm::Iri(iri) => csv_escape(iri),
        RdfTerm::Literal {
            value,
            language,
            datatype,
        } => {
            if language.is_some() || datatype.is_some() {
                // CSV just uses the lexical form
                csv_escape(value)
            } else {
                csv_escape(value)
            }
        }
        RdfTerm::BlankNode(id) => csv_escape(&format!("_:{id}")),
        RdfTerm::Unbound => String::new(),
    }
}

/// TSV format: IRIs in angle brackets, literals with type annotations
fn format_term_tsv(term: &RdfTerm, bracket_iris: bool) -> String {
    match term {
        RdfTerm::Iri(iri) => {
            if bracket_iris {
                format!("<{iri}>")
            } else {
                iri.clone()
            }
        }
        RdfTerm::Literal {
            value,
            language: Some(lang),
            ..
        } => {
            format!("\"{}\"@{}", tsv_escape_string(value), lang)
        }
        RdfTerm::Literal {
            value,
            datatype: Some(dt),
            ..
        } => {
            format!("\"{}\"^^<{}>", tsv_escape_string(value), dt)
        }
        RdfTerm::Literal { value, .. } => {
            format!("\"{}\"", tsv_escape_string(value))
        }
        RdfTerm::BlankNode(id) => format!("_:{id}"),
        RdfTerm::Unbound => String::new(),
    }
}

/// CSV escaping: quote fields that contain comma, quote, or newline.
fn csv_escape(value: &str) -> String {
    if value.contains(',') || value.contains('"') || value.contains('\n') || value.contains('\r') {
        let escaped = value.replace('"', "\"\"");
        format!("\"{escaped}\"")
    } else {
        value.to_string()
    }
}

/// TSV string escaping: escape tabs, newlines, quotes, backslashes.
fn tsv_escape_string(value: &str) -> String {
    let mut result = String::with_capacity(value.len());
    for ch in value.chars() {
        match ch {
            '\t' => result.push_str("\\t"),
            '\n' => result.push_str("\\n"),
            '\r' => result.push_str("\\r"),
            '\\' => result.push_str("\\\\"),
            '"' => result.push_str("\\\""),
            _ => result.push(ch),
        }
    }
    result
}

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

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

    fn sample_result_set() -> SparqlResultSet {
        let mut rs = SparqlResultSet::new(vec!["name".to_string(), "age".to_string()]);
        rs.add_row(ResultRow::new(vec![
            RdfTerm::literal("Alice"),
            RdfTerm::typed_literal("30", "http://www.w3.org/2001/XMLSchema#integer"),
        ]));
        rs.add_row(ResultRow::new(vec![
            RdfTerm::literal("Bob"),
            RdfTerm::typed_literal("25", "http://www.w3.org/2001/XMLSchema#integer"),
        ]));
        rs
    }

    fn iri_result_set() -> SparqlResultSet {
        let mut rs = SparqlResultSet::new(vec!["s".to_string(), "p".to_string(), "o".to_string()]);
        rs.add_row(ResultRow::new(vec![
            RdfTerm::iri("http://example.org/alice"),
            RdfTerm::iri("http://xmlns.com/foaf/0.1/name"),
            RdfTerm::literal("Alice"),
        ]));
        rs.add_row(ResultRow::new(vec![
            RdfTerm::iri("http://example.org/alice"),
            RdfTerm::iri("http://xmlns.com/foaf/0.1/knows"),
            RdfTerm::iri("http://example.org/bob"),
        ]));
        rs
    }

    // ═══ RdfTerm tests ═══════════════════════════════════

    #[test]
    fn test_rdf_term_iri() {
        let term = RdfTerm::iri("http://example.org");
        assert_eq!(term, RdfTerm::Iri("http://example.org".to_string()));
    }

    #[test]
    fn test_rdf_term_literal() {
        let term = RdfTerm::literal("hello");
        assert!(matches!(term, RdfTerm::Literal { value, .. } if value == "hello"));
    }

    #[test]
    fn test_rdf_term_lang_literal() {
        let term = RdfTerm::lang_literal("hello", "en");
        assert!(matches!(
            term,
            RdfTerm::Literal { language: Some(l), .. } if l == "en"
        ));
    }

    #[test]
    fn test_rdf_term_typed_literal() {
        let term = RdfTerm::typed_literal("42", "http://www.w3.org/2001/XMLSchema#integer");
        assert!(matches!(
            term,
            RdfTerm::Literal { datatype: Some(dt), .. } if dt.contains("integer")
        ));
    }

    #[test]
    fn test_rdf_term_blank_node() {
        let term = RdfTerm::blank_node("b0");
        assert_eq!(term, RdfTerm::BlankNode("b0".to_string()));
    }

    // ═══ Result set tests ════════════════════════════════

    #[test]
    fn test_result_set_dimensions() {
        let rs = sample_result_set();
        assert_eq!(rs.width(), 2);
        assert_eq!(rs.height(), 2);
    }

    #[test]
    fn test_result_set_empty() {
        let rs = SparqlResultSet::new(vec!["x".to_string()]);
        assert_eq!(rs.width(), 1);
        assert_eq!(rs.height(), 0);
    }

    // ═══ CSV serialization tests ═════════════════════════

    #[test]
    fn test_csv_basic() {
        let rs = sample_result_set();
        let csv = serialize_csv(&rs);
        assert!(csv.starts_with("name,age\r\n"));
        assert!(csv.contains("Alice"));
        assert!(csv.contains("30"));
    }

    #[test]
    fn test_csv_header() {
        let rs = sample_result_set();
        let csv = serialize_csv(&rs);
        let first_line = csv.lines().next().expect("first line");
        assert_eq!(first_line, "name,age");
    }

    #[test]
    fn test_csv_iri_values() {
        let rs = iri_result_set();
        let csv = serialize_csv(&rs);
        assert!(csv.contains("http://example.org/alice"));
    }

    #[test]
    fn test_csv_escaping_comma() {
        let mut rs = SparqlResultSet::new(vec!["val".to_string()]);
        rs.add_row(ResultRow::new(vec![RdfTerm::literal("hello, world")]));
        let csv = serialize_csv(&rs);
        assert!(csv.contains("\"hello, world\""));
    }

    #[test]
    fn test_csv_escaping_quote() {
        let mut rs = SparqlResultSet::new(vec!["val".to_string()]);
        rs.add_row(ResultRow::new(vec![RdfTerm::literal("say \"hi\"")]));
        let csv = serialize_csv(&rs);
        assert!(csv.contains("\"say \"\"hi\"\"\""));
    }

    #[test]
    fn test_csv_unbound() {
        let mut rs = SparqlResultSet::new(vec!["a".to_string(), "b".to_string()]);
        rs.add_row(ResultRow::new(vec![
            RdfTerm::literal("x"),
            RdfTerm::Unbound,
        ]));
        let csv = serialize_csv(&rs);
        // Unbound should produce empty field
        assert!(csv.contains("x,\r\n"));
    }

    #[test]
    fn test_csv_blank_node() {
        let mut rs = SparqlResultSet::new(vec!["node".to_string()]);
        rs.add_row(ResultRow::new(vec![RdfTerm::blank_node("b0")]));
        let csv = serialize_csv(&rs);
        assert!(csv.contains("_:b0"));
    }

    #[test]
    fn test_csv_crlf_line_endings() {
        let rs = sample_result_set();
        let csv = serialize_csv(&rs);
        assert!(csv.contains("\r\n"));
    }

    #[test]
    fn test_csv_no_header() {
        let rs = sample_result_set();
        let config = SerializerConfig {
            format: ResultFormat::Csv,
            include_header: false,
            ..Default::default()
        };
        let csv = serialize(&rs, &config);
        // First line should be data, not header
        let first_line = csv.lines().next().expect("first line");
        assert!(!first_line.contains("name"));
        assert!(first_line.contains("Alice"));
    }

    // ═══ TSV serialization tests ═════════════════════════

    #[test]
    fn test_tsv_basic() {
        let rs = sample_result_set();
        let tsv = serialize_tsv(&rs);
        assert!(tsv.starts_with("?name\t?age\r\n"));
    }

    #[test]
    fn test_tsv_header_prefixed() {
        let rs = sample_result_set();
        let tsv = serialize_tsv(&rs);
        let first_line = tsv.lines().next().expect("first line");
        assert!(first_line.starts_with("?name"));
        assert!(first_line.contains("\t?age"));
    }

    #[test]
    fn test_tsv_iri_bracketed() {
        let rs = iri_result_set();
        let tsv = serialize_tsv(&rs);
        assert!(tsv.contains("<http://example.org/alice>"));
    }

    #[test]
    fn test_tsv_literal_quoted() {
        let rs = sample_result_set();
        let tsv = serialize_tsv(&rs);
        assert!(tsv.contains("\"Alice\""));
    }

    #[test]
    fn test_tsv_typed_literal() {
        let rs = sample_result_set();
        let tsv = serialize_tsv(&rs);
        assert!(tsv.contains("^^<http://www.w3.org/2001/XMLSchema#integer>"));
    }

    #[test]
    fn test_tsv_lang_literal() {
        let mut rs = SparqlResultSet::new(vec!["label".to_string()]);
        rs.add_row(ResultRow::new(vec![RdfTerm::lang_literal("chat", "fr")]));
        let tsv = serialize_tsv(&rs);
        assert!(tsv.contains("\"chat\"@fr"));
    }

    #[test]
    fn test_tsv_blank_node() {
        let mut rs = SparqlResultSet::new(vec!["node".to_string()]);
        rs.add_row(ResultRow::new(vec![RdfTerm::blank_node("b1")]));
        let tsv = serialize_tsv(&rs);
        assert!(tsv.contains("_:b1"));
    }

    #[test]
    fn test_tsv_escaping_tab() {
        let mut rs = SparqlResultSet::new(vec!["val".to_string()]);
        rs.add_row(ResultRow::new(vec![RdfTerm::literal("a\tb")]));
        let tsv = serialize_tsv(&rs);
        assert!(tsv.contains("a\\tb"));
    }

    #[test]
    fn test_tsv_escaping_newline() {
        let mut rs = SparqlResultSet::new(vec!["val".to_string()]);
        rs.add_row(ResultRow::new(vec![RdfTerm::literal("line1\nline2")]));
        let tsv = serialize_tsv(&rs);
        assert!(tsv.contains("line1\\nline2"));
    }

    // ═══ Streaming serialization tests ═══════════════════

    #[test]
    fn test_serialize_header_csv() {
        let vars = vec!["name".to_string(), "age".to_string()];
        let config = SerializerConfig {
            format: ResultFormat::Csv,
            ..Default::default()
        };
        let header = serialize_header(&vars, &config);
        assert_eq!(header, "name,age\r\n");
    }

    #[test]
    fn test_serialize_header_tsv() {
        let vars = vec!["name".to_string(), "age".to_string()];
        let config = SerializerConfig {
            format: ResultFormat::Tsv,
            ..Default::default()
        };
        let header = serialize_header(&vars, &config);
        assert_eq!(header, "?name\t?age\r\n");
    }

    #[test]
    fn test_serialize_row_csv() {
        let row = ResultRow::new(vec![RdfTerm::literal("Alice"), RdfTerm::literal("30")]);
        let config = SerializerConfig {
            format: ResultFormat::Csv,
            ..Default::default()
        };
        let output = serialize_row(&row, &config);
        assert_eq!(output, "Alice,30\r\n");
    }

    #[test]
    fn test_serialize_row_tsv() {
        let row = ResultRow::new(vec![
            RdfTerm::iri("http://example.org/a"),
            RdfTerm::literal("hello"),
        ]);
        let config = SerializerConfig {
            format: ResultFormat::Tsv,
            ..Default::default()
        };
        let output = serialize_row(&row, &config);
        assert!(output.contains("<http://example.org/a>"));
        assert!(output.contains("\t"));
    }

    // ═══ Empty result set tests ══════════════════════════

    #[test]
    fn test_csv_empty_results() {
        let rs = SparqlResultSet::new(vec!["x".to_string(), "y".to_string()]);
        let csv = serialize_csv(&rs);
        assert_eq!(csv, "x,y\r\n");
    }

    #[test]
    fn test_tsv_empty_results() {
        let rs = SparqlResultSet::new(vec!["x".to_string()]);
        let tsv = serialize_tsv(&rs);
        assert_eq!(tsv, "?x\r\n");
    }

    // ═══ Config tests ════════════════════════════════════

    #[test]
    fn test_default_config() {
        let config = SerializerConfig::default();
        assert_eq!(config.format, ResultFormat::Csv);
        assert!(config.include_header);
        assert!(config.tsv_bracket_iris);
    }

    #[test]
    fn test_tsv_no_bracket_iris() {
        let rs = iri_result_set();
        let config = SerializerConfig {
            format: ResultFormat::Tsv,
            tsv_bracket_iris: false,
            include_header: true,
        };
        let tsv = serialize(&rs, &config);
        assert!(!tsv.contains("<http://"));
        assert!(tsv.contains("http://example.org/alice"));
    }
}