Skip to content

Latest commit

 

History

History
119 lines (103 loc) · 3.59 KB

README.md

File metadata and controls

119 lines (103 loc) · 3.59 KB

edn-rs

Crate to parse and emit EDN Build Status

  • 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.
  • Current example usage in crate https://crates.io/crates/transistor

Usage

Cargo.toml

[dependencies]
edn-rs = "0.5.3"

Parse an EDN into a Edn with edn! macro:

#![recursion_limit="512"]
#[macro_use]
extern crate edn_rs;

fn main() {
    let edn = edn!((sym 1.2 3 false :f nil 3/4));
    let expected = Edn::List(
        List::new(
            vec![
                Edn::Symbol("sym".to_string()),
                Edn::Double(1.2),
                Edn::Int(3),
                Edn::Bool(false),
                Edn::Key("f".to_string()),
                Edn::Nil,
                Edn::Rational("3/4".to_string())
            ]
        )
    );

    assert_eq!(edn, expected);
}

To navigate through Edn data you can just use get and get_mut:

let edn = edn!([ 1 1.2 3 {false :f nil 3/4}]);

assert_eq!(edn[1], edn!(1.2));
assert_eq!(edn[1], Edn::Double(1.2f64));
assert_eq!(edn[3]["false"], edn!(:f));
assert_eq!(edn[3]["false"], Edn::Key("f".to_string()));

Serializes Rust Types into EDN

#![recursion_limit="512"]
#[macro_use] extern crate edn_rs;

use std::collections::{HashMap, HashSet};
use crate::edn_rs::serialize::Serialize;

fn main() {
    ser_struct!{
        #[derive(Debug)]
        struct Edn {
            map: HashMap<String, Vec<String>>,
            set: HashSet<i64>,
            tuples: (i32, bool, char),
        }
    };
    let edn = Edn {
        map: map!{"this is a key".to_string() => vec!["with".to_string(), "many".to_string(), "keys".to_string()]},
        set: set!{3i64, 4i64, 5i64},
        tuples: (3i32, true, 'd')
    };
    println!("{}",edn.serialize());
    // { :map {:this-is-a-key ["with", "many", "keys"]}, :set #{3, 4, 5}, :tuples (3, true, \d), }
}

Emits EDN format from a Json file

use edn_rs::emit_edn;

fn main() {
    let json = String::from("{\"hello\": \"world\"}");
    let edn = String::from("{:hello \"world\"}");

    assert_eq!(edn, emit_edn(json));
}

Current Features

  • Define struct to map EDN info EdnNode
  • Define EDN types, EdnType
  • Parse simples EDN data:
    • 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}" For now the usage of Set is defined as a Vec<Edn>, this is due to the fact that the lib should not be necessarily responsible for assuring the Set's unicity. A solution could be changing the implementation to HashSet.
    • Map "{:a 1 :b 2 }"
  • Simple data structures in one another:
    • 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\"]}"
  • 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
  • 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
  • derive Serialize
  • derive Deserialize