1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Dweve HEDL - Hierarchical Entity Data Language
//
// Copyright (c) 2025 Dweve IP B.V. and individual contributors.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE file at the
// root of this repository or at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! CLI command implementations
pub use ;
pub use ;
pub use ;
pub use format;
pub use inspect;
pub use lint;
pub use stats;
pub use validate;
use crateCliError;
use fs;
use ;
use PathBuf;
/// Default maximum file size to prevent OOM attacks (1 GB)
/// Can be overridden via `HEDL_MAX_FILE_SIZE` environment variable
pub const DEFAULT_MAX_FILE_SIZE: u64 = 1024 * 1024 * 1024;
/// Get the maximum file size from environment or use default.
///
/// Reads the `HEDL_MAX_FILE_SIZE` environment variable to allow customization
/// of the maximum allowed file size. Falls back to [`DEFAULT_MAX_FILE_SIZE`] if
/// the variable is not set or contains an invalid value.
///
/// # Examples
///
/// ```
/// use hedl_cli::commands::DEFAULT_MAX_FILE_SIZE;
///
/// // Default behavior
/// std::env::remove_var("HEDL_MAX_FILE_SIZE");
/// // Note: get_max_file_size is private, so this example shows the concept
/// // let size = get_max_file_size();
/// // assert_eq!(size, DEFAULT_MAX_FILE_SIZE);
///
/// // Custom size via environment variable
/// std::env::set_var("HEDL_MAX_FILE_SIZE", "500000000"); // 500 MB
/// // let size = get_max_file_size();
/// // assert_eq!(size, 500_000_000);
/// ```
/// Read a file from disk with size validation.
///
/// Reads the entire contents of a file into a string, with built-in protection
/// against out-of-memory (OOM) attacks. Files larger than the configured maximum
/// size will be rejected before reading.
///
/// # Arguments
///
/// * `path` - Path to the file to read
///
/// # Returns
///
/// Returns the file contents as a `String` on success.
///
/// # Errors
///
/// Returns `Err` if:
/// - The file metadata cannot be accessed
/// - The file size exceeds the maximum allowed size (configurable via `HEDL_MAX_FILE_SIZE`)
/// - The file cannot be read
/// - The file contains invalid UTF-8
///
/// # Examples
///
/// ```no_run
/// use hedl_cli::commands::read_file;
///
/// # fn main() -> Result<(), hedl_cli::error::CliError> {
/// // Read a small HEDL file
/// let content = read_file("example.hedl")?;
/// assert!(!content.is_empty());
///
/// // Files larger than the limit will fail
/// std::env::set_var("HEDL_MAX_FILE_SIZE", "1000"); // 1 KB limit
/// let result = read_file("large_file.hedl");
/// assert!(result.is_err());
/// # Ok(())
/// # }
/// ```
///
/// # Security
///
/// This function includes protection against OOM attacks by checking the file
/// size before reading. The maximum file size defaults to 1 GB but can be
/// customized via the `HEDL_MAX_FILE_SIZE` environment variable.
///
/// # Performance
///
/// Uses `fs::metadata()` to check file size before allocating memory, preventing
/// unnecessary memory allocation for oversized files.
/// Write content to a file or stdout.
///
/// Writes the provided content to either a specified file path or to stdout
/// if no path is provided. This is the standard output mechanism used by
/// all HEDL CLI commands.
///
/// # Arguments
///
/// * `content` - The string content to write
/// * `path` - Optional output file path. If `None`, writes to stdout
///
/// # Returns
///
/// Returns `Ok(())` on success.
///
/// # Errors
///
/// Returns `Err` if:
/// - File creation or writing fails (when `path` is `Some`)
/// - Writing to stdout fails (when `path` is `None`)
///
/// # Examples
///
/// ```no_run
/// use hedl_cli::commands::write_output;
///
/// # fn main() -> Result<(), hedl_cli::error::CliError> {
/// // Write to stdout
/// let hedl_content = "%VERSION: 1.0\n---\nteams:@Team[name]\n |t1,Team A\n |t2,Team B";
/// write_output(hedl_content, None)?;
///
/// // Write to file
/// write_output(hedl_content, Some("output.hedl"))?;
/// # Ok(())
/// # }
/// ```
///
/// # Panics
///
/// Does not panic under normal circumstances. All I/O errors are returned
/// as `Err` values.