Add a var! macro. (#548)(#550) r=emily

This commit is contained in:
Richard Newman 2018-02-14 09:32:37 -08:00 committed by GitHub
parent 2ac7a1b1de
commit 23e11fabe6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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!(?);
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");
}
}