hedl-csv 2.0.0

HEDL to/from CSV conversion
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
// Dweve HEDL - Hierarchical Entity Data Language
//
// Copyright (c) 2025 Dweve IP B.V. and individual contributors.
//
// SPDX-License-Identifier: Apache-2.0

//! Reader variant and parser edge case tests.
//!
//! Tests `from_csv_reader` functions and various parser edge cases.

use hedl_core::{Document, Item, MatrixList, Node, Value};
use hedl_csv::{
    from_csv_reader, from_csv_reader_with_config, from_csv_with_config, to_csv_list,
    to_csv_list_writer, FromCsvConfig,
};
use std::io::Cursor;

// =============================================================================
// from_csv_reader Tests
// =============================================================================

#[test]
fn test_from_csv_reader_basic() {
    let csv = "id,name,age\n1,Alice,30\n2,Bob,25\n";
    let reader = Cursor::new(csv);

    let doc = from_csv_reader(reader, "Person", &["name", "age"]).unwrap();

    let list = doc.get("persons").unwrap().as_list().unwrap();
    assert_eq!(list.rows.len(), 2);
    assert_eq!(
        list.rows[0].fields[1],
        Value::String("Alice".to_string().into())
    );
}

#[test]
fn test_from_csv_reader_with_config() {
    let csv = "id\tname\tage\n1\tAlice\t30\n";
    let reader = Cursor::new(csv);

    let config = FromCsvConfig {
        delimiter: b'\t',
        ..Default::default()
    };

    let doc = from_csv_reader_with_config(reader, "Person", &["name", "age"], config).unwrap();

    let list = doc.get("persons").unwrap().as_list().unwrap();
    assert_eq!(list.rows.len(), 1);
}

#[test]
fn test_from_csv_reader_empty() {
    let csv = "";
    let reader = Cursor::new(csv);

    let result = from_csv_reader(reader, "Person", &["name"]);
    // Empty CSV may succeed with empty list or error
    let _ = result;
}

#[test]
fn test_from_csv_reader_large() {
    let mut csv = String::from("id,value\n");
    for i in 1..=1000 {
        csv.push_str(&format!("{},{}\n", i, i * 10));
    }
    let reader = Cursor::new(csv);

    let doc = from_csv_reader(reader, "Item", &["value"]).unwrap();

    let list = doc.get("items").unwrap().as_list().unwrap();
    assert_eq!(list.rows.len(), 1000);
}

#[test]
fn test_from_csv_reader_bytes() {
    let csv = b"id,name\n1,Alice\n";
    let reader = Cursor::new(csv.as_slice());

    let doc = from_csv_reader(reader, "Person", &["name"]).unwrap();

    let list = doc.get("persons").unwrap().as_list().unwrap();
    assert_eq!(list.rows.len(), 1);
}

// =============================================================================
// Writer Tests
// =============================================================================

#[test]
fn test_to_csv_list_writer_basic() {
    let mut doc = Document::new((1, 0));
    let mut list = MatrixList::new("Person", vec!["id".to_string(), "name".to_string()]);

    list.add_row(Node::new(
        "Person",
        "1",
        vec![Value::String("1".into()), Value::String("Alice".into())],
    ));

    doc.root.insert("people".to_string(), Item::List(list));

    let mut buffer = Vec::new();
    to_csv_list_writer(&doc, "people", &mut buffer).unwrap();

    let csv = String::from_utf8(buffer).unwrap();
    assert!(csv.contains("Alice"));
}

#[test]
fn test_to_csv_list_writer_large() {
    let mut doc = Document::new((1, 0));
    let mut list = MatrixList::new("Item", vec!["id".to_string(), "value".to_string()]);

    for i in 1..=1000 {
        let id_str = i.to_string();
        list.add_row(Node::new(
            "Item",
            &id_str,
            vec![Value::String(id_str.clone().into()), Value::Int(i)],
        ));
    }

    doc.root.insert("items".to_string(), Item::List(list));

    let mut buffer = Vec::new();
    to_csv_list_writer(&doc, "items", &mut buffer).unwrap();

    let csv = String::from_utf8(buffer).unwrap();
    let lines: Vec<&str> = csv.lines().collect();
    assert_eq!(lines.len(), 1001); // header + 1000 rows
}

// =============================================================================
// Parser Edge Cases
// =============================================================================

#[test]
fn test_single_column_csv() {
    let csv = "id\n1\n2\n3\n";
    let doc = from_csv_with_config(csv, "Item", &[], FromCsvConfig::default()).unwrap();

    let list = doc.get("items").unwrap().as_list().unwrap();
    assert_eq!(list.rows.len(), 3);
    assert_eq!(list.schema.len(), 1); // Only id column
}

#[test]
fn test_single_row_csv() {
    let csv = "id,name,age\n1,Alice,30\n";
    let doc =
        from_csv_with_config(csv, "Person", &["name", "age"], FromCsvConfig::default()).unwrap();

    let list = doc.get("persons").unwrap().as_list().unwrap();
    assert_eq!(list.rows.len(), 1);
}

#[test]
fn test_single_cell_csv() {
    let csv = "id\n1\n";
    let doc = from_csv_with_config(csv, "Item", &[], FromCsvConfig::default()).unwrap();

    let list = doc.get("items").unwrap().as_list().unwrap();
    assert_eq!(list.rows.len(), 1);
}

#[test]
fn test_headers_only_no_data() {
    let csv = "id,name,age\n";
    let doc =
        from_csv_with_config(csv, "Person", &["name", "age"], FromCsvConfig::default()).unwrap();

    let list = doc.get("persons").unwrap().as_list().unwrap();
    assert_eq!(list.rows.len(), 0);
}

#[test]
fn test_blank_lines_in_data() {
    let csv = "id,name\n1,Alice\n\n2,Bob\n";
    let result = from_csv_with_config(csv, "Person", &["name"], FromCsvConfig::default());

    // CSV library should handle blank lines (may skip or include)
    // Just ensure it doesn't panic
    let _ = result;
}

#[test]
fn test_whitespace_only_row() {
    let csv = "id,name\n1,Alice\n   \n2,Bob\n";
    let result = from_csv_with_config(csv, "Person", &["name"], FromCsvConfig::default());

    // Should handle gracefully
    let _ = result;
}

#[test]
fn test_comment_like_lines() {
    let csv = "id,name\n# comment\n1,Alice\n";
    let result = from_csv_with_config(csv, "Person", &["name"], FromCsvConfig::default());

    // CSV format doesn't support comments, so this should error or include as data
    let _ = result;
}

// =============================================================================
// Type Inference Edge Cases
// =============================================================================

#[test]
fn test_leading_zeros_not_octal() {
    let csv = "id,code\n1,0123\n";
    let doc = from_csv_with_config(csv, "Item", &["code"], FromCsvConfig::default()).unwrap();

    let list = doc.get("items").unwrap().as_list().unwrap();
    // Leading zeros: could be int 123 or string "0123"
    // Current implementation should treat as int
    assert!(matches!(
        list.rows[0].fields[1],
        Value::Int(123) | Value::String(_)
    ));
}

#[test]
fn test_scientific_notation() {
    let csv = "id,value\n1,1.5e10\n2,3.14E-5\n";
    let doc = from_csv_with_config(csv, "Item", &["value"], FromCsvConfig::default()).unwrap();

    let list = doc.get("items").unwrap().as_list().unwrap();
    assert!(matches!(list.rows[0].fields[1], Value::Float(_)));
    assert!(matches!(list.rows[1].fields[1], Value::Float(_)));
}

#[test]
fn test_hexadecimal_not_parsed() {
    let csv = "id,value\n1,0xFF\n";
    let doc = from_csv_with_config(csv, "Item", &["value"], FromCsvConfig::default()).unwrap();

    let list = doc.get("items").unwrap().as_list().unwrap();
    // Hex should be treated as string, not parsed
    assert_eq!(
        list.rows[0].fields[1],
        Value::String("0xFF".to_string().into())
    );
}

#[test]
fn test_plus_sign_in_number() {
    let csv = "id,value\n1,+42\n2,+3.14\n";
    let doc = from_csv_with_config(csv, "Item", &["value"], FromCsvConfig::default()).unwrap();

    let list = doc.get("items").unwrap().as_list().unwrap();
    // Plus sign may or may not be accepted
    let _ = list.rows[0].fields[1].clone();
}

#[test]
fn test_underscore_in_number() {
    let csv = "id,value\n1,1_000_000\n";
    let doc = from_csv_with_config(csv, "Item", &["value"], FromCsvConfig::default()).unwrap();

    let list = doc.get("items").unwrap().as_list().unwrap();
    // Underscores not valid in standard number format, should be string
    assert_eq!(
        list.rows[0].fields[1],
        Value::String("1_000_000".to_string().into())
    );
}

#[test]
fn test_very_large_integer() {
    let csv = "id,value\n1,999999999999999999\n";
    let doc = from_csv_with_config(csv, "Item", &["value"], FromCsvConfig::default()).unwrap();

    let list = doc.get("items").unwrap().as_list().unwrap();
    // Should parse as i64
    assert!(matches!(list.rows[0].fields[1], Value::Int(_)));
}

#[test]
fn test_integer_overflow() {
    let csv = "id,value\n1,99999999999999999999\n";
    let doc = from_csv_with_config(csv, "Item", &["value"], FromCsvConfig::default()).unwrap();

    let list = doc.get("items").unwrap().as_list().unwrap();
    // Too large for i64: may parse as float or string depending on implementation
    let _ = list.rows[0].fields[1].clone();
}

#[test]
fn test_float_precision() {
    let csv = "id,value\n1,3.141592653589793238\n";
    let doc = from_csv_with_config(csv, "Item", &["value"], FromCsvConfig::default()).unwrap();

    let list = doc.get("items").unwrap().as_list().unwrap();
    assert!(matches!(list.rows[0].fields[1], Value::Float(_)));
}

// =============================================================================
// Special Value Tests
// =============================================================================

#[test]
fn test_null_representations() {
    let csv = "id,a,b,c,d\n1,~,,null,NULL\n";
    let doc =
        from_csv_with_config(csv, "Item", &["a", "b", "c", "d"], FromCsvConfig::default()).unwrap();

    let list = doc.get("items").unwrap().as_list().unwrap();
    // ~ and empty should be null
    assert_eq!(list.rows[0].fields[1], Value::Null);
    assert_eq!(list.rows[0].fields[2], Value::Null);
    // "null" and "NULL" should be strings (not special)
    assert_eq!(
        list.rows[0].fields[3],
        Value::String("null".to_string().into())
    );
    assert_eq!(
        list.rows[0].fields[4],
        Value::String("NULL".to_string().into())
    );
}

#[test]
fn test_boolean_case_sensitivity() {
    let csv = "id,a,b,c,d\n1,true,True,TRUE,false\n2,False,FALSE,tRuE,fAlSe\n";
    let doc =
        from_csv_with_config(csv, "Item", &["a", "b", "c", "d"], FromCsvConfig::default()).unwrap();

    let list = doc.get("items").unwrap().as_list().unwrap();
    // Only lowercase "true" and "false" should be booleans
    assert_eq!(list.rows[0].fields[1], Value::Bool(true));
    assert_eq!(list.rows[0].fields[4], Value::Bool(false));
}

#[test]
fn test_reference_formats() {
    let csv = "id,ref1,ref2,ref3\n1,@user1,@User:user2,@Type:id:subid\n";
    let doc = from_csv_with_config(
        csv,
        "Item",
        &["ref1", "ref2", "ref3"],
        FromCsvConfig::default(),
    )
    .unwrap();

    let list = doc.get("items").unwrap().as_list().unwrap();
    // All @ prefixed values should be references
    assert!(matches!(list.rows[0].fields[1], Value::Reference(_)));
    assert!(matches!(list.rows[0].fields[2], Value::Reference(_)));
    // Complex reference might not parse
    let _ = list.rows[0].fields[3].clone();
}

#[test]
fn test_expression_syntax() {
    let csv = "id,expr1\n1,$(x)\n";
    let doc = from_csv_with_config(csv, "Item", &["expr1"], FromCsvConfig::default()).unwrap();

    let list = doc.get("items").unwrap().as_list().unwrap();
    assert!(matches!(list.rows[0].fields[1], Value::Expression(_)));
}

#[test]
fn test_invalid_expression() {
    let csv = "id,expr\n1,$(unclosed\n";
    let result = from_csv_with_config(csv, "Item", &["expr"], FromCsvConfig::default());

    // Invalid expression should error or fallback to string
    let _ = result;
}

// =============================================================================
// Custom List Key Tests
// =============================================================================

#[test]
fn test_custom_list_key_people() {
    let csv = "id,name\n1,Alice\n";
    let config = FromCsvConfig {
        list_key: Some("people".to_string()),
        ..Default::default()
    };
    let doc = from_csv_with_config(csv, "Person", &["name"], config).unwrap();

    assert!(doc.get("people").is_some());
    assert!(doc.get("persons").is_none());
}

#[test]
fn test_custom_list_key_dataset() {
    let csv = "id,value\n1,42\n";
    let config = FromCsvConfig {
        list_key: Some("dataset".to_string()),
        ..Default::default()
    };
    let doc = from_csv_with_config(csv, "Data", &["value"], config).unwrap();

    assert!(doc.get("dataset").is_some());
    assert!(doc.get("datas").is_none());
}

#[test]
fn test_custom_list_key_case_sensitive() {
    let csv = "id,name\n1,test\n";
    let config = FromCsvConfig {
        list_key: Some("MyCustomList".to_string()),
        ..Default::default()
    };
    let doc = from_csv_with_config(csv, "Item", &["name"], config).unwrap();

    assert!(doc.get("MyCustomList").is_some());
    assert!(doc.get("mycustomlist").is_none());
}

#[test]
fn test_list_key_roundtrip() {
    let csv = "id,name\n1,Alice\n";
    let config = FromCsvConfig {
        list_key: Some("people".to_string()),
        ..Default::default()
    };
    let doc = from_csv_with_config(csv, "Person", &["name"], config).unwrap();

    let csv_out = to_csv_list(&doc, "people").unwrap();
    assert!(csv_out.contains("Alice"));
}

// =============================================================================
// Whitespace and Trimming Tests
// =============================================================================

#[test]
fn test_leading_whitespace_with_trim() {
    let csv = "id,name\n1,  Alice\n";
    let config = FromCsvConfig {
        trim: true,
        ..Default::default()
    };
    let doc = from_csv_with_config(csv, "Person", &["name"], config).unwrap();

    let list = doc.get("persons").unwrap().as_list().unwrap();
    assert_eq!(
        list.rows[0].fields[1],
        Value::String("Alice".to_string().into())
    );
}

#[test]
fn test_trailing_whitespace_with_trim() {
    let csv = "id,name\n1,Alice  \n";
    let config = FromCsvConfig {
        trim: true,
        ..Default::default()
    };
    let doc = from_csv_with_config(csv, "Person", &["name"], config).unwrap();

    let list = doc.get("persons").unwrap().as_list().unwrap();
    assert_eq!(
        list.rows[0].fields[1],
        Value::String("Alice".to_string().into())
    );
}

#[test]
fn test_whitespace_preserved_without_trim() {
    let csv = "id,name\n1,  Alice  \n";
    let config = FromCsvConfig {
        trim: false,
        ..Default::default()
    };
    let doc = from_csv_with_config(csv, "Person", &["name"], config).unwrap();

    let list = doc.get("persons").unwrap().as_list().unwrap();
    let name = match &list.rows[0].fields[1] {
        Value::String(s) => s.as_ref(),
        _ => panic!("Expected string"),
    };
    assert!(name.starts_with(' ') && name.ends_with(' '));
}

#[test]
fn test_tab_whitespace_with_trim() {
    let csv = "id,name\n1,\t\tAlice\t\t\n";
    let config = FromCsvConfig {
        trim: true,
        ..Default::default()
    };
    let doc = from_csv_with_config(csv, "Person", &["name"], config).unwrap();

    let list = doc.get("persons").unwrap().as_list().unwrap();
    assert_eq!(
        list.rows[0].fields[1],
        Value::String("Alice".to_string().into())
    );
}

#[test]
fn test_mixed_whitespace_chars() {
    let csv = "id,name\n1,  \t Alice \t  \n";
    let config = FromCsvConfig {
        trim: true,
        ..Default::default()
    };
    let doc = from_csv_with_config(csv, "Person", &["name"], config).unwrap();

    let list = doc.get("persons").unwrap().as_list().unwrap();
    assert_eq!(
        list.rows[0].fields[1],
        Value::String("Alice".to_string().into())
    );
}

// =============================================================================
// No Headers Mode Tests
// =============================================================================

#[test]
fn test_no_headers_basic() {
    let csv = "1,Alice,30\n2,Bob,25\n";
    let config = FromCsvConfig {
        has_headers: false,
        ..Default::default()
    };
    let doc = from_csv_with_config(csv, "Person", &["name", "age"], config).unwrap();

    let list = doc.get("persons").unwrap().as_list().unwrap();
    assert_eq!(list.rows.len(), 2);
}

#[test]
fn test_no_headers_single_column() {
    let csv = "1\n2\n3\n";
    let config = FromCsvConfig {
        has_headers: false,
        ..Default::default()
    };
    let doc = from_csv_with_config(csv, "Item", &[], config).unwrap();

    let list = doc.get("items").unwrap().as_list().unwrap();
    assert_eq!(list.rows.len(), 3);
}

#[test]
fn test_no_headers_many_columns() {
    let csv = "1,a,b,c,d\n2,e,f,g,h\n";
    let config = FromCsvConfig {
        has_headers: false,
        ..Default::default()
    };
    let doc = from_csv_with_config(csv, "Item", &["col1", "col2", "col3", "col4"], config).unwrap();

    let list = doc.get("items").unwrap().as_list().unwrap();
    assert_eq!(list.rows.len(), 2);
}