get_nth/
get-nth.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use std::collections::BTreeMap;

use clojure_reader::edn::{self, Edn};

fn maybe_forty_two<'a>(edn: &'a Edn<'a>) -> Option<&Edn<'a>> {
  // This roughly tries to match clojure's get and nth
  // (-> (clojure.edn/read-string "{:foo {猫 {{:foo :bar} [1 2 42 3]}}}")
  //   (get :foo)
  //   (get (symbol "猫"))
  //   (get {:foo :bar})
  //   (nth 2))
  edn
    .get(&Edn::Key(":foo"))?
    .get(&Edn::Symbol("猫"))?
    .get(&Edn::Map(BTreeMap::from([(Edn::Key(":foo"), Edn::Key(":bar"))])))?
    .nth(2)
}

fn main() {
  let e = edn::read_string("{:foo {猫 {{:foo :bar} [1 2 42 3]}}}").unwrap();
  let edn = maybe_forty_two(&e).unwrap();
  assert_eq!(edn, &Edn::Int(42));
}

#[test]
fn run() {
  main();
}