oxirs-ttl 0.3.1

Turtle-family RDF parser and serializer for OxiRS - ported from Oxigraph
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
//! Transformation and execution logic: `MappingEngine`, `MappingRuleBuilder`,
//! CSV/JSON parsers, and related helpers.

use oxirs_core::model::{NamedNode, Subject, Triple};

use super::mapping_types::{
    build_triple_from_pom, DataSource, MappingError, MappingResult, MappingRule, ObjectSpec,
    PredicateObjectMap, Row, Template,
};

// ─── MappingEngine ────────────────────────────────────────────────────────────

/// Engine that executes [`MappingRule`]s and produces RDF [`Triple`]s
///
/// The engine is stateless and cheap to create.  All configuration is
/// carried by the rules themselves.
#[derive(Debug, Default, Clone)]
pub struct MappingEngine {
    /// Whether to skip rows that produce errors instead of failing fast
    pub skip_errors: bool,
}

impl MappingEngine {
    /// Create a new mapping engine with default settings (fail-fast)
    pub fn new() -> Self {
        Self::default()
    }

    /// Create an engine that silently skips rows that produce mapping errors
    pub fn new_lenient() -> Self {
        Self { skip_errors: true }
    }

    /// Execute a single mapping rule and return all produced triples
    pub fn execute(&self, rule: &MappingRule) -> MappingResult<Vec<Triple>> {
        let (headers, rows) = self.extract_rows(&rule.source)?;
        let _ = headers; // headers are embedded inside each Row already
        self.map_rows(rule, &rows)
    }

    /// Execute multiple rules and concatenate all produced triples
    pub fn execute_all(&self, rules: &[MappingRule]) -> MappingResult<Vec<Triple>> {
        let mut all_triples = Vec::new();
        for rule in rules {
            let mut triples = self.execute(rule)?;
            all_triples.append(&mut triples);
        }
        Ok(all_triples)
    }

    // ─── Internal helpers ────────────────────────────────────────────────

    fn extract_rows(&self, source: &DataSource) -> MappingResult<(Vec<String>, Vec<Row>)> {
        match source {
            DataSource::Csv { content, delimiter } => Self::parse_csv(content, *delimiter),
            DataSource::Json { content, json_path } => {
                let rows = Self::parse_json(content, json_path.as_deref())?;
                // headers are implicit in the Row keys; return empty list
                Ok((Vec::new(), rows))
            }
            DataSource::InlineValues { rows, headers } => {
                let parsed_rows: Vec<Row> = rows
                    .iter()
                    .map(|row_values| {
                        let pairs = headers
                            .iter()
                            .zip(row_values.iter())
                            .map(|(h, v)| (h.clone(), v.clone()));
                        Row::from_pairs(pairs)
                    })
                    .collect();
                Ok((headers.clone(), parsed_rows))
            }
        }
    }

    fn map_rows(&self, rule: &MappingRule, rows: &[Row]) -> MappingResult<Vec<Triple>> {
        let mut triples = Vec::with_capacity(rows.len() * rule.predicate_object_maps.len());

        for (row_idx, row) in rows.iter().enumerate() {
            // Generate subject IRI
            let subject_iri = match rule.subject_template.render(row, row_idx) {
                Ok(iri) => iri,
                Err(e) => {
                    if self.skip_errors {
                        continue;
                    }
                    return Err(e);
                }
            };

            let subject_node =
                NamedNode::new(&subject_iri).map_err(|e| MappingError::InvalidIri {
                    template: rule.subject_template.pattern.clone(),
                    iri: format!("{subject_iri} ({e})"),
                })?;
            let subject: Subject = subject_node.into();

            // Generate one triple per predicate-object map
            for pom in &rule.predicate_object_maps {
                let result = build_triple_from_pom(&subject, pom, row, row_idx);
                match result {
                    Ok(triple) => triples.push(triple),
                    Err(e) => {
                        if self.skip_errors {
                            continue;
                        }
                        return Err(e);
                    }
                }
            }
        }
        Ok(triples)
    }

    // ─── CSV parser ──────────────────────────────────────────────────────

    /// Parse CSV content into (headers, rows).
    ///
    /// Handles:
    /// - Configurable delimiter
    /// - Double-quote escaping (`""` inside a quoted field)
    /// - CRLF and LF line endings
    /// - Quoted fields that span multiple lines
    pub fn parse_csv(content: &str, delimiter: char) -> MappingResult<(Vec<String>, Vec<Row>)> {
        let lines = split_csv_lines(content);
        if lines.is_empty() {
            return Ok((Vec::new(), Vec::new()));
        }

        // Parse header row
        let headers = parse_csv_line(&lines[0], delimiter);
        if headers.is_empty() {
            return Err(MappingError::CsvParseError {
                line: 1,
                message: "empty header row".to_string(),
            });
        }

        let mut rows = Vec::with_capacity(lines.len().saturating_sub(1));
        for (line_idx, line) in lines.iter().enumerate().skip(1) {
            if line.trim().is_empty() {
                continue;
            }
            let values = parse_csv_line(line, delimiter);
            if values.len() != headers.len() {
                return Err(MappingError::CsvParseError {
                    line: line_idx + 1,
                    message: format!("expected {} fields but got {}", headers.len(), values.len()),
                });
            }
            let row = Row::from_pairs(headers.iter().cloned().zip(values));
            rows.push(row);
        }
        Ok((headers, rows))
    }

    // ─── JSON parser ─────────────────────────────────────────────────────

    /// Parse JSON content into rows.
    ///
    /// Behaviour:
    /// - If `json_path` is `None`, the root must be a JSON array of objects.
    /// - If `json_path` is `Some("a.b.c")`, the engine traverses object keys
    ///   `a` → `b` → `c` and expects to find an array there.
    /// - Each array element must be a JSON object; its key-value pairs become
    ///   the row fields (values are coerced to strings).
    pub fn parse_json(content: &str, json_path: Option<&str>) -> MappingResult<Vec<Row>> {
        let value: serde_json::Value =
            serde_json::from_str(content).map_err(|e| MappingError::JsonParseError {
                message: e.to_string(),
            })?;

        // Navigate to the target array using dot-separated path
        let array = if let Some(path) = json_path {
            navigate_json_path(&value, path)?
        } else {
            &value
        };

        let arr = array.as_array().ok_or_else(|| {
            let path_desc = json_path.unwrap_or("<root>");
            MappingError::JsonPathNoMatch {
                path: path_desc.to_string(),
            }
        })?;

        let mut rows = Vec::with_capacity(arr.len());
        for element in arr {
            let obj = element
                .as_object()
                .ok_or_else(|| MappingError::JsonParseError {
                    message: "JSON array element is not an object".to_string(),
                })?;
            let row = Row::from_pairs(
                obj.iter()
                    .map(|(k, v)| (k.clone(), json_value_to_string(v))),
            );
            rows.push(row);
        }
        Ok(rows)
    }
}

// ─── JSON helpers ─────────────────────────────────────────────────────────────

fn navigate_json_path<'a>(
    value: &'a serde_json::Value,
    path: &str,
) -> MappingResult<&'a serde_json::Value> {
    let mut current = value;
    for key in path.split('.') {
        current = current
            .get(key)
            .ok_or_else(|| MappingError::JsonPathNoMatch {
                path: path.to_string(),
            })?;
    }
    Ok(current)
}

fn json_value_to_string(v: &serde_json::Value) -> String {
    match v {
        serde_json::Value::String(s) => s.clone(),
        serde_json::Value::Null => String::new(),
        serde_json::Value::Bool(b) => b.to_string(),
        serde_json::Value::Number(n) => n.to_string(),
        other => other.to_string(),
    }
}

// ─── CSV helpers ──────────────────────────────────────────────────────────────

/// Split CSV text into logical lines, handling quoted fields that contain newlines.
fn split_csv_lines(content: &str) -> Vec<String> {
    let mut lines = Vec::new();
    let mut current = String::new();
    let mut in_quotes = false;
    let mut chars = content.chars().peekable();

    while let Some(ch) = chars.next() {
        match ch {
            '"' => {
                in_quotes = !in_quotes;
                current.push(ch);
            }
            '\r' => {
                // Handle CRLF
                if chars.peek() == Some(&'\n') {
                    let _ = chars.next();
                }
                if !in_quotes {
                    lines.push(std::mem::take(&mut current));
                } else {
                    current.push('\n');
                }
            }
            '\n' if !in_quotes => {
                lines.push(std::mem::take(&mut current));
            }
            _ => {
                current.push(ch);
            }
        }
    }
    if !current.is_empty() {
        lines.push(current);
    }
    lines
}

/// Parse a single CSV line into a vector of field values.
fn parse_csv_line(line: &str, delimiter: char) -> Vec<String> {
    let mut fields = Vec::new();
    let mut current = String::new();
    let mut in_quotes = false;
    let mut chars = line.chars().peekable();

    while let Some(ch) = chars.next() {
        if in_quotes {
            if ch == '"' {
                if chars.peek() == Some(&'"') {
                    // Escaped double-quote inside quoted field
                    current.push('"');
                    let _ = chars.next();
                } else {
                    in_quotes = false;
                }
            } else {
                current.push(ch);
            }
        } else if ch == '"' {
            in_quotes = true;
        } else if ch == delimiter {
            fields.push(std::mem::take(&mut current));
        } else {
            current.push(ch);
        }
    }
    fields.push(current);
    fields
}

// ─── Builder ──────────────────────────────────────────────────────────────────

/// Fluent builder for constructing [`MappingRule`] instances
///
/// # Example
///
/// ```rust
/// use oxirs_ttl::mapping::{MappingRuleBuilder, ObjectSpec};
///
/// let rule = MappingRuleBuilder::new("employees")
///     .csv_source("id,name\n1,Alice\n2,Bob")
///     .subject_template("http://example.org/employee/{id}")
///     .map("http://xmlns.com/foaf/0.1/name", ObjectSpec::Column("name".to_string()))
///     .build();
/// ```
#[derive(Debug)]
pub struct MappingRuleBuilder {
    rule: MappingRule,
}

impl MappingRuleBuilder {
    /// Start building a new rule with the given name
    pub fn new(name: impl Into<String>) -> Self {
        let name_str = name.into();
        Self {
            rule: MappingRule {
                name: name_str,
                source: DataSource::Csv {
                    content: String::new(),
                    delimiter: ',',
                },
                subject_template: Template::new(""),
                predicate_object_maps: Vec::new(),
                graph_name: None,
            },
        }
    }

    /// Use a CSV string as the data source (comma delimiter)
    pub fn csv_source(mut self, content: impl Into<String>) -> Self {
        self.rule.source = DataSource::Csv {
            content: content.into(),
            delimiter: ',',
        };
        self
    }

    /// Use a CSV string with a custom delimiter
    pub fn csv_source_with_delimiter(
        mut self,
        content: impl Into<String>,
        delimiter: char,
    ) -> Self {
        self.rule.source = DataSource::Csv {
            content: content.into(),
            delimiter,
        };
        self
    }

    /// Use a JSON string as the data source (root must be an array)
    pub fn json_source(mut self, content: impl Into<String>) -> Self {
        self.rule.source = DataSource::Json {
            content: content.into(),
            json_path: None,
        };
        self
    }

    /// Use a JSON string with a dot-separated path to the target array
    pub fn json_source_with_path(
        mut self,
        content: impl Into<String>,
        json_path: impl Into<String>,
    ) -> Self {
        self.rule.source = DataSource::Json {
            content: content.into(),
            json_path: Some(json_path.into()),
        };
        self
    }

    /// Use pre-parsed inline values
    pub fn inline_source(mut self, headers: Vec<String>, rows: Vec<Vec<String>>) -> Self {
        self.rule.source = DataSource::InlineValues { rows, headers };
        self
    }

    /// Set the subject IRI template
    pub fn subject_template(mut self, template: impl Into<String>) -> Self {
        self.rule.subject_template = Template::new(template);
        self
    }

    /// Add a predicate-object mapping
    pub fn map(mut self, predicate: impl Into<String>, object: ObjectSpec) -> Self {
        self.rule.predicate_object_maps.push(PredicateObjectMap {
            predicate: predicate.into(),
            object_template: object,
        });
        self
    }

    /// Assign all produced triples to a named graph
    pub fn graph(mut self, graph_name: impl Into<String>) -> Self {
        self.rule.graph_name = Some(graph_name.into());
        self
    }

    /// Consume the builder and return the finished [`MappingRule`]
    pub fn build(self) -> MappingRule {
        self.rule
    }
}