hedl_cli/lib.rs
1// Dweve HEDL - Hierarchical Entity Data Language
2//
3// Copyright (c) 2025 Dweve IP B.V. and individual contributors.
4//
5// SPDX-License-Identifier: Apache-2.0
6//
7// Licensed under the Apache License, Version 2.0 (the "License");
8// you may not use this file except in compliance with the License.
9// You may obtain a copy of the License in the LICENSE file at the
10// root of this repository or at: http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! HEDL CLI library for command-line parsing and execution.
19//!
20//! This library provides the core functionality for the HEDL command-line interface,
21//! including all command implementations for validation, formatting, linting, and
22//! format conversion operations.
23//!
24//! # Commands
25//!
26//! The CLI provides the following commands:
27//!
28//! ## Validation & Inspection
29//!
30//! - **validate**: Validate HEDL file syntax and structure
31//! - **inspect**: Visualize HEDL internal structure with tree view
32//!
33//! ## Formatting & Canonicalization
34//!
35//! - **format**: Format HEDL files to canonical form
36//! - **lint**: Lint HEDL files for best practices and style
37//!
38//! ## Format Conversion
39//!
40//! Bidirectional conversion between HEDL and popular data formats:
41//!
42//! - **to-json/from-json**: JSON conversion (compact and pretty)
43//! - **to-yaml/from-yaml**: YAML conversion
44//! - **to-xml/from-xml**: XML conversion (compact and pretty)
45//! - **to-csv/from-csv**: CSV conversion (tabular data)
46//! - **to-parquet/from-parquet**: Apache Parquet conversion (columnar)
47//!
48//! ## Analysis & Statistics
49//!
50//! - **stats**: Compare size and token efficiency vs other formats
51//!
52//! ## Utilities
53//!
54//! - **completion**: Generate shell completion scripts (bash, zsh, fish, powershell, elvish)
55//!
56//! ## Batch Processing
57//!
58//! - **batch-validate**: Validate multiple files in parallel
59//! - **batch-format**: Format multiple files in parallel
60//! - **batch-lint**: Lint multiple files in parallel
61//!
62//! # Examples
63//!
64//! ## Validation
65//!
66//! ```no_run
67//! use hedl_cli::commands::validate;
68//!
69//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
70//! // Validate a HEDL file
71//! validate("example.hedl", false)?;
72//!
73//! // Strict validation (all references must resolve)
74//! validate("example.hedl", true)?;
75//! # Ok(())
76//! # }
77//! ```
78//!
79//! ## Format Conversion
80//!
81//! ```no_run
82//! use hedl_cli::commands::{to_json, from_json};
83//!
84//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
85//! // Convert HEDL to pretty JSON
86//! to_json("data.hedl", Some("output.json"), false, true)?;
87//!
88//! // Convert JSON back to HEDL
89//! from_json("data.json", Some("output.hedl"))?;
90//! # Ok(())
91//! # }
92//! ```
93//!
94//! ## Batch Processing
95//!
96//! ```no_run
97//! use hedl_cli::commands::batch_validate;
98//!
99//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
100//! // Validate multiple files in parallel
101//! // Args: patterns, strict, recursive, max_depth, parallel, verbose
102//! let files = vec!["file1.hedl".to_string(), "file2.hedl".to_string()];
103//! batch_validate(files, false, true, 10, true, false)?;
104//! # Ok(())
105//! # }
106//! ```
107//!
108//! # Security
109//!
110//! The CLI includes several security features:
111//!
112//! - **File size limits**: Prevents OOM attacks (configurable via `HEDL_MAX_FILE_SIZE`)
113//! - **Input validation**: Type names and parameters are validated
114//! - **Safe conversions**: All format conversions use safe, well-tested libraries
115//!
116//! # Performance
117//!
118//! - **Parallel processing**: Batch commands automatically use parallel processing
119//! - **Optimized stats**: Format conversions run in parallel (3-5x speedup)
120//! - **Memory efficiency**: Streaming where possible, size checks before reading
121//!
122//! # Error Handling
123//!
124//! All commands return `Result<(), String>` for consistent error handling.
125//! Errors are descriptive and include context like file paths and line numbers
126//! where applicable.
127
128#![cfg_attr(not(test), warn(missing_docs))]
129pub mod batch;
130pub mod cli;
131pub mod commands;
132pub mod error;
133pub mod file_discovery;
134
135use clap::{Command, CommandFactory, Parser};
136use cli::Commands;
137
138/// Shared CLI command structure for HEDL.
139///
140/// This struct provides a single source of truth for the CLI command structure,
141/// used by both the main binary and shell completion generation. It implements
142/// `CommandFactory` to allow programmatic access to the clap `Command` structure.
143///
144/// # Examples
145///
146/// ```no_run
147/// use clap::CommandFactory;
148/// use hedl_cli::CliCommand;
149///
150/// // Get the command for completion generation
151/// let mut cmd = CliCommand::command();
152/// ```
153#[derive(Parser)]
154#[command(name = "hedl")]
155#[command(
156 author,
157 version,
158 about = "HEDL - Hierarchical Entity Data Language toolkit",
159 long_about = None
160)]
161pub struct CliCommand {
162 /// The subcommand to execute.
163 #[command(subcommand)]
164 pub command: Commands,
165}
166
167/// Create a clap Command for shell completion generation.
168///
169/// This function provides a convenient way to get the complete command structure
170/// for generating shell completions. It uses the shared `CliCommand` struct to
171/// ensure consistency with the actual CLI.
172///
173/// # Returns
174///
175/// A clap `Command` instance configured with all subcommands and arguments.
176///
177/// # Examples
178///
179/// ```no_run
180/// use hedl_cli::create_cli_command;
181/// use clap_complete::{generate, shells::Bash};
182/// use std::io;
183///
184/// let mut cmd = create_cli_command();
185/// generate(Bash, &mut cmd, "hedl", &mut io::stdout());
186/// ```
187#[must_use]
188pub fn create_cli_command() -> Command {
189 CliCommand::command()
190}