hedl-cli 2.0.0

HEDL command-line interface
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
// Dweve HEDL - Hierarchical Entity Data Language
//
// Copyright (c) 2025 Dweve IP B.V. and individual contributors.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE file at the
// root of this repository or at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Conversion commands - HEDL format interoperability
//!
//! This module provides bidirectional conversion between HEDL and popular data formats:
//! - JSON (compact and pretty-printed)
//! - YAML
//! - XML (compact and pretty-printed)
//! - CSV
//! - Parquet
//!
//! All conversions preserve data fidelity where possible, with format-specific
//! optimizations and configurations.

use super::{read_file, write_output};
use crate::error::CliError;
use hedl_c14n::canonicalize;
use hedl_core::parse;
use hedl_csv::{from_csv as csv_to_hedl, to_csv_with_config, ToCsvConfig};
use hedl_json::{from_json as json_to_hedl, to_json_value, FromJsonConfig, ToJsonConfig};
use hedl_parquet::{from_parquet as parquet_to_hedl, to_parquet as hedl_to_parquet};
use hedl_toon::{hedl_to_toon, toon_to_hedl};
use hedl_xml::{from_xml as xml_to_hedl, to_xml as hedl_to_xml, FromXmlConfig, ToXmlConfig};
use hedl_yaml::{from_yaml as yaml_to_hedl, to_yaml as hedl_to_yaml, FromYamlConfig, ToYamlConfig};
use std::path::Path;

// JSON conversion

/// Convert a HEDL file to JSON format.
///
/// Parses a HEDL file and converts it to JSON, with options for metadata inclusion
/// and pretty-printing.
///
/// # Arguments
///
/// * `file` - Path to the HEDL file to convert
/// * `output` - Optional output file path. If `None`, writes to stdout
/// * `metadata` - If `true`, includes HEDL-specific metadata in the JSON output
/// * `pretty` - If `true`, pretty-prints the JSON with indentation
///
/// # Returns
///
/// Returns `Ok(())` on success.
///
/// # Errors
///
/// Returns `Err` if:
/// - The file cannot be read
/// - The file contains syntax errors
/// - JSON conversion fails
/// - Output writing fails
///
/// # Examples
///
/// ```no_run
/// use hedl_cli::commands::to_json;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Convert to compact JSON on stdout
/// to_json("data.hedl", None, false, false)?;
///
/// // Convert to pretty JSON with metadata
/// to_json("data.hedl", Some("output.json"), true, true)?;
/// # Ok(())
/// # }
/// ```
pub fn to_json(
    file: &str,
    output: Option<&str>,
    metadata: bool,
    pretty: bool,
) -> Result<(), CliError> {
    let content = read_file(file)?;

    let doc =
        parse(content.as_bytes()).map_err(|e| CliError::parse(format!("Parse error: {e}")))?;

    let config = ToJsonConfig {
        include_metadata: metadata,
        ..Default::default()
    };

    let value = to_json_value(&doc, &config)
        .map_err(|e| CliError::json_conversion(format!("JSON conversion error: {e}")))?;
    let output_str = if pretty {
        serde_json::to_string_pretty(&value)
            .map_err(|e| CliError::json_conversion(format!("JSON format error: {e}")))?
    } else {
        serde_json::to_string(&value)
            .map_err(|e| CliError::json_conversion(format!("JSON format error: {e}")))?
    };

    write_output(&output_str, output)
}

/// Convert a JSON file to HEDL format.
///
/// Parses a JSON file and converts it to canonical HEDL format.
///
/// # Arguments
///
/// * `file` - Path to the JSON file to convert
/// * `output` - Optional output file path. If `None`, writes to stdout
///
/// # Returns
///
/// Returns `Ok(())` on success.
///
/// # Errors
///
/// Returns `Err` if:
/// - The file cannot be read
/// - The JSON is malformed
/// - JSON-to-HEDL conversion fails
/// - HEDL canonicalization fails
/// - Output writing fails
///
/// # Examples
///
/// ```no_run
/// use hedl_cli::commands::from_json;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Convert JSON to HEDL on stdout
/// from_json("data.json", None)?;
///
/// // Convert JSON to HEDL file
/// from_json("data.json", Some("output.hedl"))?;
/// # Ok(())
/// # }
/// ```
pub fn from_json(file: &str, output: Option<&str>) -> Result<(), CliError> {
    let content = read_file(file)?;

    // FK relationships are automatically detected and nested
    let doc = json_to_hedl(&content, &FromJsonConfig::default())
        .map_err(|e| CliError::json_conversion(format!("JSON conversion error: {e}")))?;

    let hedl = canonicalize(&doc)
        .map_err(|e| CliError::canonicalization(format!("HEDL generation error: {e}")))?;

    write_output(&hedl, output)
}

// YAML conversion

/// Convert a HEDL file to YAML format.
///
/// Parses a HEDL file and converts it to YAML format.
///
/// # Arguments
///
/// * `file` - Path to the HEDL file to convert
/// * `output` - Optional output file path. If `None`, writes to stdout
///
/// # Returns
///
/// Returns `Ok(())` on success.
///
/// # Errors
///
/// Returns `Err` if:
/// - The file cannot be read
/// - The file contains syntax errors
/// - YAML conversion fails
/// - Output writing fails
///
/// # Examples
///
/// ```no_run
/// use hedl_cli::commands::to_yaml;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Convert to YAML on stdout
/// to_yaml("data.hedl", None)?;
///
/// // Convert to YAML file
/// to_yaml("data.hedl", Some("output.yaml"))?;
/// # Ok(())
/// # }
/// ```
pub fn to_yaml(file: &str, output: Option<&str>) -> Result<(), CliError> {
    let content = read_file(file)?;

    let doc =
        parse(content.as_bytes()).map_err(|e| CliError::parse(format!("Parse error: {e}")))?;

    let config = ToYamlConfig::default();
    let yaml = hedl_to_yaml(&doc, &config)
        .map_err(|e| CliError::yaml_conversion(format!("YAML conversion error: {e}")))?;

    write_output(&yaml, output)
}

/// Convert a YAML file to HEDL format.
///
/// Parses a YAML file and converts it to canonical HEDL format.
///
/// # Arguments
///
/// * `file` - Path to the YAML file to convert
/// * `output` - Optional output file path. If `None`, writes to stdout
///
/// # Returns
///
/// Returns `Ok(())` on success.
///
/// # Errors
///
/// Returns `Err` if:
/// - The file cannot be read
/// - The YAML is malformed
/// - YAML-to-HEDL conversion fails
/// - HEDL canonicalization fails
/// - Output writing fails
///
/// # Examples
///
/// ```no_run
/// use hedl_cli::commands::from_yaml;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Convert YAML to HEDL on stdout
/// from_yaml("data.yaml", None)?;
///
/// // Convert YAML to HEDL file
/// from_yaml("data.yml", Some("output.hedl"))?;
/// # Ok(())
/// # }
/// ```
pub fn from_yaml(file: &str, output: Option<&str>) -> Result<(), CliError> {
    let content = read_file(file)?;

    let config = FromYamlConfig::default();
    let doc = yaml_to_hedl(&content, &config)
        .map_err(|e| CliError::yaml_conversion(format!("YAML conversion error: {e}")))?;

    let hedl = canonicalize(&doc)
        .map_err(|e| CliError::canonicalization(format!("HEDL generation error: {e}")))?;

    write_output(&hedl, output)
}

// XML conversion

/// Convert a HEDL file to XML format.
///
/// Parses a HEDL file and converts it to XML, with optional pretty-printing.
///
/// # Arguments
///
/// * `file` - Path to the HEDL file to convert
/// * `output` - Optional output file path. If `None`, writes to stdout
/// * `pretty` - If `true`, pretty-prints the XML with indentation
///
/// # Returns
///
/// Returns `Ok(())` on success.
///
/// # Errors
///
/// Returns `Err` if:
/// - The file cannot be read
/// - The file contains syntax errors
/// - XML conversion fails
/// - Output writing fails
///
/// # Examples
///
/// ```no_run
/// use hedl_cli::commands::to_xml;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Convert to compact XML on stdout
/// to_xml("data.hedl", None, false)?;
///
/// // Convert to pretty XML file
/// to_xml("data.hedl", Some("output.xml"), true)?;
/// # Ok(())
/// # }
/// ```
pub fn to_xml(file: &str, output: Option<&str>, pretty: bool) -> Result<(), CliError> {
    let content = read_file(file)?;

    let doc =
        parse(content.as_bytes()).map_err(|e| CliError::parse(format!("Parse error: {e}")))?;

    let config = ToXmlConfig {
        pretty,
        include_metadata: true, // Enable for roundtrip fidelity
        ..Default::default()
    };
    let xml = hedl_to_xml(&doc, &config)
        .map_err(|e| CliError::xml_conversion(format!("XML conversion error: {e}")))?;

    write_output(&xml, output)
}

/// Convert an XML file to HEDL format.
///
/// Parses an XML file and converts it to canonical HEDL format.
///
/// # Arguments
///
/// * `file` - Path to the XML file to convert
/// * `output` - Optional output file path. If `None`, writes to stdout
///
/// # Returns
///
/// Returns `Ok(())` on success.
///
/// # Errors
///
/// Returns `Err` if:
/// - The file cannot be read
/// - The XML is malformed
/// - XML-to-HEDL conversion fails
/// - HEDL canonicalization fails
/// - Output writing fails
///
/// # Examples
///
/// ```no_run
/// use hedl_cli::commands::from_xml;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Convert XML to HEDL on stdout
/// from_xml("data.xml", None)?;
///
/// // Convert XML to HEDL file
/// from_xml("data.xml", Some("output.hedl"))?;
/// # Ok(())
/// # }
/// ```
pub fn from_xml(file: &str, output: Option<&str>) -> Result<(), CliError> {
    let content = read_file(file)?;

    let config = FromXmlConfig::default();
    let doc = xml_to_hedl(&content, &config)
        .map_err(|e| CliError::xml_conversion(format!("XML conversion error: {e}")))?;

    let hedl = canonicalize(&doc)
        .map_err(|e| CliError::canonicalization(format!("HEDL generation error: {e}")))?;

    write_output(&hedl, output)
}

// CSV conversion

/// Convert a HEDL file to CSV format.
///
/// Parses a HEDL file and converts it to CSV format. Expects the HEDL file to contain
/// a matrix list that can be represented as a table.
///
/// # Arguments
///
/// * `file` - Path to the HEDL file to convert
/// * `output` - Optional output file path. If `None`, writes to stdout
/// * `include_headers` - If `true`, includes header row with column names (default: `true`)
///
/// # Returns
///
/// Returns `Ok(())` on success.
///
/// # Errors
///
/// Returns `Err` if:
/// - The file cannot be read
/// - The file contains syntax errors
/// - The HEDL structure is not compatible with CSV (e.g., nested structures)
/// - CSV conversion fails
/// - Output writing fails
///
/// # Examples
///
/// ```no_run
/// use hedl_cli::commands::to_csv;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Convert to CSV on stdout with headers
/// to_csv("data.hedl", None, true)?;
///
/// // Convert to CSV without headers (useful for appending)
/// to_csv("data.hedl", Some("output.csv"), false)?;
/// # Ok(())
/// # }
/// ```
pub fn to_csv(file: &str, output: Option<&str>, include_headers: bool) -> Result<(), CliError> {
    let content = read_file(file)?;

    let doc =
        parse(content.as_bytes()).map_err(|e| CliError::parse(format!("Parse error: {e}")))?;

    // Use to_csv_with_config to respect user's header preference
    let config = ToCsvConfig {
        include_headers,
        ..Default::default()
    };
    let csv = to_csv_with_config(&doc, config)
        .map_err(|e| CliError::csv_conversion(format!("CSV conversion error: {e}")))?;

    write_output(&csv, output)
}

/// Convert a CSV file to HEDL format.
///
/// Parses a CSV file and converts it to canonical HEDL format. The first row is assumed
/// to be the header row containing column names.
///
/// # Arguments
///
/// * `file` - Path to the CSV file to convert
/// * `output` - Optional output file path. If `None`, writes to stdout
/// * `type_name` - The type name to use for the HEDL matrix list (must be alphanumeric)
///
/// # Returns
///
/// Returns `Ok(())` on success.
///
/// # Errors
///
/// Returns `Err` if:
/// - The file cannot be read
/// - The CSV is malformed or empty
/// - The type name is invalid (must be alphanumeric with underscores)
/// - CSV-to-HEDL conversion fails
/// - HEDL canonicalization fails
/// - Output writing fails
///
/// # Examples
///
/// ```no_run
/// use hedl_cli::commands::from_csv;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Convert CSV to HEDL on stdout with type name "Person"
/// from_csv("people.csv", None, "Person")?;
///
/// // Convert CSV to HEDL file
/// from_csv("data.csv", Some("output.hedl"), "Record")?;
///
/// // Invalid type name will fail
/// let result = from_csv("data.csv", None, "Invalid-Name!");
/// assert!(result.is_err());
/// # Ok(())
/// # }
/// ```
///
/// # Security
///
/// The type name is validated to prevent injection attacks. Only alphanumeric
/// characters and underscores are allowed.
pub fn from_csv(file: &str, output: Option<&str>, type_name: &str) -> Result<(), CliError> {
    let content = read_file(file)?;

    // Validate type_name to prevent injection
    if !type_name.chars().all(|c| c.is_alphanumeric() || c == '_') {
        return Err(CliError::invalid_input(
            "Type name must be alphanumeric (with underscores allowed)",
        ));
    }

    // Infer column names from header row
    let first_line = content
        .lines()
        .next()
        .ok_or_else(|| CliError::invalid_input("CSV file is empty or has no header row"))?;
    let columns: Vec<&str> = first_line.split(',').skip(1).collect(); // Skip ID column

    let doc = csv_to_hedl(&content, type_name, &columns)
        .map_err(|e| CliError::csv_conversion(format!("CSV conversion error: {e}")))?;

    let hedl = canonicalize(&doc)
        .map_err(|e| CliError::canonicalization(format!("HEDL generation error: {e}")))?;

    write_output(&hedl, output)
}

// Parquet conversion

/// Convert a HEDL file to Parquet format.
///
/// Parses a HEDL file and converts it to Apache Parquet columnar format. This is ideal
/// for analytical workloads and integration with data processing frameworks.
///
/// # Arguments
///
/// * `file` - Path to the HEDL file to convert
/// * `output` - Output Parquet file path (required, cannot write to stdout)
///
/// # Returns
///
/// Returns `Ok(())` on success.
///
/// # Errors
///
/// Returns `Err` if:
/// - The file cannot be read
/// - The file contains syntax errors
/// - The HEDL structure is not compatible with Parquet
/// - Parquet conversion fails
/// - Output file cannot be written
///
/// # Examples
///
/// ```no_run
/// use hedl_cli::commands::to_parquet;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Convert to Parquet file
/// to_parquet("data.hedl", "output.parquet")?;
/// # Ok(())
/// # }
/// ```
///
/// # Note
///
/// Parquet requires a file path for output; it cannot write to stdout due to
/// the binary columnar format.
pub fn to_parquet(file: &str, output: &str) -> Result<(), CliError> {
    let content = read_file(file)?;

    let doc =
        parse(content.as_bytes()).map_err(|e| CliError::parse(format!("Parse error: {e}")))?;

    hedl_to_parquet(&doc, Path::new(output))
        .map_err(|e| CliError::parquet_conversion(format!("Parquet conversion error: {e}")))?;

    Ok(())
}

/// Convert a Parquet file to HEDL format.
///
/// Reads an Apache Parquet file and converts it to canonical HEDL format.
///
/// # Arguments
///
/// * `file` - Path to the Parquet file to convert
/// * `output` - Optional output file path. If `None`, writes to stdout
///
/// # Returns
///
/// Returns `Ok(())` on success.
///
/// # Errors
///
/// Returns `Err` if:
/// - The file cannot be read
/// - The Parquet file is malformed or unsupported
/// - Parquet-to-HEDL conversion fails
/// - HEDL canonicalization fails
/// - Output writing fails
///
/// # Examples
///
/// ```no_run
/// use hedl_cli::commands::from_parquet;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Convert Parquet to HEDL on stdout
/// from_parquet("data.parquet", None)?;
///
/// // Convert Parquet to HEDL file
/// from_parquet("data.parquet", Some("output.hedl"))?;
/// # Ok(())
/// # }
/// ```
pub fn from_parquet(file: &str, output: Option<&str>) -> Result<(), CliError> {
    let doc = parquet_to_hedl(Path::new(file))
        .map_err(|e| CliError::parquet_conversion(format!("Parquet conversion error: {e}")))?;

    let hedl = canonicalize(&doc)
        .map_err(|e| CliError::canonicalization(format!("HEDL generation error: {e}")))?;

    write_output(&hedl, output)
}

// TOON conversion

/// Convert a HEDL file to TOON format.
///
/// Parses a HEDL file and converts it to TOON format.
///
/// # Arguments
///
/// * `file` - Path to the HEDL file to convert
/// * `output` - Optional output file path. If `None`, writes to stdout
///
/// # Returns
///
/// Returns `Ok(())` on success.
///
/// # Errors
///
/// Returns `Err` if:
/// - The file cannot be read
/// - The file contains syntax errors
/// - TOON conversion fails
/// - Output writing fails
///
/// # Examples
///
/// ```no_run
/// use hedl_cli::commands::to_toon;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Convert to TOON on stdout
/// to_toon("data.hedl", None)?;
///
/// // Convert to TOON file
/// to_toon("data.hedl", Some("output.toon"))?;
/// # Ok(())
/// # }
/// ```
pub fn to_toon(file: &str, output: Option<&str>) -> Result<(), CliError> {
    let content = read_file(file)?;

    let doc =
        parse(content.as_bytes()).map_err(|e| CliError::parse(format!("Parse error: {e}")))?;

    let toon = hedl_to_toon(&doc)
        .map_err(|e| CliError::invalid_input(format!("TOON conversion error: {e}")))?;

    write_output(&toon, output)
}

/// Convert a TOON file to HEDL format.
///
/// Parses a TOON file and converts it to HEDL format.
///
/// # Arguments
///
/// * `file` - Path to the TOON file to convert
/// * `output` - Optional output file path. If `None`, writes to stdout
///
/// # Returns
///
/// Returns `Ok(())` on success.
///
/// # Errors
///
/// Returns `Err` if:
/// - The file cannot be read
/// - The file contains syntax errors
/// - HEDL generation fails
/// - Output writing fails
///
/// # Examples
///
/// ```no_run
/// use hedl_cli::commands::from_toon;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Convert TOON to HEDL on stdout
/// from_toon("data.toon", None)?;
///
/// // Convert TOON to HEDL file
/// from_toon("data.toon", Some("output.hedl"))?;
/// # Ok(())
/// # }
/// ```
pub fn from_toon(file: &str, output: Option<&str>) -> Result<(), CliError> {
    let content = read_file(file)?;

    let doc =
        toon_to_hedl(&content).map_err(|e| CliError::parse(format!("TOON parse error: {e}")))?;

    let hedl = canonicalize(&doc)
        .map_err(|e| CliError::canonicalization(format!("HEDL generation error: {e}")))?;

    write_output(&hedl, output)
}