Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ keywords = [
"json",
]


[dev-dependencies]
serde_json = "1.0.140"

Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ Add the following to your `Cargo.toml` under the `dependencies` section
dyn_path = "1.0.0"
```

## No std 💡

This crate supports `no_std`, meaning you can use it in your project without
depending on specific system I/O or anything else.

The crate has a default feature enabled which is `std`, with this feature
the crate doesn't really use `std` but it pre-includes `alloc` which
permits the use of the `dyn_path` macro, which uses `String` to describe
a path.

You can also disable the default features and the crate will annotate `no_std`.
Then use the `alloc` feature if you want to have the `dyn_path` macro enabled.

## License 📜

This repository is dual licensed, TLDR. If your repository is open source, the library
Expand Down
47 changes: 47 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,50 @@
//! # dyn_path
//!
//! dyn_path is a set of macros that permit you access objects
//! that have `.get()` methods that return `Option<T>` in a nested
//! way.
//!
//! It is as specific as it looks, but most libraries that parse
//! data interchange languages have a "Value" that contains other
//! "Value"s inside. And casually all the "Value"s have a `.get()`
//! method, a generic `.get()` method in fact.
//!
//! How does this work? Just like JavaScript.
//! ```rust
//! use serde_json::json;
//! use dyn_path::dyn_access;
//!
//! let object = json!({
//! "very": {
//! "nested": {
//! "value": [
//! "hello",
//! "world"
//! ]
//! }
//! }
//! });
//!
//! let hello = dyn_access!(object.very.nested.value[0]).unwrap();
//! let world = dyn_access!(object.very.nested.value[1]).unwrap();
//!
//! assert_eq!(hello, "hello");
//! assert_eq!(world, "world");
//! ```
//! This is also useful for nested `HashMap`s but the difference is
//! that you will actually get a compile time error if you are wrong
//! with the type.
//! ```rust
//! use std::collections::HashMap;
//! use dyn_path::dyn_access;
//!
//! let map: HashMap<String, HashMap<String, HashMap<i32, ()>>> = HashMap::new();
//!
//! dyn_access!(map.nested.value[0]); // since we don't have any real value this will return None.
//! ```
//! Check the available macro documentation to learn more about how to use
//! the specific macros.

#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(all(not(feature = "std"), feature = "alloc"))]
Expand Down
Loading