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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description = "A JavaScript-like nested object-like structure non-fallible path
repository = "https://github.com/FlakySL/dyn_path"
license = "GPL-3.0"
readme = "./README.md"
version = "1.0.0"
version = "1.0.1"
edition = "2024"
authors = ["Esteve Autet <[email protected]>", "Chiko <[email protected]>"]
keywords = [
Expand All @@ -18,3 +18,8 @@ keywords = [

[dev-dependencies]
serde_json = "1.0.140"

[features]
default = ["std"]
std = []
alloc = []
17 changes: 12 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(all(not(feature = "std"), feature = "alloc"))]
pub extern crate alloc;

#[cfg(test)]
mod test;

Expand Down Expand Up @@ -59,7 +64,7 @@ macro_rules! dyn_access {
}};

(@recurse $acc:expr, . $field:ident $($rest:tt)*) => {{
let __ = $acc.and_then(|v| v.get(::std::stringify!($field)));
let __ = $acc.and_then(|v| v.get(::core::stringify!($field)));
$crate::dyn_access!(@recurse __, $($rest)*)
}};

Expand Down Expand Up @@ -89,21 +94,23 @@ macro_rules! dyn_access {
/// assert_eq!(display_path, r#"nested.path.at[2].with["no"]["head"]"#);
/// ```
/// Notice how the macro pre-computes the indexes and generates the target string.
#[cfg(any(feature = "alloc", feature = "std"))]
#[macro_export]
macro_rules! dyn_path {
($head:ident $($rest:tt)*) => {{
use ::std::fmt::Write;
let mut __ = ::std::stringify!($head).to_string();
use ::core::fmt::Write;
use $crate::alloc::string::ToString;
let mut __ = ::core::stringify!($head).to_string();
$crate::dyn_path!(@recurse __, $($rest)*)
}};

(@recurse $acc:expr, . $field:ident $($rest:tt)*) => {{
let _ = ::std::write!($acc, ".{}", ::std::stringify!($field));
let _ = ::core::write!($acc, ".{}", ::core::stringify!($field));
$crate::dyn_path!(@recurse $acc, $($rest)*)
}};

(@recurse $acc:expr, [$idx:expr] $($rest:tt)*) => {{
let _ = ::std::write!($acc, "[{:?}]", ($idx));
let _ = ::core::write!($acc, "[{:?}]", ($idx));
$crate::dyn_path!(@recurse $acc, $($rest)*)
}};

Expand Down
9 changes: 6 additions & 3 deletions src/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use serde_json::{json, Value};

use crate::{dyn_access, dyn_path};
use crate::dyn_access;
#[cfg(feature = "alloc")]
use crate::dyn_path;

const ERROR: &str = "nested value to exist.";

Expand Down Expand Up @@ -45,13 +47,14 @@ pub fn nested_access() {

#[test]
pub fn direct_expression() {
let vector = vec![map()];
let _1 = dyn_access!((vector[0]).very.nested[1])
let vector = (map(), ());
let _1 = dyn_access!((vector.0).very.nested[1])
.expect(ERROR);

assert_eq!(_1, "of");
}

#[cfg(feature = "alloc")]
#[test]
pub fn path_descriptor() {
let _1 = dyn_path!(very.nested["value"].on.index[1 + 1]);
Expand Down
Loading