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