hedl_cli/commands/validate.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//! Validate command - HEDL file syntax and structure validation
19
20use super::read_file;
21use crate::error::CliError;
22use colored::Colorize;
23use hedl_core::{parse_with_limits, ParseOptions, ReferenceMode};
24
25/// Validate a HEDL file for syntax and structural correctness.
26///
27/// Parses a HEDL file and reports whether it is syntactically valid. In strict mode,
28/// all entity references must resolve to defined entities.
29///
30/// # Arguments
31///
32/// * `file` - Path to the HEDL file to validate
33/// * `strict` - If `true`, enables strict reference validation (all references must resolve)
34///
35/// # Returns
36///
37/// Returns `Ok(())` if the file is valid, `Err` with a descriptive error message otherwise.
38///
39/// # Errors
40///
41/// Returns `Err` if:
42/// - The file cannot be read
43/// - The file contains syntax errors
44/// - In strict mode, if any entity references cannot be resolved
45///
46/// # Examples
47///
48/// ```no_run
49/// use hedl_cli::commands::validate;
50///
51/// # fn main() -> Result<(), hedl_cli::error::CliError> {
52/// // Validate a well-formed HEDL file
53/// validate("valid.hedl", false)?;
54///
55/// // Strict validation requires all references to resolve
56/// validate("references.hedl", true)?;
57///
58/// // Invalid syntax will fail
59/// let result = validate("invalid.hedl", false);
60/// assert!(result.is_err());
61/// # Ok(())
62/// # }
63/// ```
64///
65/// # Output
66///
67/// Prints a summary to stdout including:
68/// - File validation status (✓ or ✗)
69/// - HEDL version
70/// - Count of structs, aliases, and nests
71/// - Strict mode indicator if enabled
72pub fn validate(file: &str, strict: bool) -> Result<(), CliError> {
73 let content = read_file(file)?;
74
75 // Configure parser options with strict mode
76 let options = ParseOptions {
77 reference_mode: if strict {
78 ReferenceMode::Strict
79 } else {
80 ReferenceMode::Lenient
81 },
82 ..ParseOptions::default()
83 };
84
85 match parse_with_limits(content.as_bytes(), options) {
86 Ok(doc) => {
87 println!("{} {}", "✓".green().bold(), file);
88 println!(" Version: {}.{}", doc.version.0, doc.version.1);
89 println!(" Structs: {}", doc.structs.len());
90 println!(" Aliases: {}", doc.aliases.len());
91 println!(" Nests: {}", doc.nests.len());
92 if strict {
93 println!(" Mode: strict (all references must resolve)");
94 }
95 Ok(())
96 }
97 Err(e) => {
98 println!("{} {}", "✗".red().bold(), file);
99 Err(CliError::parse(e.to_string()))
100 }
101 }
102}