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.
Struct Deserialization
Cirru EDN supports Struct values with symbol names, which can be deserialized to Rust structs. During deserialization, the struct name is ignored since Rust structs don't expose their type names at runtime:
use ;
use Arc;
// Create a Struct with a symbol name
let person_struct = Struct;
// Deserialize Struct to Rust struct (ignoring the struct name)
let person: Person = from_edn.unwrap;
println!;
// Note: When serializing structs back to EDN, they become Maps, not EDN Struct values
// since Rust doesn't provide struct names at runtime
let edn_back = to_edn.unwrap;
// This will be a Map, not an EDN Struct
This feature allows interoperability between EDN data containing Struct values and Rust structs, with the semantic understanding that struct 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
- Struct names are ignored during deserialization and Rust structs serialize to Maps, not EDN Struct values
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
enum, or tagged union, is limited by Calcit semantics:
:: 'a
:: 'b 1
extra values can be added to an enum:
:: 'a 1 |extra :l
%:: represents enums with a symbol type name:
%:: 'e 'a 1 |extra :l
an EDN struct uses a symbol name and tag field keys:
%{} '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 syntax-error reporting with source 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
Structure and value errors include a nested Cirru path in @1.2.3 notation; for example,
@1.2.3.4 means indices 1, 2, 3, and 4 from the root node. Syntax errors include
line/column and byte-offset information when provided by the parser.
See ERROR_HANDLING.md for detailed documentation and examples.
Run the error demo:
License
MIT