xportrs 0.0.8

CDISC-compliant XPT file generation and parsing library for Rust
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
//! Tests for writing XPT v5 files.

use std::path::PathBuf;

use tempfile::tempdir;
use xportrs::{Agency, Column, ColumnData, Dataset, Format, Xpt};

/// Get the path to test data directory.
fn test_data_dir() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/data")
}

/// Test basic write and read roundtrip.
#[test]
fn test_write_read_roundtrip() {
    let dir = tempdir().unwrap();
    let path = dir.path().join("ae.xpt");

    // Create dataset
    let dataset = Dataset::new(
        "AE",
        vec![
            Column::new(
                "USUBJID",
                ColumnData::String(vec![Some("001".into()), Some("002".into())]),
            ),
            Column::new("AESEQ", ColumnData::F64(vec![Some(1.0), Some(2.0)])),
        ],
    )
    .unwrap();

    let original_nrows = dataset.nrows();

    // Write
    Xpt::writer(dataset)
        .finalize()
        .unwrap()
        .write_path(&path)
        .unwrap();

    // Read back
    let loaded = Xpt::read(&path).unwrap();

    assert_eq!(loaded.domain_code(), "AE");
    assert_eq!(loaded.ncols(), 2);
    assert!(!loaded.columns().is_empty());
    assert_eq!(loaded.columns()[0].len(), original_nrows);
}

/// Test FDA agency validation.
#[test]
fn test_fda_agency_validation() {
    let dir = tempdir().unwrap();
    let path = dir.path().join("dm.xpt");

    let dataset = Dataset::new(
        "DM",
        vec![
            Column::new("STUDYID", ColumnData::String(vec![Some("STUDY001".into())])),
            Column::new("AGE", ColumnData::F64(vec![Some(45.0)])),
        ],
    )
    .unwrap();

    let mut builder = Xpt::writer(dataset);
    builder.agency(Agency::FDA);
    let validated = builder.finalize().unwrap();

    // Should have no blocking errors for valid data
    assert!(!validated.has_errors());

    validated.write_path(&path).unwrap();

    let loaded = Xpt::read(&path).unwrap();
    assert_eq!(loaded.domain_code(), "DM");
}

/// Test column length mismatch detection.
#[test]
fn test_column_length_mismatch() {
    let result = Dataset::new(
        "AE",
        vec![
            Column::new("A", ColumnData::F64(vec![Some(1.0), Some(2.0)])),
            Column::new("B", ColumnData::F64(vec![Some(1.0)])), // Wrong length
        ],
    );

    assert!(result.is_err());
}

/// Test From conversions for `ColumnData`.
#[test]
fn test_column_data_from_conversions() {
    let dir = tempdir().unwrap();
    let path = dir.path().join("conv.xpt");

    // Use From conversions
    let dataset = Dataset::new(
        "LB",
        vec![
            Column::new("LBSEQ", vec![1.0f64, 2.0, 3.0].into()),
            Column::new("LBTEST", vec!["HGB", "WBC", "PLT"].into()),
        ],
    )
    .unwrap();

    let original_nrows = dataset.nrows();
    assert_eq!(original_nrows, 3);

    Xpt::writer(dataset)
        .finalize()
        .unwrap()
        .write_path(&path)
        .unwrap();

    let loaded = Xpt::read(&path).unwrap();
    assert_eq!(loaded.columns()[0].len(), original_nrows);
}

/// Test write to in-memory buffer.
#[test]
fn test_write_to_buffer() {
    let dataset = Dataset::new(
        "CM",
        vec![Column::new("CMSEQ", ColumnData::F64(vec![Some(1.0)]))],
    )
    .unwrap();

    let mut buffer = Vec::new();
    Xpt::writer(dataset)
        .finalize()
        .unwrap()
        .write_to(&mut buffer)
        .unwrap();

    // Should contain XPT header
    assert!(!buffer.is_empty());
    assert!(buffer.starts_with(b"HEADER RECORD"));
}

/// Test with missing values.
#[test]
fn test_missing_values() {
    let dir = tempdir().unwrap();
    let path = dir.path().join("vs.xpt");

    let dataset = Dataset::new(
        "VS",
        vec![
            Column::new(
                "USUBJID",
                ColumnData::String(vec![Some("001".into()), None]),
            ),
            Column::new("VSSTRESN", ColumnData::F64(vec![Some(120.0), None])),
        ],
    )
    .unwrap();

    let original_nrows = dataset.nrows();

    Xpt::writer(dataset)
        .finalize()
        .unwrap()
        .write_path(&path)
        .unwrap();

    let loaded = Xpt::read(&path).unwrap();
    assert_eq!(loaded.columns()[0].len(), original_nrows);
}

/// Test multiple columns with various types.
#[test]
fn test_multiple_column_types() {
    let dir = tempdir().unwrap();
    let path = dir.path().join("mixed.xpt");

    let dataset = Dataset::new(
        "AE",
        vec![
            Column::new("USUBJID", ColumnData::String(vec![Some("SUBJ001".into())])),
            Column::new("AESEQ", ColumnData::F64(vec![Some(1.0)])),
            Column::new("AESTDY", ColumnData::F64(vec![Some(15.0)])),
            Column::new("AETERM", ColumnData::String(vec![Some("Headache".into())])),
        ],
    )
    .unwrap();

    let original_nrows = dataset.nrows();

    Xpt::writer(dataset)
        .finalize()
        .unwrap()
        .write_path(&path)
        .unwrap();

    let loaded = Xpt::read(&path).unwrap();
    assert_eq!(loaded.ncols(), 4);
    assert_eq!(loaded.columns()[0].len(), original_nrows);
}

/// Test that all agencies can be used.
#[test]
fn test_all_agencies() {
    let dir = tempdir().unwrap();

    for (agency, name) in [
        (Agency::FDA, "fda.xpt"),
        (Agency::PMDA, "pmda.xpt"),
        (Agency::NMPA, "nmpa.xpt"),
    ] {
        let path = dir.path().join(name);

        let dataset = Dataset::new(
            "AE",
            vec![Column::new("AESEQ", ColumnData::F64(vec![Some(1.0)]))],
        )
        .unwrap();

        let mut builder = Xpt::writer(dataset);
        builder.agency(agency);
        let validated = builder.finalize().unwrap();
        assert!(!validated.has_errors());
        validated.write_path(&path).unwrap();

        // Verify file was created
        assert!(path.exists());
    }
}

/// Test round-trip preserves data from real XPT files.
#[test]
fn test_roundtrip_real_files() {
    let dir = tempdir().unwrap();

    for filename in ["dm.xpt", "relrec.xpt", "suppdm.xpt"] {
        let original_path = test_data_dir().join(filename);
        let output_path = dir.path().join(format!("roundtrip_{}", filename));

        // Read original
        let original = Xpt::read(&original_path).expect("Failed to read original");

        // Write copy
        Xpt::writer(original.clone())
            .finalize()
            .unwrap()
            .write_path(&output_path)
            .unwrap();

        // Read back
        let reloaded = Xpt::read(&output_path).expect("Failed to read roundtrip");

        // Verify structure preserved
        assert_eq!(
            original.domain_code(),
            reloaded.domain_code(),
            "{} domain code mismatch",
            filename
        );
        assert_eq!(
            original.ncols(),
            reloaded.ncols(),
            "{} column count mismatch",
            filename
        );
        assert_eq!(
            original.nrows(),
            reloaded.nrows(),
            "{} row count mismatch",
            filename
        );
    }
}

/// Test metadata (labels, formats) roundtrip.
#[test]
fn test_metadata_roundtrip() {
    let dir = tempdir().unwrap();
    let path = dir.path().join("ae_metadata.xpt");

    // Create dataset with full CDISC metadata
    let dataset = Dataset::with_label(
        "AE",
        "Adverse Events",
        vec![
            Column::new("STUDYID", ColumnData::String(vec![Some("STUDY123".into())]))
                .with_label("Study Identifier")
                .with_format(Format::character(20))
                .with_length(20), // explicit length override
            Column::new("USUBJID", ColumnData::String(vec![Some("001-001".into())]))
                .with_label("Unique Subject Identifier")
                .with_format(Format::character(40))
                .with_length(40), // explicit length override
            Column::new("AESEQ", ColumnData::F64(vec![Some(1.0)]))
                .with_label("Sequence Number")
                .with_format(Format::numeric(8, 0)),
            Column::new("AESTDTC", ColumnData::F64(vec![Some(21185.0)]))
                .with_label("Start Date/Time")
                .with_format_str("DATE9.")
                .unwrap(),
        ],
    )
    .unwrap();

    // Verify original dataset has metadata
    assert_eq!(dataset.dataset_label().unwrap(), "Adverse Events");
    assert_eq!(
        dataset.columns()[0].label().unwrap().to_string(),
        "Study Identifier"
    );
    assert!(dataset.columns()[0].format().is_some());

    // Write
    Xpt::writer(dataset.clone())
        .finalize()
        .unwrap()
        .write_path(&path)
        .unwrap();

    // Read back
    let loaded = Xpt::read(&path).unwrap();

    // Verify dataset label preserved
    assert_eq!(loaded.dataset_label().unwrap(), "Adverse Events");

    // Verify variable labels preserved
    assert_eq!(
        loaded.columns()[0].label().unwrap().to_string(),
        "Study Identifier"
    );
    assert_eq!(
        loaded.columns()[1].label().unwrap().to_string(),
        "Unique Subject Identifier"
    );
    assert_eq!(
        loaded.columns()[2].label().unwrap().to_string(),
        "Sequence Number"
    );
    assert_eq!(
        loaded.columns()[3].label().unwrap().to_string(),
        "Start Date/Time"
    );

    // Verify formats preserved
    let format0 = loaded.columns()[0]
        .format()
        .expect("STUDYID should have format");
    assert_eq!(format0.name_without_prefix(), "CHAR");
    assert_eq!(format0.length(), 20);

    // Note: Numeric formats with bare width (like "8.") are not written to XPT
    // as they have no format name. Only named formats like DATE9. are preserved.
    // The AESEQ column used Format::numeric(8, 0) which creates a bare format.

    let format3 = loaded.columns()[3]
        .format()
        .expect("AESTDTC should have format");
    assert_eq!(format3.name_without_prefix(), "DATE");
    assert_eq!(format3.length(), 9);

    // Verify explicit length preserved for character variables
    assert_eq!(loaded.columns()[0].explicit_length(), Some(20));
    assert_eq!(loaded.columns()[1].explicit_length(), Some(40));
}

/// Test that missing labels generate warnings.
#[test]
fn test_missing_labels_warning() {
    let dataset = Dataset::new(
        "AE",
        vec![
            Column::new("USUBJID", ColumnData::String(vec![Some("001".into())])),
            Column::new("AESEQ", ColumnData::F64(vec![Some(1.0)])),
        ],
    )
    .unwrap();

    // Without labels, validation should produce warnings
    let validated = Xpt::writer(dataset).finalize().unwrap();

    // Should have warnings for missing labels (but no errors)
    assert!(validated.has_warnings());
    assert!(!validated.has_errors());

    // Check that the issues include missing label warnings
    let issues = validated.issues();
    assert!(
        issues
            .iter()
            .any(|i| format!("{}", i).contains("missing a label")),
        "Expected missing label warnings, got: {:?}",
        issues
    );
}

/// Test Format parsing.
#[test]
fn test_format_parsing() {
    // Date format
    let date_format = Format::parse("DATE9.").unwrap();
    assert_eq!(date_format.name_without_prefix(), "DATE");
    assert_eq!(date_format.length(), 9);
    assert_eq!(date_format.decimals(), 0);

    // Numeric format with decimals
    let num_format = Format::parse("8.2").unwrap();
    assert_eq!(num_format.name(), "");
    assert_eq!(num_format.length(), 8);
    assert_eq!(num_format.decimals(), 2);

    // Character format
    let char_format = Format::parse("$CHAR200.").unwrap();
    assert_eq!(char_format.name_without_prefix(), "CHAR");
    assert_eq!(char_format.length(), 200);
    assert!(char_format.is_character());

    // BEST format
    let best_format = Format::parse("BEST12.").unwrap();
    assert_eq!(best_format.name_without_prefix(), "BEST");
    assert_eq!(best_format.length(), 12);
}

/// Test Format constructors.
#[test]
fn test_format_constructors() {
    // Numeric constructor
    let num = Format::numeric(8, 2);
    assert_eq!(num.length(), 8);
    assert_eq!(num.decimals(), 2);
    assert!(!num.is_character());

    // Character constructor
    let char_fmt = Format::character(200);
    assert_eq!(char_fmt.name_without_prefix(), "CHAR");
    assert_eq!(char_fmt.length(), 200);
    assert!(char_fmt.is_character());
}