Cirru Edn in Rust
Extensible data notations based on Cirru syntax
Usages
Basic parsing and formatting:
use Edn;
parse; // Result<Edn, String>
format; // Result<String, String>.
Serde Integration
Cirru EDN provides seamless integration with serde, allowing you to easily convert between Rust structs and EDN data with efficient direct serialization and deserialization.
Key Type Distinction
An important feature of this implementation is the semantic distinction between struct fields and map keys:
- Struct fields use
Tag(:field_name) - representing named constants and structured identifiers - Map keys use
String("key") - representing arbitrary string data
This design preserves the intended meaning of different data elements in EDN format.
Basic Usage
use ;
use ;
use HashMap;
let person = Person ;
// Convert struct to Edn
let edn_value = to_edn.unwrap;
println!;
// Output: {:name "Alice", :age 30, :email "alice@example.com", :tags ["developer", "rust"], :metadata {"role" "senior"}}
// ^^^^^ Tag ^^^^^^ String
// Convert Edn back to struct
let reconstructed: Person = from_edn.unwrap;
assert_eq!;
Supported Data Types
- Primitive types:
bool,i32,i64,u32,u64,f32,f64,String - Container types:
Vec<T>,HashMap<K, V>,HashSet<T> - Optional types:
Option<T>(maps toEdn::Nilor the actual value) - Nested structures: Arbitrarily deep nested structs
Manual Edn Construction
You can also manually construct Edn data and then deserialize it to structs. Remember to use Tags for struct field keys:
use ;
use HashMap;
// Construct EDN manually with proper key types
let mut map = new;
map.insert; // Tag for struct field
map.insert; // Tag for struct field
map.insert; // Tag for struct field
map.insert;
// For metadata HashMap, use String keys
let mut metadata_map = new;
metadata_map.insert; // String for map key
map.insert;
let edn_data = Map;
let person: Person = from_edn.unwrap;
println!;
Error Handling
When deserialization fails (e.g., missing required fields or type mismatches), descriptive error messages are returned:
let incomplete_edn = map_from_iter;
match
Complex Examples
See examples/serde_demo.rs for more complex nested structures and usage patterns.
Record Deserialization
Cirru EDN supports Record types with named tags, which can be deserialized to Rust structs. During deserialization, the record name is ignored since Rust structs don't expose their type names at runtime:
use ;
// Create a Record with a named type
let person_record = Record;
// Deserialize Record to struct (ignoring the record name)
let person: Person = from_edn.unwrap;
println!;
// Note: When serializing structs back to EDN, they become Maps, not Records
// since Rust doesn't provide struct names at runtime
let edn_back = to_edn.unwrap;
// This will be a Map, not a Record
This feature allows interoperability between EDN data containing Records and Rust structs, with the semantic understanding that record names are metadata that may be lost during round-trip conversion.
Limitations
- Some special Edn types (like
Quote,AnyRef) cannot be serialized - Maps with complex keys will use their string representation when serializing structs
- Record names are ignored during deserialization and structs serialize to Maps, not Records
EDN Format
mixed data:
{} (:a 1.0)
:b $ [] 2.0 3.0 4.0
:c $ {} (:d 4.0)
:e true
:f :g
:h $ {} (|a 1.0)
|b true
{}
:b $ [] 2 3 4
:a 1
:c $ {}
:h $ {} (|b true) (|a 1)
:f :g
:e true
:d 4
for top-level literals, need to use do expression:
do nil
do true
do false
do 1
do -1.1
quoted code:
do 'a
quote (a b)
tags(previously called "keyword")
do :a
string syntax, note it's using prefixed syntax of |:
do |a
string with special characters:
do \"|a b\"
nested list:
[] 1 2 $ [] 3
#{} ([] 3) 1
tuple, or tagged union, actually very limitted due to Calcit semantics:
:: :a
:: :b 1
extra values can be added to tuple since 0.3:
:: :a 1 |extra :l
a record, notice that now it's all using tags:
%{} :Demo (:a 1)
:b 2
:c $ [] 1 2 3
extra format for holding buffer, which is internally Vec<u8>:
buf 00 01 f1 11
atom, which translates to a reference to a value:
atom 1
Error Handling (v0.7.0+)
With cirru_parser 0.2.0, Cirru EDN provides enhanced error reporting with position information:
use ;
match parse
Error types include:
ParseError- Syntax errors from the parserStructureError- Invalid EDN structureValueError- Invalid values (e.g., bad hex, invalid tokens)DeserializationError- Serde deserialization errors
See ERROR_HANDLING.md for detailed documentation and examples.
Run the error demo:
License
MIT