diff --git a/Cargo.lock b/Cargo.lock index 00bd498..6c846a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,7 @@ version = 4 [[package]] name = "dyn_path" -version = "1.0.0" +version = "1.0.1" dependencies = [ "serde_json", ] diff --git a/Cargo.toml b/Cargo.toml index cab0c76..a242698 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 ", "Chiko "] keywords = [ @@ -18,3 +18,8 @@ keywords = [ [dev-dependencies] serde_json = "1.0.140" + +[features] +default = ["std"] +std = [] +alloc = [] diff --git a/src/lib.rs b/src/lib.rs index 558f805..f56c6c0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -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)*) }}; @@ -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)*) }}; diff --git a/src/test.rs b/src/test.rs index 771df7d..2a22307 100644 --- a/src/test.rs +++ b/src/test.rs @@ -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."; @@ -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]);