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 (strict=false, lenient=false)
71//! validate("example.hedl", false, false)?;
72//!
73//! // Strict validation (all references must resolve)
74//! validate("example.hedl", true, false)?;
75//!
76//! // Lenient validation (allow constraint violations)
77//! validate("example.hedl", false, true)?;
78//! # Ok(())
79//! # }
80//! ```
81//!
82//! ## Format Conversion
83//!
84//! ```no_run
85//! use hedl_cli::commands::{to_json, from_json};
86//!
87//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
88//! // Convert HEDL to pretty JSON
89//! to_json("data.hedl", Some("output.json"), false, true)?;
90//!
91//! // Convert JSON back to HEDL
92//! from_json("data.json", Some("output.hedl"))?;
93//! # Ok(())
94//! # }
95//! ```
96//!
97//! ## Batch Processing
98//!
99//! ```no_run
100//! use hedl_cli::commands::batch_validate;
101//!
102//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
103//! // Validate multiple files in parallel
104//! // Args: patterns, strict, recursive, max_depth, parallel, verbose
105//! let files = vec!["file1.hedl".to_string(), "file2.hedl".to_string()];
106//! batch_validate(files, false, true, 10, true, false)?;
107//! # Ok(())
108//! # }
109//! ```
110//!
111//! # Security
112//!
113//! The CLI includes several security features:
114//!
115//! - **File size limits**: Prevents OOM attacks (configurable via `HEDL_MAX_FILE_SIZE`)
116//! - **Input validation**: Type names and parameters are validated
117//! - **Safe conversions**: All format conversions use safe, well-tested libraries
118//!
119//! # Performance
120//!
121//! - **Parallel processing**: Batch commands automatically use parallel processing
122//! - **Optimized stats**: Format conversions run in parallel (3-5x speedup)
123//! - **Memory efficiency**: Streaming where possible, size checks before reading
124//!
125//! # Error Handling
126//!
127//! All commands return `Result<(), String>` for consistent error handling.
128//! Errors are descriptive and include context like file paths and line numbers
129//! where applicable.
130
131#![cfg_attr(not(test), warn(missing_docs))]
132/// Batch processing for multiple HEDL files with parallel execution and progress reporting.
133pub mod batch;
134/// CLI command definitions and argument parsing.
135pub mod cli;
136/// CLI command implementations.
137pub mod commands;
138/// Structured error types for the HEDL CLI.
139pub mod error;
140/// File discovery with glob patterns and recursive traversal.
141pub mod file_discovery;
142
143use clap::{Command, CommandFactory, Parser};
144use cli::Commands;
145
146/// Shared CLI command structure for HEDL.
147///
148/// This struct provides a single source of truth for the CLI command structure,
149/// used by both the main binary and shell completion generation. It implements
150/// `CommandFactory` to allow programmatic access to the clap `Command` structure.
151///
152/// # Examples
153///
154/// ```no_run
155/// use clap::CommandFactory;
156/// use hedl_cli::CliCommand;
157///
158/// // Get the command for completion generation
159/// let mut cmd = CliCommand::command();
160/// ```
161#[derive(Parser)]
162#[command(name = "hedl")]
163#[command(
164 author,
165 version,
166 about = "HEDL - Hierarchical Entity Data Language toolkit",
167 long_about = None
168)]
169pub struct CliCommand {
170 /// The subcommand to execute.
171 #[command(subcommand)]
172 pub command: Commands,
173}
174
175/// Create a clap Command for shell completion generation.
176///
177/// This function provides a convenient way to get the complete command structure
178/// for generating shell completions. It uses the shared `CliCommand` struct to
179/// ensure consistency with the actual CLI.
180///
181/// # Returns
182///
183/// A clap `Command` instance configured with all subcommands and arguments.
184///
185/// # Examples
186///
187/// ```no_run
188/// use hedl_cli::create_cli_command;
189/// use clap_complete::{generate, shells::Bash};
190/// use std::io;
191///
192/// let mut cmd = create_cli_command();
193/// generate(Bash, &mut cmd, "hedl", &mut io::stdout());
194/// ```
195#[must_use]
196pub fn create_cli_command() -> Command {
197 CliCommand::command()
198}