From 688564d9d405b2d7a6f10b58b46090e9488c9478 Mon Sep 17 00:00:00 2001 From: bouzuya Date: Thu, 7 Sep 2023 06:41:08 +0900 Subject: [PATCH] Fix to use $crate in path macros (#124) --- src/struct_path_macro.rs | 8 +++---- tests/macro_path_test.rs | 50 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 tests/macro_path_test.rs diff --git a/src/struct_path_macro.rs b/src/struct_path_macro.rs index ec70c82..c7f27d6 100644 --- a/src/struct_path_macro.rs +++ b/src/struct_path_macro.rs @@ -1,27 +1,27 @@ #[macro_export] macro_rules! path { ($($x:tt)*) => {{ - struct_path::path!($($x)*).to_string() + $crate::struct_path::path!($($x)*).to_string() }}; } #[macro_export] macro_rules! paths { ($($x:tt)*) => {{ - struct_path::paths!($($x)*).iter().map(|s| s.to_string()).collect::>() + $crate::struct_path::paths!($($x)*).iter().map(|s| s.to_string()).collect::>() }}; } #[macro_export] macro_rules! path_camel_case { ($($x:tt)*) => {{ - struct_path::path!($($x)*;case="camel").to_string() + $crate::struct_path::path!($($x)*;case="camel").to_string() }}; } #[macro_export] macro_rules! paths_camel_case { ($($x:tt)*) => {{ - struct_path::paths!($($x)*;case="camel").into_iter().map(|s| s.to_string()).collect::>() + $crate::struct_path::paths!($($x)*;case="camel").into_iter().map(|s| s.to_string()).collect::>() }} } diff --git a/tests/macro_path_test.rs b/tests/macro_path_test.rs new file mode 100644 index 0000000..203faa7 --- /dev/null +++ b/tests/macro_path_test.rs @@ -0,0 +1,50 @@ +#[test] +fn test_ambiguous_path_macro() { + struct MyTestStructure { + some_id: String, + some_num: u64, + } + assert_eq!(firestore::path!(MyTestStructure::some_id), "some_id"); + assert_eq!( + firestore::paths!(MyTestStructure::{some_id, some_num}), + vec!["some_id".to_string(), "some_num".to_string()] + ); + assert_eq!( + firestore::path_camel_case!(MyTestStructure::some_id), + "someId" + ); + assert_eq!( + firestore::paths_camel_case!(MyTestStructure::{some_id, some_num}), + vec!["someId".to_string(), "someNum".to_string()] + ); +} + +mod struct_path { + #[macro_export] + macro_rules! path { + () => { + unreachable!() + }; + } + + #[macro_export] + macro_rules! paths { + ($($x:tt)*) => {{ + unreachable!() + }}; + } + + #[macro_export] + macro_rules! path_camel_case { + ($($x:tt)*) => {{ + unreachable!() + }}; + } + + #[macro_export] + macro_rules! paths_camel_case { + ($($x:tt)*) => {{ + unreachable!() + }}; + } +}