hedl 2.0.0

HEDL - Hierarchical Entity Data Language
Documentation
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// 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.

//! # HEDL - Hierarchical Entity Data Language
//!
//! HEDL is a text-based data serialization format optimized for AI/ML data representation.
//! It combines the token efficiency of CSV-style tables with the semantic richness of
//! hierarchical structures.
//!
//! ## Quick Start
//!
//! ```rust
//! use hedl::{parse, canonicalize, to_json};
//!
//! let hedl_doc = r#"
//! %V:2.0
//! %NULL:~
//! %QUOTE:"
//! %S:User:[id,name,email]
//! ---
//! users: @User
//!  |alice, Alice, alice@example.com
//!  |bob, Bob, bob@example.com
//! "#;
//!
//! // Parse the document
//! let doc = parse(hedl_doc).expect("Failed to parse");
//!
//! // Convert to canonical form
//! let canonical = canonicalize(&doc).expect("Failed to canonicalize");
//!
//! // Convert to JSON
//! let json = to_json(&doc).expect("Failed to convert to JSON");
//! ```
//!
//! ## Features
//!
//! - **Type-scoped IDs**: IDs are unique within their type namespace
//! - **Matrix lists**: CSV-like tables for homogeneous collections
//! - **References**: `@id` or `@Type:id` for graph relationships
//! - **Tensor literals**: `[1, 2, 3]` for numerical arrays
//! - **Expressions**: `$(...)` for deferred computation
//! - **Aliases**: `%key` for constant substitution
//!
//! ## Modules
//!
//! - [`core`]: Core parsing and data model
//! - [`lex`]: Lexical analysis utilities
//! - [`csv`]: CSV field parsing (internal row parsing)
//! - [`tensor`]: Tensor literal parsing
//! - [`c14n`](mod@c14n): Canonicalization
//! - [`json`]: JSON conversion
//! - [`lint`](mod@lint): Linting and best practices
//!
//! ### Optional Format Converters (feature-gated)
//!
//! - `yaml`: YAML conversion (feature = "yaml")
//! - `xml`: XML conversion (feature = "xml")
//! - `csv_file`: CSV file conversion (feature = "csv")
//! - `parquet`: Parquet conversion (feature = "parquet")

// Re-export core types
#![cfg_attr(not(test), warn(missing_docs))]
pub use hedl_core::{
    // Functions
    parse as core_parse,
    parse_with_limits,
    // Main types
    Document,
    // Errors
    HedlError,
    HedlErrorKind,
    Item,
    // Parser
    Limits,
    MatrixList,
    Node,
    ParseOptions,
    Reference,
    ReferenceMode,
    // Tensor type
    Tensor,
    Value,
};

// Error handling extensions
mod error_ext;
pub use error_ext::HedlResultExt;

/// Lexical analysis utilities.
pub mod lex {
    pub use hedl_core::lex::{
        is_valid_id_token, is_valid_key_token, is_valid_type_name, parse_reference, scan_regions,
        strip_comment, validate_indent, IndentInfo, LexError, Reference, Region,
    };
}

/// CSV field parsing.
pub mod csv {
    pub use hedl_core::lex::{parse_csv_row, CsvField};
}

/// Tensor literal parsing.
pub mod tensor {
    pub use hedl_core::lex::{parse_tensor, Tensor};
}

/// Canonicalization utilities.
pub mod c14n {
    pub use hedl_c14n::{
        canonicalize, canonicalize_with_config, CanonicalConfig, CanonicalWriter, QuotingStrategy,
    };
}

/// JSON conversion utilities.
pub mod json {
    pub use hedl_json::{
        from_json, from_json_value, hedl_to_json, json_to_hedl, to_json, to_json_value,
        FromJsonConfig, ToJsonConfig,
    };
}

/// Linting utilities.
pub mod lint {
    pub use hedl_lint::{
        lint, lint_with_config, Diagnostic, DiagnosticKind, LintConfig, LintRule, LintRunner,
        RuleConfig, Severity,
    };
}

// Optional format converters

/// YAML conversion utilities (requires `yaml` feature)
#[cfg(feature = "yaml")]
pub mod yaml {
    pub use hedl_yaml::{
        from_yaml, hedl_to_yaml, to_yaml, yaml_to_hedl, FromYamlConfig, ToYamlConfig,
    };
}

/// XML conversion utilities (requires `xml` feature)
#[cfg(feature = "xml")]
pub mod xml {
    pub use hedl_xml::{from_xml, hedl_to_xml, to_xml, xml_to_hedl, FromXmlConfig, ToXmlConfig};
}

/// CSV file conversion utilities (requires `csv` feature).
/// Distinct from internal row parsing.
#[cfg(feature = "csv")]
pub mod csv_file {
    pub use hedl_csv::{
        from_csv, from_csv_with_config, to_csv, to_csv_with_config, FromCsvConfig, ToCsvConfig,
    };
}

/// Parquet conversion utilities (requires `parquet` feature)
#[cfg(feature = "parquet")]
pub mod parquet {
    pub use hedl_parquet::{
        from_parquet, from_parquet_bytes, to_parquet, to_parquet_bytes, ToParquetConfig,
    };
}

/// Neo4j/Cypher conversion utilities (requires `neo4j` feature).
/// Provides bidirectional conversion between HEDL documents and Neo4j graph databases.
#[cfg(feature = "neo4j")]
pub mod neo4j {
    pub use hedl_neo4j::{
        build_record,
        build_relationship,
        // Core import functions
        from_neo4j_records,
        hedl_to_cypher,
        neo4j_to_hedl,
        // Core export functions
        to_cypher,
        to_cypher_statements,
        CypherScript,
        CypherStatement,
        CypherValue,
        FromNeo4jConfig,
        // Errors
        Neo4jError,
        Neo4jNode,
        // Types
        Neo4jRecord,
        Neo4jRelationship,
        ObjectHandling,
        RelationshipNaming,
        Result as Neo4jResult,
        StatementType,
        // Configuration
        ToCypherConfig,
    };
}

/// TOON conversion utilities (requires `toon` feature).
#[cfg(feature = "toon")]
pub mod toon {
    pub use hedl_toon::{
        hedl_to_toon, to_toon, Delimiter, ToToonConfig, ToToonConfigBuilder, ToonError,
    };
}

// Convenience functions at crate root

/// Parse a HEDL document from a string.
///
/// Uses strict mode by default. For lenient parsing, use [`parse_lenient`].
///
/// # Performance
///
/// This is a hot path function with `#[inline]` hint for 5-10% improvement
/// in small document parsing scenarios.
///
/// # Examples
///
/// ```rust
/// use hedl::parse;
///
/// // Parsing v1.0 input preserves the version
/// let doc = parse("%VERSION: 1.0\n---\nkey: value").unwrap();
/// assert_eq!(doc.version, (1, 0));
/// ```
#[inline]
pub fn parse(input: &str) -> Result<Document, HedlError> {
    core_parse(input.as_bytes())
}

/// Parse a HEDL document with lenient reference handling.
///
/// Unresolved references become `null` instead of causing errors.
#[inline]
pub fn parse_lenient(input: &str) -> Result<Document, HedlError> {
    let options = ParseOptions {
        reference_mode: hedl_core::ReferenceMode::Lenient,
        ..Default::default()
    };
    parse_with_limits(input.as_bytes(), options)
}

/// Canonicalize a HEDL document to a string.
///
/// Produces deterministic output suitable for hashing and diffing.
///
/// # Performance
///
/// This is a hot path function with `#[inline]` hint for 5-10% improvement
/// in serialization benchmarks.
///
/// # Examples
///
/// ```rust
/// use hedl::{parse, canonicalize};
///
/// let doc = parse("%VERSION: 1.0\n---\nb: 2\na: 1").unwrap();
/// let canonical = canonicalize(&doc).unwrap();
/// // Keys are sorted alphabetically in canonical form
/// assert!(canonical.contains("a: 1"));
/// ```
#[inline]
pub fn canonicalize(doc: &Document) -> Result<String, HedlError> {
    hedl_c14n::canonicalize(doc)
}

/// Convert a HEDL document to JSON.
///
/// # Performance
///
/// This is a hot path function with `#[inline]` hint for 5-10% improvement
/// in format conversion benchmarks.
///
/// # Examples
///
/// ```rust
/// use hedl::{parse, to_json};
///
/// let doc = parse("%VERSION: 1.0\n---\nkey: 42").unwrap();
/// let json = to_json(&doc).unwrap();
/// assert!(json.contains("\"key\": 42"));
/// ```
#[inline]
pub fn to_json(doc: &Document) -> Result<String, HedlError> {
    hedl_json::to_json(doc, &hedl_json::ToJsonConfig::default())
        .map_err(|e| HedlError::syntax(format!("JSON conversion error: {e}"), 0))
}

/// Convert JSON to a HEDL document.
///
/// # Examples
///
/// ```rust
/// use hedl::from_json;
///
/// let json = r#"{"key": "value"}"#;
/// let doc = from_json(json).unwrap();
/// ```
#[inline]
pub fn from_json(json: &str) -> Result<Document, HedlError> {
    hedl_json::from_json(json, &hedl_json::FromJsonConfig::default())
        .map_err(|e| HedlError::syntax(format!("JSON conversion error: {e}"), 0))
}

/// Lint a HEDL document for best practices.
///
/// # Examples
///
/// ```rust
/// use hedl::{parse, lint};
///
/// let doc = parse("%VERSION: 1.0\n---\nkey: value").unwrap();
/// let diagnostics = lint(&doc);
/// for d in diagnostics {
///     println!("{}", d);
/// }
/// ```
#[inline]
#[must_use]
pub fn lint(doc: &Document) -> Vec<lint::Diagnostic> {
    hedl_lint::lint(doc)
}

/// Validate a HEDL string without fully parsing.
///
/// Returns `Ok(())` if valid, `Err` with details if invalid.
#[inline]
pub fn validate(input: &str) -> Result<(), HedlError> {
    parse(input).map(|_| ())
}

/// HEDL format version supported by this library.
pub const SUPPORTED_VERSION: (u32, u32) = (2, 0);

/// Library version.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_minimal() {
        // Parsing v1.0 input should preserve the version
        let doc = parse("%VERSION: 1.0\n---\n").unwrap();
        assert_eq!(doc.version, (1, 0));
    }

    #[test]
    fn test_parse_key_value() {
        // Parsing v1.0 input should preserve the version
        let doc = parse("%VERSION: 1.0\n---\nkey: value\nnum: 42").unwrap();
        assert_eq!(doc.version, (1, 0));
    }

    #[test]
    fn test_parse_matrix_list() {
        let input = r"
%VERSION: 1.0
%STRUCT: User: [id,name]
---
users:@User
 |alice,Alice
 |bob,Bob
";
        let doc = parse(input).unwrap();
        assert!(doc.structs.contains_key("User"));
    }

    #[test]
    fn test_canonicalize() {
        let doc = parse("%VERSION: 1.0\n---\nb: 2\na: 1").unwrap();
        let canonical = canonicalize(&doc).unwrap();
        // Canonical form has sorted keys
        let a_pos = canonical.find("a:").unwrap();
        let b_pos = canonical.find("b:").unwrap();
        assert!(a_pos < b_pos);
    }

    #[test]
    fn test_to_json() {
        let doc = parse("%VERSION: 1.0\n---\nkey: 42").unwrap();
        let json = to_json(&doc).unwrap();
        assert!(json.contains("42"));
    }

    #[test]
    fn test_validate() {
        assert!(validate("%VERSION: 1.0\n---\n").is_ok());
        assert!(validate("invalid").is_err());
    }
}