From 23e11fabe65c8bd5d7a9a57d53a1be60698b85f3 Mon Sep 17 00:00:00 2001 From: Richard Newman Date: Wed, 14 Feb 2018 09:32:37 -0800 Subject: [PATCH] Add a var! macro. (#548)(#550) r=emily --- src/lib.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index bf0ce753..06466ecf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -51,6 +51,16 @@ pub use mentat_db::{ new_connection, }; +/// Produce the appropriate `Variable` for the provided valid ?-prefixed name. +/// This lives here because we can't re-export macros: +/// https://github.com/rust-lang/rust/issues/29638. +#[macro_export] +macro_rules! var { + ( ? $var:ident ) => { + $crate::Variable::from_valid_name(concat!("?", stringify!($var))) + }; +} + /// Produce the appropriate `NamespacedKeyword` for the provided namespace and name. /// This lives here because we can't re-export macros: /// https://github.com/rust-lang/rust/issues/29638. @@ -121,4 +131,13 @@ mod tests { assert_eq!(kw!(:foo/bar), NamespacedKeyword::new("foo", "bar")); assert_eq!(kw!(:org.mozilla.foo/bar_baz), NamespacedKeyword::new("org.mozilla.foo", "bar_baz")); } + + #[test] + fn test_var() { + let foo_baz = var!(?foo_baz); + let vu = var!(?vü); + assert_eq!(foo_baz, Variable::from_valid_name("?foo_baz")); + assert_eq!(vu, Variable::from_valid_name("?vü")); + assert_eq!(foo_baz.as_str(), "?foo_baz"); + } }