hedl_cli/cli/
batch.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//! Batch processing commands for HEDL.
19//!
20//! This module provides commands for processing multiple HEDL files in parallel,
21//! enabling efficient bulk operations on large collections of files.
22
23use crate::commands;
24use clap::Subcommand;
25
26/// Batch processing commands.
27///
28/// These commands operate on multiple HEDL files simultaneously, with automatic
29/// parallelization for improved performance. All batch commands support glob
30/// patterns for file selection.
31///
32/// # Performance
33///
34/// Batch commands automatically use parallel processing when beneficial:
35/// - CPU-bound operations scale with available cores
36/// - I/O-bound operations use async parallelization
37/// - Progress reporting shows real-time status
38///
39/// # Design
40///
41/// All batch commands follow consistent patterns:
42/// - Multiple file inputs (with glob support)
43/// - Optional parallel processing flag
44/// - Verbose mode for detailed progress
45#[derive(Subcommand)]
46pub enum BatchCommands {
47    /// Batch validate multiple HEDL files
48    ///
49    /// Validates multiple HEDL files in parallel. Supports glob patterns for
50    /// file selection and provides aggregated results.
51    BatchValidate {
52        /// Input file paths (supports glob patterns)
53        #[arg(value_name = "FILES", num_args = 1..)]
54        files: Vec<String>,
55
56        /// Strict mode (fail on any error)
57        #[arg(short, long)]
58        strict: bool,
59
60        /// Force parallel processing
61        #[arg(short, long)]
62        parallel: bool,
63
64        /// Show verbose progress
65        #[arg(short, long)]
66        verbose: bool,
67    },
68
69    /// Batch format multiple HEDL files
70    ///
71    /// Formats multiple HEDL files to canonical form in parallel. Can either
72    /// modify files in-place or write to an output directory.
73    BatchFormat {
74        /// Input file paths (supports glob patterns)
75        #[arg(value_name = "FILES", num_args = 1..)]
76        files: Vec<String>,
77
78        /// Output directory for formatted files
79        #[arg(short, long)]
80        output_dir: Option<String>,
81
82        /// Check only (exit 1 if not canonical)
83        #[arg(short, long)]
84        check: bool,
85
86        /// Use ditto optimization
87        #[arg(long, default_value = "true")]
88        ditto: bool,
89
90        /// Automatically add count hints to all matrix lists
91        #[arg(long)]
92        with_counts: bool,
93
94        /// Force parallel processing
95        #[arg(short, long)]
96        parallel: bool,
97
98        /// Show verbose progress
99        #[arg(short, long)]
100        verbose: bool,
101    },
102
103    /// Batch lint multiple HEDL files
104    ///
105    /// Lints multiple HEDL files in parallel, checking for best practices
106    /// and style issues. Provides aggregated results across all files.
107    BatchLint {
108        /// Input file paths (supports glob patterns)
109        #[arg(value_name = "FILES", num_args = 1..)]
110        files: Vec<String>,
111
112        /// Treat warnings as errors
113        #[arg(short = 'W', long)]
114        warn_error: bool,
115
116        /// Force parallel processing
117        #[arg(short, long)]
118        parallel: bool,
119
120        /// Show verbose progress
121        #[arg(short, long)]
122        verbose: bool,
123    },
124}
125
126impl BatchCommands {
127    /// Execute the batch command.
128    ///
129    /// # Returns
130    ///
131    /// Returns `Ok(())` on success, or an error message on failure.
132    ///
133    /// # Errors
134    ///
135    /// Returns `Err` if:
136    /// - No files match the provided patterns
137    /// - Any file operation fails
138    /// - Processing fails for any file
139    ///
140    /// # Performance
141    ///
142    /// Batch commands automatically parallelize when beneficial. The `parallel`
143    /// flag forces parallelization even for small file sets.
144    pub fn execute(self) -> Result<(), String> {
145        match self {
146            BatchCommands::BatchValidate {
147                files,
148                strict,
149                parallel,
150                verbose,
151            } => commands::batch_validate(files, strict, parallel, verbose),
152            BatchCommands::BatchFormat {
153                files,
154                output_dir,
155                check,
156                ditto,
157                with_counts,
158                parallel,
159                verbose,
160            } => commands::batch_format(
161                files,
162                output_dir,
163                check,
164                ditto,
165                with_counts,
166                parallel,
167                verbose,
168            ),
169            BatchCommands::BatchLint {
170                files,
171                warn_error,
172                parallel,
173                verbose,
174            } => commands::batch_lint(files, warn_error, parallel, verbose),
175        }
176    }
177}