edn-rs
Near Stable no breaking changes expected.
- This lib does not make effort to conform the EDN received to EDN Spec. The lib that generated this EDN should be responsible for this. For more information on Edn Spec please visit: https://github.com/edn-format/edn.
Current example usage in:
Usage
Cargo.toml
[]
= "0.16.2"
Simple time-only benchmarks of edn-rs agains Clojure Edn
- Link to benchmarks implementation here
| Method\Lang | Rust --release | Rust --debug | Clojure |
|---|---|---|---|
| parse string | 77.57µs | 266.479µs | 4.712235 milis |
| get-in/navigate (3 blocks) | 4.224µs | 22.861µs | 26.333 µs |
| Deserialize to struct | 110.358µs | 357.054µs | 4.712235 milis |
| parse with criterium | 11.348µs | - | 23.230µs |
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 ser_struct! or use edn-derive::Serialize
use ;
use ;
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:
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.16.2", features = ["json"] }.
use json_to_edn;
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:
Using async/await with Edn type
Edn supports futures by using the feature async. To enable this feature add to your Cargo.toml dependencies the following line edn-rs = { version = 0.16.2", features = ["async"] } and you can use futures as in the following example.
use ;
use *;
use Future;
use *;
async + Send
async
The objective of foo is to show that Edn can be wrapped with a Future. If you want to return an Edn from an async function just use:
async
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 }" - Inst
#inst \"yyyy-mm-ddTHH:MM:ss\" - 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 pre-alpha and it can be found at crates.io or at github.
Usage
Just add to your Cargo.toml the following:
[]
= "<version>"
= "0.16.2"
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)