lnmp-core
Core type definitions for LNMP (LLM Native Minimal Protocol) v0.4.
Overview
This crate provides the fundamental data structures for representing LNMP data:
FieldId- Type alias for field identifiers (u16, range 0-65535)LnmpValue- Enum representing all supported value types (including nested structures in v0.3)LnmpField- A field ID and value pairLnmpRecord- A collection of fields representing a complete recordSemanticChecksum- SC32 checksum computation for semantic fidelity (v0.3)TypeHint- Type annotations for explicit typing (including nested types in v0.3)
Usage
Add to your Cargo.toml:
[]
= { = "path/to/lnmp-core" }
Example
use ;
// Create a new record
let mut record = new;
// Add fields with different value types
record.add_field;
record.add_field;
record.add_field;
record.add_field;
// Access fields
if let Some = record.get_field
// Iterate over all fields
for field in record.fields
// Get field count
println!;
Types
LnmpValue
Represents all supported value types in LNMP v0.3:
v0.3 Nested Structure Support:
use ;
// Create a nested record
let mut inner_record = new;
inner_record.add_field;
let mut outer_record = new;
outer_record.add_field;
// Create a nested array
let mut record1 = new;
record1.add_field;
let mut record2 = new;
record2.add_field;
let mut parent = new;
parent.add_field;
LnmpField
A single field assignment consisting of a field ID and value:
LnmpRecord
A collection of fields representing a complete LNMP record:
TypeHint
Type annotations for explicit typing (v0.2+):
SemanticChecksum (v0.3)
Compute and validate semantic checksums (SC32) for drift prevention:
use ;
// Compute checksum
let checksum = compute;
// Validate checksum
let is_valid = validate;
// Format as hex string
let hex = format; // "36AAE667"
v0.3 Features
Nested Structures
Support for hierarchical data modeling:
- Nested Records:
F50={F12=1;F7=1}- Records within records - Nested Arrays:
F60=[{F1=1},{F1=2}]- Arrays of records - Arbitrary Depth: Limited only by available memory
- Structural Validation:
value.validate_structure()ensures integrity
Semantic Checksums (SC32)
32-bit checksums for preventing LLM input drift:
- Deterministic: Same value always produces same checksum
- Semantic: Based on FID + type hint + normalized value
- Optional: Can be enabled/disabled via configuration
- Fast: CRC32-based algorithm (<1μs per field)
Value Normalization
Canonical transformations for semantic equivalence:
- Booleans:
true/false/yes/no/1/0→1or0 - Floats:
-0.0→0.0,3.140→3.14 - Strings: Configurable case normalization
Features
- Zero dependencies - Pure Rust implementation (except CRC32 for checksums)
- Type safety - Strong typing for all value types
- Efficient storage - Fields stored in a Vec for cache-friendly access
- Flexible access - Get fields by ID or iterate over all fields
- Nested structures - Support for hierarchical data in text format (v0.3)
- Semantic checksums - Drift prevention with SC32 (v0.3)
- Binary format support - Compatible with v0.4 binary protocol (via lnmp-codec)
Binary Format Support (v0.4)
The core types are fully compatible with the v0.4 binary protocol format:
- All primitive types (Int, Float, Bool, String, StringArray) can be encoded to binary
- Binary encoding provides 30-50% size reduction compared to text format
- Round-trip conversion (text ↔ binary) maintains data integrity
- Note: Nested structures (NestedRecord, NestedArray) are not yet supported in v0.4 binary format
- Nested structures remain fully supported in text format
- Binary support for nested structures is planned for v0.5
For binary encoding/decoding, use the lnmp-codec crate:
use ;
use ;
let mut record = new;
record.add_field;
// Encode to binary
let encoder = new;
let binary = encoder.encode.unwrap;
// Decode from binary
let decoder = new;
let decoded_record = decoder.decode.unwrap;
Migration from v0.2
v0.3 is backward compatible with v0.2. New features:
LnmpValue::NestedRecordandLnmpValue::NestedArrayvariantsTypeHint::RecordandTypeHint::RecordArrayvariantsSemanticChecksummodule for checksum computationdepth()andvalidate_structure()methods onLnmpValue
v0.4 adds binary protocol support (via lnmp-codec) with no changes to core types.
Existing v0.2 code continues to work without changes.
License
MIT OR Apache-2.0