MAINTENANCE WARNING
Development is currently halted for this project. No new features are under development, but also no breaking changes. If you have a critical issue, we might take a look.
You may find Grinkers/clojure-reader as a suitable alternative as its actively maintained, it has been built by one of our maintainers @Grinkers.
edn-rs
A crate to parse and emit EDN (Extensible Data Notation)
- MSRV (minimal supported rust version) latest.
- Current library maintainer is reovate bot
Full integration examples:
Usage
Default
Includes features std and sets.
[]
= "0.17.4"
no_std
To use edn-rs without any additional dependencies, disable default features.
edn-rs still relies on alloc. In no_std environments, you must supply your own #[global_allocator].
[]
= { = "0.17.4", = false }
Optional features
std: Implements (de)serialization for Hashmap and HashSet; Also some floating point functionality.sets: Implements (de)serialization for EDN sets. Depends onordered-float.json: Implements json->edn and edn->json conversions. Depends onregex.
Quick reference
Parse an EDN token into a Edn with edn! macro:
use ;
Parse an EDN String with Edn::from_str:
use ;
use FromStr;
To navigate through Edn data you can just use get and get_mut:
use ;
Serializes Rust Types into EDN with edn-derive::Serialize
use ;
use ;
use Serialize;
Deserializes Strings into Rust Types:
For now you have to implement the conversion yourself with the
Deserializetrait. Soon you'll be able to have that implemented for you viaedn-derivecrate.
use ;
Deserializes Edn types into Rust Types:
- Deserialization to
std::collection::*is currently unsafe.
For now you have to implement the conversion yourself with the
Deserializetrait. Soon you'll be able to have that implemented for you viaedn-derivecrate.
use ;
Emits EDN format from a Json:
- This function requires feature
jsonto be activated. To enable this feature add to yourCargo.tomldependencies the following lineedn-rs = { version = 0.17.4", features = ["json"] }.
use json_to_edn;
Emits a JSON from type edn_rs::Edn.
- The associated emthod is
to_json(&self)and it requires featurejsonto be activated. To enable this feature add to yourCargo.tomldependencies the following lineedn-rs = { version = 0.17.4", features = ["json"] }.
use FromStr;
to_string/to_debug
to_debug emits a Debug version of Edn type.
use ;
let edn = Vector;
let expected = "Vector(Vector([Int(5), Int(6), Int(7)]))";
assert_eq!;
to_string emits a valid edn.
use ;
let edn = Vector;
let expected = "[5, 6, 7, ]";
assert_eq!;
Larger to_string example:
Edn-rs Current Features
- Define
structto map EDN infoEdnNode - Define EDN types,
EdnType- Edn Type into primitive:
Edn::Bool(true).into() -> true. This was done byto_float,to_bool,to_int,to_vec. - implement
futures::Futuretrait toEdn -
to_string()forEdn. -
to_debug()forEdn.
- Edn Type into primitive:
- Parse EDN data
from_str:- nil
"" - String
"\"string\"" - Numbers
"324352","3442.234","3/4" - Keywords
:a - Symbol
sym-bol-s - Vector
"[1 :2 \"d\"]" - List
"(1 :2 \"d\")" - Set
"#{1 2 3}" - Map
"{:a 1 :b 2 }" - Tag
#inst \"yyyy-mm-ddTHH:MM:ss\",#uuid \"<some-uuid>\"as string data (no custom reader support) - Nested structures
"{:a \"2\" :b [true false] :c #{:A {:a :b} nil}}"
- nil
- Simple data structures in one another
edn!:- Vec in Vec
"[1 2 [:3 \"4\"]]" - Set in Vec
"[1 2 #{:3 \"4\"}]" - List in List
"(1 2 (:3 \"4\"))" - List in Set
"'#{1 2 (:3 \"4\")}" - Maps in general
"{:a 2 :b {:3 \"4\"}}","{:a 2 :b [:3 \"4\"]}"
- Vec in Vec
- Multiple simple data structures in one another (Map and Set in a vector)
- Multi deepen data structures (Map in a Set in a List in a Vec in a Vec)
- Navigate through Edn Data
- Navigate through Sets. DOne by
set_iter
- Navigate through Sets. DOne by
- Json to Edn
- Json String to EDN String
- macro to process Structs and Enums to EDN
- trait Deserialize EDN to Struct
- trait Serialize struct to EDN
edn-derive
edn-derive is a proc-macro crate to (De)serialize Edn values, currently it is beta and it can be found at crates.io or at github.
Usage
Just add to your Cargo.toml the following:
[]
= "<version>"
= "0.17.4"
Examples
Serialize
use Serialize;
Deserialization
use Deserialize;
use EdnError;
// The `Debug` and `PartialEq` are only necessary because of `assert_eq`, you don't need them
Current Features
-
derive Serialize -
edn_rs::to_string -
derive Deserialize -
let val: YourStruct = edn_rs::from_str(&str)