Skip to content

Commit

Permalink
get/nth impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Grinkers committed Jun 2, 2024
1 parent d6827a2 commit 4a113b3
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
run: |
cargo test --all-features --no-fail-fast --target ${{ matrix.target }}
cargo test --features std --no-default-features --no-fail-fast --target ${{ matrix.target }}
cargo run --example get-nth --target ${{ matrix.target }}
build_linux:
name: Build Linux
Expand Down Expand Up @@ -113,6 +114,7 @@ jobs:
run: |
cargo test --all-features --no-fail-fast --target ${{ matrix.target }}
cargo test --features std --no-default-features --no-fail-fast --target ${{ matrix.target }}
cargo run --example get-nth --target ${{ matrix.target }}
build_embedded:
name: Build Embedded
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ unsafe_code = "deny"
nursery = { level = "warn", priority = -1 }
pedantic = { level = "warn", priority = -1 }
inline_always = "allow"
must_use_candidate = "allow"

[features]
default = ["floats", "std"]
Expand All @@ -28,3 +29,5 @@ std = []
[dependencies]
ordered-float = { version = "4.1", default-features = false, optional = true }

[[example]]
name = "get-nth"
7 changes: 4 additions & 3 deletions bb.edn
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
:task (fs/delete-tree "target")}
test_lib_features (shell "cargo test --all-features --no-fail-fast")
test_lib_no_default_features (shell "cargo test --features std --no-default-features --no-fail-fast")
test-examples (do (shell "cargo run --example get-nth"))
cargo-test {:doc "Runs all cargo tests"
:depends [test_lib_features test_lib_no_default_features]}
:depends [test_lib_features test_lib_no_default_features test-examples]}
cargo-fmt {:doc "Checks cargo fmt"
:task (shell "cargo fmt --check")}
cargo-clippy-all-features {:doc "Cargo clippy with all features"
:task (shell "cargo clippy --all-features -- -W future-incompatible -W rust_2018_idioms -W clippy::all -W clippy::pedantic -W clippy::nursery --deny warnings")}
:task (shell "cargo clippy --all-features -- --deny warnings")}
cargo-clippy-no-defaults {:doc "Cargo clippy with no default features"
:task (shell "cargo clippy --no-default-features -- -W future-incompatible -W rust_2018_idioms -W clippy::all -W clippy::pedantic -W clippy::nursery --deny warnings")}
:task (shell "cargo clippy --no-default-features -- --deny warnings")}
clippy {:doc "Runs all variations of cargo clippy"
:depends [cargo-clippy-all-features cargo-clippy-no-defaults]}
cov-all-features {:doc "Coverage, all features"
Expand Down
18 changes: 18 additions & 0 deletions examples/get-nth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use clojure_reader::edn::{self, Edn};

fn maybe_forty_two<'a>() -> Option<i64> {
let e = edn::read_string("{:foo {:bar [1 2 42 3]}}").unwrap();

// This roughly tries to match clojure's
// (-> (clojure.edn/read-string "{:foo {:bar [1 2 42 3]}}") (get :foo) (get :bar) (nth 2)) -> 42
let idx = e.get(&Edn::Key(":foo"))?.get(&Edn::Key(":bar"))?.nth(2)?;

if let Edn::Int(i) = idx {
return Some(*i);
}
None
}

fn main() {
println!("{:?}", maybe_forty_two());
}
21 changes: 21 additions & 0 deletions src/edn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@ pub fn read_string(edn: &str) -> Result<Edn<'_>, crate::error::Error> {
crate::parse::parse(edn)
}

impl<'e> Edn<'e> {
pub fn get(&self, e: &Edn<'e>) -> Option<&Edn<'e>> {
if let Edn::Map(m) = self {
let lol = m.get(e);
if let Some(l) = lol {
return Some(l);
};
}
None
}
pub fn nth(&self, i: usize) -> Option<&Edn<'e>> {
let vec = match self {
Edn::Vector(v) => v,
Edn::List(l) => l,
_ => return None,
};

vec.get(i)
}
}

const fn char_to_edn(c: char) -> Option<&'static str> {
match c {
'\n' => Some("newline"),
Expand Down

0 comments on commit 4a113b3

Please sign in to comment.