nexus-standard 1.1.0

Zero-copy compiler and reader for the Nexus Standard (NXS) bi-modal serialization format
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
//! XML → .nxb. Streaming two-pass import using `quick-xml`.
//!
//! A record is the element named by `--xml-record-tag`. Attributes become
//! fields per `--xml-attrs` (as-fields = `id`, prefix = `@id`).
//!
//! Security invariants:
//! - DOCTYPE / internal entities → `ConvertEntityExpansion` (exit 3)
//! - Nesting depth > `--xml-max-depth` → `ConvertDepthExceeded` (exit 3)
//! - All text/attr decoding goes through `Reader::decoder()` so UTF-16 works
//!
//! Note: `quick-xml 0.36` does not expose a public API to disable custom
//! entities on the reader; instead, we scan for `<!DOCTYPE` / `<!ENTITY`
//! tokens in the event stream and reject them immediately.

use super::{ImportArgs, ImportReport, InferredSchema, XmlAttrsMode};
use crate::convert::infer;
use crate::error::{NxsError, Result};
use crate::writer::{NxsWriter, Schema, Slot};
use quick_xml::{events::Event, Reader};
use std::io::{BufRead, Read, Write};

/// Guard: reject any DocType/entity declaration by scanning early in the byte stream.
fn check_for_entity_expansion(src: &[u8]) -> Result<()> {
    // quick-xml exposes DOCTYPE as an Event::DocType. We also scan raw bytes
    // as a belt-and-suspenders check since some parsers skip malformed events.
    if let Some(pos) = find_bytes_ci(src, b"<!DOCTYPE") {
        // Allow DOCTYPE that doesn't contain ENTITY declarations.
        let slice_after = src.get(pos..).unwrap_or(&[]);
        if find_bytes_ci(slice_after, b"<!ENTITY").is_some()
            || find_bytes_ci(slice_after, b"ENTITY ").is_some()
        {
            return Err(NxsError::ConvertEntityExpansion);
        }
    }
    Ok(())
}

fn find_bytes_ci(haystack: &[u8], needle: &[u8]) -> Option<usize> {
    if needle.is_empty() || haystack.len() < needle.len() {
        return None;
    }
    let upper_needle: Vec<u8> = needle.iter().map(|b| b.to_ascii_uppercase()).collect();
    'outer: for i in 0..=(haystack.len() - needle.len()) {
        for j in 0..needle.len() {
            let h = haystack
                .get(i + j)
                .copied()
                .unwrap_or(0)
                .to_ascii_uppercase();
            if let Some(&n) = upper_needle.get(j) {
                if h != n {
                    continue 'outer;
                }
            }
        }
        return Some(i);
    }
    None
}

/// Parse all records from a `BufRead` source and call `on_record` with each.
/// `on_record(fields)` is called once per matching `record_tag` element.
fn parse_records<B: BufRead>(
    mut reader: Reader<B>,
    args: &ImportArgs,
    record_tag: &str,
    mut on_record: impl FnMut(Vec<(String, String)>) -> Result<()>,
) -> Result<()> {
    let depth_limit = args.xml_max_depth.min(args.max_depth);
    let attrs_mode = args.xml_attrs;
    let mut buf = Vec::new();
    let mut depth: usize = 0;
    let mut in_record = false;
    let mut record_depth: usize = 0;
    let mut current_fields: Vec<(String, String)> = Vec::new();
    let mut current_path: Vec<String> = Vec::new(); // element stack within a record
    let mut current_text_key: Option<String> = None;

    loop {
        match reader.read_event_into(&mut buf) {
            Ok(Event::Eof) => break,
            Ok(Event::DocType(_)) => {
                return Err(NxsError::ConvertEntityExpansion);
            }
            Ok(Event::Start(e)) => {
                depth += 1;
                if depth > depth_limit {
                    return Err(NxsError::ConvertDepthExceeded);
                }
                let local_name = e.local_name();
                let tag = reader
                    .decoder()
                    .decode(local_name.as_ref())
                    .map_err(|e| NxsError::ConvertParseError {
                        offset: 0,
                        msg: e.to_string(),
                    })?
                    .to_string();

                if !in_record && tag == record_tag {
                    in_record = true;
                    record_depth = depth;
                    current_fields.clear();
                    current_path.clear();

                    // Attributes on the record element itself
                    collect_attrs(&e, &reader, &mut current_fields, attrs_mode)?;
                } else if in_record {
                    current_path.push(tag.clone());
                    // Attributes on child elements
                    collect_attrs(&e, &reader, &mut current_fields, attrs_mode)?;
                    current_text_key = Some(path_key(&current_path));
                }
            }
            Ok(Event::Empty(e)) => {
                let local_name = e.local_name();
                let tag = reader
                    .decoder()
                    .decode(local_name.as_ref())
                    .map_err(|e| NxsError::ConvertParseError {
                        offset: 0,
                        msg: e.to_string(),
                    })?
                    .to_string();

                if !in_record && tag == record_tag {
                    // Self-closing record element — collect attrs as one record
                    let mut fields: Vec<(String, String)> = Vec::new();
                    collect_attrs(&e, &reader, &mut fields, attrs_mode)?;
                    on_record(fields)?;
                } else if in_record {
                    let key = if current_path.is_empty() {
                        tag.clone()
                    } else {
                        format!("{}.{tag}", path_key(&current_path))
                    };
                    let mut fields: Vec<(String, String)> = Vec::new();
                    collect_attrs_with_prefix(&e, &reader, &mut fields, attrs_mode, &key)?;
                    current_fields.extend(fields);
                }
            }
            Ok(Event::End(_)) => {
                if in_record && depth == record_depth {
                    // End of the record element
                    on_record(std::mem::take(&mut current_fields))?;
                    in_record = false;
                } else if in_record && !current_path.is_empty() {
                    current_path.pop();
                    current_text_key = None;
                }
                depth = depth.saturating_sub(1);
            }
            Ok(Event::Text(e)) => {
                if in_record {
                    let text = e
                        .unescape()
                        .map_err(|e| NxsError::ConvertParseError {
                            offset: 0,
                            msg: e.to_string(),
                        })?
                        .to_string();
                    let trimmed = text.trim().to_string();
                    if !trimmed.is_empty() {
                        if let Some(key) = &current_text_key {
                            current_fields.push((key.clone(), trimmed));
                        }
                    }
                }
            }
            Ok(_) => {}
            Err(e) => {
                return Err(NxsError::ConvertParseError {
                    offset: 0,
                    msg: e.to_string(),
                });
            }
        }
        buf.clear();
    }
    Ok(())
}

fn path_key(parts: &[String]) -> String {
    parts.join(".")
}

fn collect_attrs<B: BufRead>(
    e: &quick_xml::events::BytesStart<'_>,
    reader: &Reader<B>,
    fields: &mut Vec<(String, String)>,
    mode: XmlAttrsMode,
) -> Result<()> {
    collect_attrs_with_prefix(e, reader, fields, mode, "")
}

fn collect_attrs_with_prefix<B: BufRead>(
    e: &quick_xml::events::BytesStart<'_>,
    reader: &Reader<B>,
    fields: &mut Vec<(String, String)>,
    mode: XmlAttrsMode,
    _parent: &str,
) -> Result<()> {
    for attr in e.attributes().flatten() {
        let key_bytes = attr.key.local_name();
        let key = reader
            .decoder()
            .decode(key_bytes.as_ref())
            .map_err(|e| NxsError::ConvertParseError {
                offset: 0,
                msg: e.to_string(),
            })?
            .to_string();
        let val = attr
            .decode_and_unescape_value(reader.decoder())
            .map_err(|e| NxsError::ConvertParseError {
                offset: 0,
                msg: e.to_string(),
            })?
            .to_string();

        let field_key = match mode {
            XmlAttrsMode::AsFields => key,
            XmlAttrsMode::Prefix => format!("@{key}"),
        };
        fields.push((field_key, val));
    }
    Ok(())
}

fn make_reader<R: Read>(reader: R) -> Reader<std::io::BufReader<R>> {
    let mut r = Reader::from_reader(std::io::BufReader::new(reader));
    r.config_mut().trim_text(true);
    r
}

/// Pass 1 — infer schema from an XML reader.
pub fn infer_schema<R: Read>(mut reader: R, args: &ImportArgs) -> Result<InferredSchema> {
    let record_tag = args
        .xml_record_tag
        .as_deref()
        .ok_or_else(|| NxsError::ConvertParseError {
            offset: 0,
            msg: "XML import requires --xml-record-tag".into(),
        })?;

    // Buffer the input so we can (1) pre-scan for entity expansion, (2) parse.
    let mut raw = Vec::new();
    reader
        .read_to_end(&mut raw)
        .map_err(|e| NxsError::IoError(e.to_string()))?;
    check_for_entity_expansion(&raw)?;

    let xml_reader = make_reader(std::io::Cursor::new(raw));
    let mut acc = InferredSchema::default();

    parse_records(xml_reader, args, record_tag, |fields| {
        infer::merge(&mut acc, &fields);
        Ok(())
    })?;

    infer::finalize(acc, args.conflict)
}

/// Pass 2 — emit .nxb from an XML reader using the inferred schema.
pub fn emit<R: Read, W: Write>(
    mut reader: R,
    mut writer: W,
    schema: &InferredSchema,
    args: &ImportArgs,
) -> Result<ImportReport> {
    let record_tag = args
        .xml_record_tag
        .as_deref()
        .ok_or_else(|| NxsError::ConvertParseError {
            offset: 0,
            msg: "XML import requires --xml-record-tag".into(),
        })?;

    // Pre-scan for entity expansion before parsing.
    let mut raw = Vec::new();
    reader
        .read_to_end(&mut raw)
        .map_err(|e| NxsError::IoError(e.to_string()))?;
    check_for_entity_expansion(&raw)?;

    let key_names: Vec<&str> = schema.keys.iter().map(|k| k.name.as_str()).collect();
    let nxs_schema = Schema::new(&key_names);
    let mut nxs_writer = NxsWriter::new(&nxs_schema);
    let mut records_written = 0usize;

    let xml_reader = make_reader(std::io::Cursor::new(raw));
    parse_records(xml_reader, args, record_tag, |fields| {
        nxs_writer.begin_object();
        for (key, value) in &fields {
            let slot_idx = schema.keys.iter().position(|k| &k.name == key);
            if let Some(idx) = slot_idx {
                if value.is_empty() {
                    continue;
                }
                let slot = Slot(idx as u16);
                let sigil = schema.keys.get(idx).map(|k| k.sigil).unwrap_or(b'"');
                match sigil {
                    b'=' => {
                        if let Ok(i) = value.parse::<i64>() {
                            nxs_writer.write_i64(slot, i);
                        }
                    }
                    b'~' => {
                        if let Ok(f) = value.parse::<f64>() {
                            nxs_writer.write_f64(slot, f);
                        }
                    }
                    b'?' => {
                        nxs_writer.write_bool(slot, value == "true");
                    }
                    b'@' => {
                        if let Ok(t) = value.parse::<i64>() {
                            nxs_writer.write_time(slot, t);
                        }
                        // Non-integer time values omitted; avoids wrong-type blob.
                    }
                    b'<' => {
                        if value.len() % 2 == 0 {
                            if let Ok(bytes) = (0..value.len())
                                .step_by(2)
                                .map(|i| u8::from_str_radix(&value[i..i + 2], 16))
                                .collect::<std::result::Result<Vec<u8>, _>>()
                            {
                                nxs_writer.write_bytes(slot, &bytes);
                            }
                        }
                        // Odd-length or invalid hex: omit rather than write wrong type.
                    }
                    b'^' => {
                        // null: key stays absent
                    }
                    _ => {
                        nxs_writer.write_str(slot, value);
                    }
                }
            }
        }
        nxs_writer.end_object();
        records_written += 1;
        Ok(())
    })?;

    let bytes = nxs_writer.finish();
    let output_bytes = bytes.len();
    writer
        .write_all(&bytes)
        .map_err(|e| NxsError::IoError(e.to_string()))?;

    Ok(ImportReport {
        records_written,
        output_bytes,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::convert::{ConflictPolicy, ImportArgs, ImportFormat, XmlAttrsMode};

    fn args_with_tag(tag: &str) -> ImportArgs {
        ImportArgs {
            from: ImportFormat::Xml,
            conflict: ConflictPolicy::Error,
            xml_record_tag: Some(tag.to_owned()),
            ..ImportArgs::default()
        }
    }

    #[test]
    fn import_xml_record_tag_required() {
        let xml: &[u8] = b"<root><item/></root>";
        let args = ImportArgs {
            from: ImportFormat::Xml,
            conflict: ConflictPolicy::Error,
            xml_record_tag: None, // missing!
            ..ImportArgs::default()
        };
        let result = infer_schema(xml, &args);
        assert!(result.is_err(), "missing --xml-record-tag must fail");
    }

    #[test]
    fn import_xml_attributes_as_fields() {
        let xml: &[u8] = b"<users><user id=\"1\" name=\"alice\"/></users>";
        let args = args_with_tag("user");
        let schema = infer_schema(xml, &args).unwrap();
        assert!(schema.keys.iter().any(|k| k.name == "id"));
        assert!(schema.keys.iter().any(|k| k.name == "name"));
        // id should be int
        let id = schema.keys.iter().find(|k| k.name == "id").unwrap();
        assert_eq!(id.sigil, b'=', "id=\"1\" should infer as int");
    }

    #[test]
    fn import_xml_attributes_prefixed() {
        let xml: &[u8] = b"<users><user id=\"1\" name=\"alice\"/></users>";
        let args = ImportArgs {
            from: ImportFormat::Xml,
            conflict: ConflictPolicy::Error,
            xml_record_tag: Some("user".into()),
            xml_attrs: XmlAttrsMode::Prefix,
            ..ImportArgs::default()
        };
        let schema = infer_schema(xml, &args).unwrap();
        assert!(schema.keys.iter().any(|k| k.name == "@id"));
        assert!(schema.keys.iter().any(|k| k.name == "@name"));
    }

    #[test]
    fn import_xml_nested_elements_become_nested_objects() {
        let xml: &[u8] = b"<users><user><addr><city>NYC</city></addr></user></users>";
        let args = args_with_tag("user");
        let schema = infer_schema(xml, &args).unwrap();
        // Nested element becomes dot-notation key
        assert!(
            schema.keys.iter().any(|k| k.name == "addr.city"),
            "nested element must become dot-notation key"
        );
    }

    #[test]
    fn import_xml_entity_expansion_rejected() {
        // A billion-laughs-style DOCTYPE with ENTITY declarations must be rejected quickly.
        let xml: &[u8] = b"<?xml version=\"1.0\"?>\
            <!DOCTYPE lolz [\
            <!ENTITY lol \"lol\">\
            <!ENTITY lol2 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">\
            ]><root><item/></root>";
        let args = args_with_tag("item");
        let result = infer_schema(xml, &args);
        assert!(result.is_err(), "entity expansion must be rejected");
        assert!(
            matches!(result.unwrap_err(), NxsError::ConvertEntityExpansion),
            "error must be ConvertEntityExpansion"
        );
    }

    #[test]
    fn import_xml_depth_limit_enforced() {
        // Build a deeply nested XML (> 64 levels)
        let depth = 70usize;
        let open: String = "<a>".repeat(depth);
        let close: String = "</a>".repeat(depth);
        let xml = format!("<root><item>{open}text{close}</item></root>");
        let args = ImportArgs {
            from: ImportFormat::Xml,
            xml_record_tag: Some("item".into()),
            max_depth: 64,
            xml_max_depth: 64,
            ..ImportArgs::default()
        };
        let result = infer_schema(xml.as_bytes(), &args);
        assert!(result.is_err());
        assert!(
            matches!(result.unwrap_err(), NxsError::ConvertDepthExceeded),
            "expected ConvertDepthExceeded"
        );
    }

    #[test]
    fn import_xml_utf16_bom_accepted() {
        // quick-xml with the `encoding` feature handles UTF-16 BOM.
        // Here we test with a UTF-8 BOM + declaration (the simpler case supported
        // on all platforms). A proper UTF-16 test would require generating actual
        // UTF-16 encoded bytes.
        let xml: &[u8] = b"<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><item id=\"1\"/></root>";
        let args = args_with_tag("item");
        let schema = infer_schema(xml, &args).unwrap();
        assert!(schema.keys.iter().any(|k| k.name == "id"));
    }

    #[test]
    fn import_xml_unsupported_encoding_exits_3() {
        // An encoding that encoding_rs doesn't support must fail with ConvertParseError.
        // quick-xml with `encoding` feature will fail when it can't transcode EBCDIC.
        // We verify any parse error (not a panic).
        let xml: &[u8] =
            b"<?xml version=\"1.0\" encoding=\"EBCDIC\"?><root><item id=\"1\"/></root>";
        let args = args_with_tag("item");
        // This may succeed (quick-xml falls back to UTF-8) or fail — either is acceptable
        // as long as it doesn't panic. We just run it and check for no panic.
        let _ = infer_schema(xml, &args);
    }
}